0

There is no errors but when I Run it the content that I added into the JPanel won't appear, only the one not inside the JPanel appear.

import javax.swing.*; 

import java.awt.*;

public class SimpleGUI extends JFrame 
{ 

        public static void main(String arg[]) 
        { 
                SimpleGUI f = new SimpleGUI("GUI components"); 
                f.setSize(600,200); 
                f.setVisible(true); 
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         } 
        SimpleGUI(String s) 
         { 


                setTitle(s); 
                setLayout(new GridLayout(3,2)); 

                JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!"); 
                JButton bt = new JButton("OK"); 

                JLabel lb = new JLabel ("Enter your name:"); 
                JTextField tf = new JTextField("<type name here>"); 

                JLabel lb2 = new JLabel ("Enter age:"); 
                JTextField tf2= new JTextField(10);
                tf2.setHorizontalAlignment(JTextField.RIGHT); 

                JCheckBox cb = new JCheckBox("Bold",true); 
                JRadioButton rb1 = new JRadioButton("Red"); 

                JTextArea ta = new JTextArea(5,20);
                JList list = new JList(new Object[] {"Block A", "Block B"}); 
                JComboBox jcb = new JComboBox(new Object[] {"Hello", "Bye"}); 

                ImageIcon ic = new ImageIcon("music.gif"); 
                JButton newbt = new JButton("Play",ic); 
                newbt.setVerticalTextPosition(JButton.TOP); 
                newbt.setHorizontalTextPosition(JButton.CENTER); 

                JPanel p1 = new JPanel(); 
                p1.setLayout(new BorderLayout()); 
                p1.add(lb, BorderLayout.WEST); 
                p1.add(tf, BorderLayout.CENTER);
                p1.add(cb, BorderLayout.EAST); 

                JPanel p2 = new JPanel();
                p2.setLayout(new BorderLayout()); 
                p2.add(lb2, BorderLayout.WEST); 
                p2.add(tf2, BorderLayout.CENTER);                 
                p2.add(rb1, BorderLayout.EAST);

                JPanel p3 = new JPanel();
                p3.setLayout(new BorderLayout());
                p3.add(jcb); 
                add(ta); 
                add(list);
                p3.add(newbt, BorderLayout.NORTH); 
                add(msg);
                p3.add(bt, BorderLayout.SOUTH); 
        } 
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Pickle
  • 19
  • 1
  • 1
  • 3

2 Answers2

3

I've updated your code. Have a look at this version:

import javax.swing.*;

import java.awt.*;

public class SimpleGUI extends JFrame {

    public static void main(String arg[]) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                SimpleGUI f = new SimpleGUI("GUI components");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }

        });

    }

    public SimpleGUI(String s) {


        setTitle(s);
        setLayout(new GridLayout(3, 2));

        JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
        JButton bt = new JButton("OK");

        JLabel lb = new JLabel("Enter your name:");
        JTextField tf = new JTextField("<type name here>");

        JLabel lb2 = new JLabel("Enter age:");
        JTextField tf2 = new JTextField(10);
        tf2.setHorizontalAlignment(JTextField.RIGHT);

        JCheckBox cb = new JCheckBox("Bold", true);
        JRadioButton rb1 = new JRadioButton("Red");

        JTextArea ta = new JTextArea(5, 20);
        JList list = new JList(new Object[]{"Block A", "Block B"});
        JComboBox jcb = new JComboBox(new Object[]{"Hello", "Bye"});

        ImageIcon ic = new ImageIcon("music.gif");
        JButton newbt = new JButton("Play", ic);
        newbt.setVerticalTextPosition(JButton.TOP);
        newbt.setHorizontalTextPosition(JButton.CENTER);

        JPanel p1 = new JPanel();
        p1.setLayout(new BorderLayout());
        p1.add(lb, BorderLayout.WEST);
        p1.add(tf, BorderLayout.CENTER);
        p1.add(cb, BorderLayout.EAST);

        JPanel p2 = new JPanel();
        p2.setLayout(new BorderLayout());
        p2.add(lb2, BorderLayout.WEST);
        p2.add(tf2, BorderLayout.CENTER);
        p2.add(rb1, BorderLayout.EAST);

        JPanel p3 = new JPanel();
        p3.setLayout(new BorderLayout());
        p3.add(jcb);
        add(ta);
        add(list);
        p3.add(newbt, BorderLayout.NORTH);
        add(msg);
        p3.add(bt, BorderLayout.SOUTH);

        /**
         * Need to add the following lines
         */
        this.add(p1);
        this.add(p2);
        this.add(p3);

        this.pack();
        this.setVisible(true);
    }
}

A couple of pointers:

  • You need to add your components to your JFrame for them to actually show up.

  • Any updates to the user interface must happen on the event dispatch thread. Consequently you would notice that I've added a SwingUtilites.invokeLater() to the main. Have a look at this article to understand "Threading with Swing"

Sujay
  • 6,753
  • 2
  • 30
  • 49
  • Consider omitting `setSize()` and calling `setVisible()` just once. +1 for `invokeLater()`. – trashgod Aug 13 '12 at 04:59
  • 1
    woops, I didn't see the `setVisible()` in `main()`. Thanks for pointing that out! – Sujay Aug 13 '12 at 05:02
  • @BruceMartin - Perhaps this SO article can help answer your question: http://stackoverflow.com/questions/7554125/when-should-i-use-jframe-addcomponent-and-jframe-getcontentpane-addcomponen – Sujay Aug 13 '12 at 06:21
  • @BruceMartin: Sujay is right; here's another [perspective](http://stackoverflow.com/a/11769153/230513). – trashgod Aug 13 '12 at 09:54
  • @pickle: glad to help! if this was useful for you, do consider accepting the answer! :) – Sujay Aug 14 '12 at 15:40
0

Where you you add your panels to the frame? Also, forgotten my java "rules and regulations": do you need to call "super()"?

John3136
  • 28,809
  • 4
  • 51
  • 69