0
我在JPanel中創建了一個JPanel,我想將其寬度和高度屬性設置爲某些特定的數字。這是我到目前爲止。JPanel的Java設置尺寸
searchPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
searchPanel.setBorder(BorderFactory.createCompoundBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED), new EmptyBorder(12, 12, 12, 12)));
c.insets = new Insets(2, 2, 2, 2);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.weighty = 1;
c.gridx = 0;
c.gridy = 0;
searchPanel.add(typeLabel, c);
c.weightx = .8;
c.weighty = 1;
c.gridx = 0;
c.gridy = 1;
searchPanel.add(typeComboBox, c);
....
yearComboBox.setModel(this.yearDefaultCombo);
typeComboBox.setModel(this.typeDefaultCombo);
updateYearComboBox();
this.setLayout(new BorderLayout(10, 10));
this.setBorder(new EmptyBorder(10, 10, 10, 10));
searchPanel.setSize(200, 100);
this.add(searchPanel, BorderLayout.NORTH);
在代碼中,「this」是指父級JPanel。現在,由於BorderLayout.NORTH約束,我的內部「searchPanel」沿屏幕的整個頂部延伸,但我只希望它作爲左上角的一個小面板,但setSize對我不起作用。
謝謝。