Hello World message disappeared?!

G

Geoff Cox

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_CLOSE);
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();
}
});
}
}
 
A

Andrew Thompson

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
 
T

Thomas Fritsch

Geoff said:
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_CLOSE);
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();
}
});
}
}
 
G

Geoff Cox

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_CLOSE);
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();
}
});
}
}
 
G

Geoff Cox

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top