0

没有错误,但是当我运行它时,我添加到 JPanel 中的内容不会出现,只会出现不在 JPanel 内的内容。

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); 
        } 
}
4

2 回答 2

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"

于 2012-08-13T04:54:45.527 回答
0

您在哪里将面板添加到框架中?另外,忘记了我的java“规则和规定”:你需要调用“super()”吗?

于 2012-08-13T04:40:53.023 回答