Java 当在JButton之前添加Jlabel时,如何阻止JButton改变位置
我有一个最初为空的JLabel(SeriesInformation标签)。旁边有一个Jbutton(复制按钮)。问题是每次JLabel加载文本值时,JButton都会向右移动。如何阻止这个按钮移动Java 当在JButton之前添加Jlabel时,如何阻止JButton改变位置,java,swing,jbutton,jlabel,Java,Swing,Jbutton,Jlabel,我有一个最初为空的JLabel(SeriesInformation标签)。旁边有一个Jbutton(复制按钮)。问题是每次JLabel加载文本值时,JButton都会向右移动。如何阻止这个按钮移动 private void init() { super.initializeLayout(); this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); Box buttonPanel = Box.createHorizon
private void init() {
super.initializeLayout();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
Box buttonPanel = Box.createHorizontalBox();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
buttonPanel.setLayout(null);
copyButton = new JButton(Utilities.getString("COPY"));
copyButton.setActionCommand(COPY);
copyButton.setEnabled(false);
seriesInformationLabel = new JLabel();
seriesInformationLabel.setAlignmentY(CENTER_ALIGNMENT);
seriesInformationLabel.setName(this.getClass().getSimpleName() + "_seriesInformationLabel");
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
buttonPanel.add(Box.createHorizontalGlue());
}
自由空间随着标签文本的变化而变化
这意味着“胶水”的可用空间将在两个胶水组件之间均匀变化,从而导致按钮移动
如果不希望按钮移动,则需要去掉第二个“粘合”组件:
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
//buttonPanel.add(Box.createHorizontalGlue());
如果不希望按钮完全位于面板的右边缘,可以添加另一个刚性区域
buttonPanel.add(seriesInformationLabel);
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(copyButton);
//buttonPanel.add(Box.createHorizontalGlue());