我JFrame
看起来像这样:
当它被调整大小时,它会JComboBoxes
适应这样的大小JFrame
:
这就是我想要的。但是当我添加 a 时JScrollPane
,JComboBoxes
不再适应:
任何想法如何使 JComboBoxes 仍然用添加的 填充 HORIZONTAL JScrollPane
?(使JScrollPane
唯一的效果是 VERTICAL 滚动)
public class FillHorizonScrollVertical extends JFrame {
private boolean withScroller = true;
private JPanel content;
public FillHorizonScrollVertical() {
// FILL = BOTH for JFrame
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
this.setLayout(new GridBagLayout());
// GridLayout = All content gets the same size
content = new JPanel(new GridLayout());
// ADD TO THE FRAME
JScrollPane scroller = new JScrollPane(content);
if (withScroller)
this.add(scroller, c);
else
this.add(content, c);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// ADDING THE BOXES AFTER SETTING VISIBLE
addBoxes();
this.pack();
}
/**
* Create JComboBoxes and adding them to the JPanel
*/
private void addBoxes() {
// CREATE JCOMBOBOXES
JComboBox cb1 = new JComboBox();
cb1.addItem("");
cb1.addItem("aaaaaaaaaaaaaaaaaa");
JComboBox cb2 = new JComboBox();
cb2.addItem("");
JComboBox cb3 = new JComboBox();
cb3.addItem("");
content.add(cb1);
content.add(cb2);
content.add(cb3);
}
public static void main(String[] args) {
new FillHorizonScrollVertical();
}
}