我已經爲項目嘲笑了一個快速的GUI,我可以看到標籤和文本字段,但出於某種原因,窗口不會重新調整大小。JFrame沒有調整大小以適合JPanel
這裏是基本的破敗:
創建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);
}
我基本上得到一個窗口大小適當,但字段水平隱藏。
爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –