Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当在JButton之前添加Jlabel时,如何阻止JButton改变位置_Java_Swing_Jbutton_Jlabel - Fatal编程技术网

Java 当在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

我有一个最初为空的JLabel(SeriesInformation标签)。旁边有一个Jbutton(复制按钮)。问题是每次JLabel加载文本值时,JButton都会向右移动。如何阻止这个按钮移动

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());