使用此代碼,我將有以下窗口。我創建了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);
我願意與 – MadProgrammer