Hello World message disappeared?!

Discussion in 'Java' started by Geoff Cox, Jul 15, 2005.

  1. Geoff Cox

    Geoff Cox Guest

    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();
    }
    });
    }
    }
     
    Geoff Cox, Jul 15, 2005
    #1
    1. Advertising

  2. 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
     
    Andrew Thompson, Jul 15, 2005
    #2
    1. Advertising

  3. 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_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();
    > }
    > });
    > }
    > }
    >
    >

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Jul 15, 2005
    #3
  4. Geoff Cox

    Geoff Cox Guest

    On Fri, 15 Jul 2005 10:43:57 GMT, Thomas Fritsch
    <> 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_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();
    >> }
    >> });
    >> }
    >> }
    >>
    >>
     
    Geoff Cox, Jul 15, 2005
    #4
  5. Geoff Cox

    Geoff Cox Guest

    On Fri, 15 Jul 2005 10:34:12 GMT, Andrew Thompson
    <> 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
     
    Geoff Cox, Jul 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl
    Replies:
    4
    Views:
    421
    Peter Hansen
    May 21, 2004
  2. vijay
    Replies:
    8
    Views:
    723
  3. Roy
    Replies:
    6
    Views:
    635
    Roedy Green
    Jan 7, 2008
  4. HipHop

    Validator message disappeared after post back

    HipHop, Dec 22, 2006, in forum: ASP .Net Building Controls
    Replies:
    8
    Views:
    238
    HipHop
    Jan 17, 2007
  5. Larry
    Replies:
    27
    Views:
    435
    Michele Dondi
    Jan 25, 2005
Loading...

Share This Page