【发布时间】:2015-07-31 18:51:38
【问题描述】:
我正在努力完成以下工作:
- JFrame 底部的页脚,包含两个组件:左侧的 JLabel 和右侧包含三个按钮的面板
- 我希望右侧按钮面板占用尽可能少的空间,同时 JLabel 填充托管 JFrame 的宽度减去按钮面板的宽度。
- JLabel 周围有斜角边框,而按钮则没有
- 按钮之间几乎没有空格
我一直在测试各种 Swing 布局以使其正常工作,但到目前为止还没有运气。目前我正在按照教程尝试 GridBagConstraints;这是现在的样子:
如您所见,它不起作用,我不知道为什么。
这是我当前的代码:
public class MessageFooter extends JPanel {
private static final int BORDER_WIDTH = 5;
private final JLabel _recentMessage = new JLabel();
private int _footerHeight;
/*package*/ MessageFooter() {
// Set the size of the footer
_footerHeight = FlowFramePrefs.getUserPrefFontSize() + (BORDER_WIDTH * 2);
this.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
this.setMaximumSize(new java.awt.Dimension(java.lang.Integer.MAX_VALUE, _footerHeight));
this.setMinimumSize(new java.awt.Dimension(0, _footerHeight));
this.setPreferredSize(new Dimension(_footerHeight, _footerHeight));
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
// JLabel
JPanel labelPanel = new JPanel();
_recentMessage.setText(" Blahhhhh");
labelPanel.add(_recentMessage);//, BorderLayout.CENTER);
labelPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
//_recentMessage.setAlignmentX(Component.LEFT_ALIGNMENT);
// Buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
//buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
// Create the buttons
JButton upprB = new JButton("U");
upprB.setMargin(new Insets(0, 0, 0, 0));
upprB.setBorder(new EmptyBorder(0, 0, 0, 0));
JButton downB = new JButton("D");
downB.setMargin(new Insets(0, 0, 0, 0));
downB.setBorder(new EmptyBorder(0, 0, 0, 0));
JButton openB = new JButton("O");
openB.setMargin(new Insets(0, 0, 0, 0));
openB.setBorder(new EmptyBorder(0, 0, 0, 0));
buttonPanel.add(upprB);
buttonPanel.add(downB);
buttonPanel.add(openB);
// Add both
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 0;
this.add(labelPanel, c);
c.weightx = 0.0;
c.gridx = 1;
c.gridy = 0;
this.add(buttonPanel, c);
}
...
}
【问题讨论】:
-
你在使用像 Netbeans 这样的 IDE 吗?
标签: java swing layout jpanel layout-manager