我真的很生气,因为我知道一个问题,但事实并非如此!每次在 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);