java gui大小_调整Java GUI大小时防止垂直间距

我有一个简单的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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值