我有一个简单的GUI,我正在尝试为窗口的左侧创建按钮和控件。右侧有一个文本区域,最终将显示内容。左侧包含用户操作的按钮和控件。我已经使用了一系列布局管理器(而且它们看起来相当挑剔)来制作我现在拥有的东西。
我查看了关于BoxLayout的Oracle文档,这是左控件容器正在使用的内容,我没有看到在窗口调整大小时阻止按钮分开的方法。我希望他们能够在顶部被砸碎,然后在没有间隔的情况下呆在那里。 BoxLayout的“胶水”功能并没有真正做到你想象的那样,它应该被称为橡皮筋。
我的问题是,当屏幕调整大小时,如何保持左侧内容的分离范围更广?
我的GUI:
public class TestCode extends JFrame{
JTextArea textArea = new JTextArea ();
JComboBox typeComboBox;
JTextField searchField;
JTextField fileField;
public TestCode() {
System.out.println ("In constructor");
setTitle ("GUI Test");
setSize (600, 300);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
JButton readButton = new JButton("Read File");
JButton displayButton = new JButton("Display");
JButton searchButton = new JButton("Search");
searchField = new JTextField(10);
fileField = new JTextField(15);
typeComboBox = new JComboBox ();
typeComboBox.addItem("Index");
typeComboBox.addItem("Type");
typeComboBox.addItem("Name");
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
JPanel filePanel = new JPanel();
filePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
filePanel.add(new JLabel("Source file:", SwingConstants.RIGHT));
filePanel.add(fileField);
filePanel.add(readButton);
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
displayPanel.add(new JLabel("Display data:", SwingConstants.RIGHT));
displayPanel.add(displayButton);
JPanel searchPanel = new JPanel();
searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
searchPanel.add(new JLabel ("Search target"));
searchPanel.add(Box.createHorizontalBox());
searchPanel.add(searchField);
searchPanel.add(typeComboBox);
searchPanel.add(Box.createHorizontalBox());
searchPanel.add(searchButton);
container.add(filePanel);
container.add(displayPanel);
container.add(searchPanel);
add(container, BorderLayout.WEST);
validate();
}