我已经为一个项目模拟了一个快速的 GUI,我可以看到标签和文本字段,但由于某种原因,窗口没有根据它包含的内容重新调整大小。
这是基本的破败:
创建 JFrame 并添加 JPanel:
JFrame frame1 = new JFrame("Hotel Reservation App");
frame1.getContentPane().add(rViewPan, BorderLayout.CENTER);
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);
这是 rViewPan 的 JPanel 代码:
private void initComponents() {
setLayout(new BorderLayout());
arrivalDateTF = new JTextField();
departureDateTF = new JTextField();
roomCategoryTF = new JTextField();
roomQtyTF = new JTextField();
JTextField[] textFields = { arrivalDateTF, departureDateTF, roomCategoryTF, roomQtyTF };
JLabel[] textLabels = { new JLabel("1 : "), new JLabel("2 : "),
new JLabel("3 : "), new JLabel("4 : ")
};
JPanel displayPan = new JPanel();
GridBagLayout gridBagLay = new GridBagLayout();
GridBagConstraints gridBagC = new GridBagConstraints();
displayPan.setLayout(gridBagLay);
SwingUtilities.addTextElementsAsRows(textLabels, textFields, gridBagLay, displayPan);
gridBagC.gridwidth = GridBagConstraints.REMAINDER;
gridBagC.anchor = GridBagConstraints.EAST;
gridBagC.weightx = 1.0;
displayPan.add(new JLabel(" "), gridBagC);
submitB = new JButton("Soumettre");
displayPan.add(submitB, gridBagC);
SwingUtilities.addStdBorder(displayPan, "Reservation");
add(displayPan, BorderLayout.CENTER);
}
我基本上得到了一个大小合适的窗口,但是这些字段是水平隐藏的。