使用此代码,我将拥有以下窗口。我创建了 2 个面板并将 mainp 一个添加到框架中,并将面板添加到 mainp 我这样做是为了使窗口大小调整为动态的(因此面板不会根据框架调整大小)我尝试使我的默认面板大小更宽,以便文本字段和标签变得更宽,但 panel.setsize 似乎没有做任何事情。
// creates the labels
studId = new JLabel("Student ID");
studAvg = new JLabel("Student Average");
studName = new JLabel("Student Name");
// creates the text fields
JTextField studIdText = new JTextField();
JTextField studAvgText = new JTextField();
JTextField studNameText = new JTextField();
JPanel mainp = new JPanel();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 2, 2));
panel.setSize(300, 100);
// adds to the GridLayout
panel.add(studId);
panel.add(studIdText);
panel.add(studName);
panel.add(studNameText);
panel.add(studAvg);
panel.add(studAvgText);
mainp.add(panel);
add(BorderLayout.CENTER,mainp);
// verifies the textfields
studIdText.setInputVerifier(new IntVerifier());
studAvgText.setInputVerifier(new DoubleVerifier());
setTitle("Student Form");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);