java - GridBagLayout Swing 中的 JSpinners 和 JButton
问题描述
这是我创建消息面板的代码:
private class MessagePane extends JPanel {
private MessagePane() {
setLayout(new GridBagLayout());
setBackground(backgroundColor);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
add(allowButton, gbc);
gbc.gridx = 1;
add(blockButton, gbc);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
add(timerL, gbc);
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(messageArea), gbc);
gbc = new GridBagConstraints();
gbc.gridy = 2;
gbc.gridx = 0;
add(countryList, gbc);
gbc.gridx = 1;
add(msgList, gbc);
gbc.gridx = 2;
add(sendButton, gbc);
setVisible(true);
}
}
这看起来像:
我想要实现的观点:
你能帮我解决这个问题吗?
解决方案
您没有正确设置 gbc 约束宽度,但话虽如此,对于这个 GUI,我不会使用 GridBagLayout。我会:
- 对整个 JPanel 使用 BorderLayout
- 使用 GridLayout 将顶部按钮添加到 JPanel,其中 1 行 2 列,中间有一些空间
- 将该 JPanel 放入主 JPanel
BorderLayout.PAGE_START
- 放置中心组件
BorderLayout.CENTER
- 使用沿线(而不是页面)定向的 BoxLayout 为底部创建一个 JPanel
- 将其添加到主 JPanel
BorderLayout.PAGE_END
推荐阅读
- node.js - 如何从我的 MongoDB 查询中获取值
- javascript - 如何使用此 axios 代码实现拦截器
- satellite - azure-maps-control StyleOptions interface is not style?: string its style?:vector
- javascript - 将数组项传递到 googlesheets 中的 forEach 循环的问题
- django - Django 模型如何与非 Django 文件交互
- android - 无法在android上运行反应本机应用程序
- cache-control - Ping Federate - 引擎节点的缓存控制
- powerbi - 仅显示或过滤计算列中的不同数据
- spring-boot - Spring PushGateway 在没有实例标签的情况下推送指标
- android - OkHttp 忽略 GET 参数