我真的很生气,因为我知道一个不在我身边的事实!每次我在Eclipse中运行当前项目时,每次显示都不同! 我的意思是,我运行一次它只显示我的三个组件。 我再次运行它显示更多。 我再次运行它,它显示三个 等等.. 这到底是怎么回事。这是我在主字符串args中运行的代码:
JFrame frame = new JFrame("Survey");
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(Color.GRAY);
frame.add(panel, BorderLayout.NORTH);
GridBagConstraints c = new GridBagConstraints();
JLabel label1 = new JLabel("First name:");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(5, 5, 5, 5);
panel.add(label1, c);
JLabel label2 = new JLabel("Last name:");
c.gridx = 0;
c.gridy = 1;
panel.add(label2, c);
JTextField text1 = new JTextField(10);
c.gridx = 1;
c.gridy = 0;
panel.add(text1, c);
JTextField text2 = new JTextField(10);
c.gridx = 1;
c.gridy = 1;
panel.add(text2, c);
JLabel label3 = new JLabel("What is your favorite sport:");
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
panel.add(label3, c);
String[] combobox = {"Basketball", "Soccor", "Other"};
JComboBox cbx = new JComboBox(combobox);
cbx.setPreferredSize(new Dimension(150, 20));
c.gridx = 0;
c.gridy = 3;
panel.add(cbx, c);
JLabel label4 = new JLabel("Comments about yourself:");
c.gridx = 0;
c.gridy = 4;
panel.add(label4, c);
JTextArea area = new JTextArea();
area.setPreferredSize(new Dimension(185, 100));
c.gridx = 0;
c.gridy = 5;
c.gridheight = 5;
panel.add(area, c);
JButton submit = new JButton("Submit");
submit.setPreferredSize(new Dimension(20, 20));
c.gridx = 0;
c.gridy = 6;
panel.add(submit, c);
答案 0 :(得分:1)
根本不需要重新验证。 确保在EDT主题上正确创建组件,您将不再遇到此类问题。请查看以下文章http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html