Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Hello World message disappeared?!

Reply
Thread Tools

Hello World message disappeared?!

 
 
Geoff Cox
Guest
Posts: n/a
 
      07-15-2005
Hello,

I am trying out some of the Sun Java sample code and starting to learn
about swing gui etc.

I have added the slider object and now the Hello World message no
longewr appears. Why is this?

Thanks

Geoff

import javax.swing.*;

public class HelloWorldSwing {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(800, 600);

//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);

//Add slider
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
frame.getContentPane().add(slider);

//Display the window.

// frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}


 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      07-15-2005
On Fri, 15 Jul 2005 10:11:36 GMT, Geoff Cox wrote:

> I am trying out some of the Sun Java sample code and starting to learn
> about swing gui etc.


First you need to learn about AWT layouts
(specifically BorderLayout[1], in this instance).
[1] <http://java.sun.com/j2se/1.5.0/docs/api/java/awt/BorderLayout.html>

GUI questions are best pursued on the GUI group.
<http://www.physci.org/codes/javafaq.jsp#cljg>

Please note that the group 'comp.lang.java.misc' is
not one of the more productive groups, I will set the
Follow-Ups of this message to c.l.j.programmer only.

Check the other groups listed at the GUI link for a
brief run-down of the major Java groups.

HTH

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Mr Bender's Wardrobe By ROBOTANY 500
 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      07-15-2005
Geoff Cox wrote:
> Hello,

Hello!
>
> I am trying out some of the Sun Java sample code and starting to learn
> about swing gui etc.
>
> I have added the slider object and now the Hello World message no
> longer appears. Why is this?

Because the JFrame's contentPane a has BorderLayout as its
LayoutManager. You add both components (the label and the slider)
without specifying a constraint for each. So both get added with the
same default constraint (which happens to be BorderLayout.CENTER).
Solution: You have to add both components with different constraints
(see below in your code)
>
> Thanks
>
> Geoff
>
> import javax.swing.*;

import java.awt.BorderLayout;
>
> public class HelloWorldSwing {
> /**
> * Create the GUI and show it. For thread safety,
> * this method should be invoked from the
> * event-dispatching thread.
> */
> private static void createAndShowGUI() {
> //Make sure we have nice window decorations.
> JFrame.setDefaultLookAndFeelDecorated(true);
>
> //Create and set up the window.
> JFrame frame = new JFrame("HelloWorldSwing");
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
> frame.setSize(800, 600);
>
> //Add the ubiquitous "Hello World" label.
> JLabel label = new JLabel("Hello World");
> //frame.getContentPane().add(label);

frame.getContentPane().add(label, BorderLayout.CENTER);

>
> //Add slider
> JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
> //frame.getContentPane().add(slider);

frame.getContentPane().add(slider, BorderLayout.SOUTH);
>
> //Display the window.
>
> // frame.pack();
> frame.setVisible(true);
> }
>
> public static void main(String[] args) {
> //Schedule a job for the event-dispatching thread:
> //creating and showing this application's GUI.
> javax.swing.SwingUtilities.invokeLater(new Runnable() {
> public void run() {
> createAndShowGUI();
> }
> });
> }
> }
>
>

--
"Thomas:Fritsch$ops:de".replace(':','.').replace(' $','@')

 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      07-15-2005
On Fri, 15 Jul 2005 10:43:57 GMT, Thomas Fritsch
<(E-Mail Removed)> wrote:

>Geoff Cox wrote:
>> Hello,

>Hello!
>>
>> I am trying out some of the Sun Java sample code and starting to learn
>> about swing gui etc.
>>
>> I have added the slider object and now the Hello World message no
>> longer appears. Why is this?

>Because the JFrame's contentPane a has BorderLayout as its
>LayoutManager. You add both components (the label and the slider)
>without specifying a constraint for each. So both get added with the
>same default constraint (which happens to be BorderLayout.CENTER).
>Solution: You have to add both components with different constraints
>(see below in your code)


Thomas - many thanks - a little more mist has cleared!

Cheers

Geoff



>> Thanks
>>
>> Geoff
>>
>> import javax.swing.*;

> import java.awt.BorderLayout;
>>
>> public class HelloWorldSwing {
>> /**
>> * Create the GUI and show it. For thread safety,
>> * this method should be invoked from the
>> * event-dispatching thread.
>> */
>> private static void createAndShowGUI() {
>> //Make sure we have nice window decorations.
>> JFrame.setDefaultLookAndFeelDecorated(true);
>>
>> //Create and set up the window.
>> JFrame frame = new JFrame("HelloWorldSwing");
>> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
>> frame.setSize(800, 600);
>>
>> //Add the ubiquitous "Hello World" label.
>> JLabel label = new JLabel("Hello World");
>> //frame.getContentPane().add(label);

> frame.getContentPane().add(label, BorderLayout.CENTER);
>
>>
>> //Add slider
>> JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
>> //frame.getContentPane().add(slider);

> frame.getContentPane().add(slider, BorderLayout.SOUTH);
>>
>> //Display the window.
>>
>> // frame.pack();
>> frame.setVisible(true);
>> }
>>
>> public static void main(String[] args) {
>> //Schedule a job for the event-dispatching thread:
>> //creating and showing this application's GUI.
>> javax.swing.SwingUtilities.invokeLater(new Runnable() {
>> public void run() {
>> createAndShowGUI();
>> }
>> });
>> }
>> }
>>
>>


 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      07-15-2005
On Fri, 15 Jul 2005 10:34:12 GMT, Andrew Thompson
<(E-Mail Removed)> wrote:

>On Fri, 15 Jul 2005 10:11:36 GMT, Geoff Cox wrote:
>
>> I am trying out some of the Sun Java sample code and starting to learn
>> about swing gui etc.

>
>First you need to learn about AWT layouts
>(specifically BorderLayout[1], in this instance).
>[1] <http://java.sun.com/j2se/1.5.0/docs/api/java/awt/BorderLayout.html>
>
>GUI questions are best pursued on the GUI group.
><http://www.physci.org/codes/javafaq.jsp#cljg>
>
>Please note that the group 'comp.lang.java.misc' is
>not one of the more productive groups, I will set the
>Follow-Ups of this message to c.l.j.programmer only.
>
>Check the other groups listed at the GUI link for a
>brief run-down of the major Java groups.


Andrew,

Thanks for the pointers.

Cheers

Geoff

>
>HTH


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression to parse {"hello", "hello world","1hello-2*hello"} Roy Java 6 01-07-2008 08:06 PM
to print in the reverse order, ("Hello World" -> "World Hello") vijay C Programming 8 04-26-2005 02:11 AM
The world's shortest 'Hello World!' program: a proposal Larry Perl Misc 27 01-25-2005 07:53 PM



Advertisments