我有一個JPanel是這樣的:JTextField的大小
GridBagConstraints constPanelInfo = new GridBagConstraints();
panelInfo = new JPanel(new GridBagLayout());
JLabel sensor1 = new JLabel("Sensor: ");
constPanelInfo.gridx = 0;
constPanelInfo.gridy = 0;
constPanelInfo.weightx = 0.0;
constPanelInfo.fill = GridBagConstraints.NONE;
panelInfo.add(sensor1, constPanelInfo);
constPanelInfo.gridx = 1;
constPanelInfo.gridy = 0;
constPanelInfo.weightx = 1.0;
constPanelInfo.fill = GridBagConstraints.HORIZONTAL;
campoSensor.setPreferredSize(new Dimension(100, campoSensor.getPreferredSize().height));
panelInfo.add(campoSensor, constPanelInfo);
其中campoSensor被定義爲:
private JTextField campoSensor = new JTextField();
...但我想JTextField是沒有擴展到100像素重量:它保持它在寫入setPreferredSize方法之前的大小。這裏有什麼想法?
使用JTextField(int columns)構造函數會比假設您可以猜測此GUI元素的寬度應該更好。 –
您可以顯示將'panelInfo'添加到'JFrame'或'JDialog'的代碼部分嗎?你是否在該框架或對話框中調用'pack()'? – jfpoilpret