Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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 GUI组件会移动?_Java_User Interface_Swing - Fatal编程技术网

当另一个组件不可见时,为什么Java GUI组件会移动?

当另一个组件不可见时,为什么Java GUI组件会移动?,java,user-interface,swing,Java,User Interface,Swing,这就是我面临的问题。我有一个包含3个JPanel的JToolBar,每个面板包含一些不同的组件。包含我的JProgressBar的面板只有在加载内容时才可见。当它从可见变为不可见时,我的其他两个面板会移动大约1像素。代码如下: private JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL); private JPanel progressPanel = new JPanel(); private JPane

这就是我面临的问题。我有一个包含3个JPanel的JToolBar,每个面板包含一些不同的组件。包含我的JProgressBar的面板只有在加载内容时才可见。当它从可见变为不可见时,我的其他两个面板会移动大约1像素。代码如下:

private JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL);
private JPanel progressPanel = new JPanel();
private JPanel globalPanel = new JPanel();
private JPanel cameraPanel = new JPanel();
private JLabel cameraLabel = new JLabel("Camera: ");
private JLabel cameraCoords = new JLabel();
private JLabel globalLabel = new JLabel("Global: ");
private JLabel globalCoords = new JLabel();

progressPanel.setLayout(new BoxLayout(progressPanel, BoxLayout.X_AXIS));
progressPanel.setBackground(Color.RED);
globalPanel.setLayout(new BoxLayout(globalPanel, BoxLayout.X_AXIS));
globalPanel.setBackground(Color.BLUE);
cameraPanel.setLayout(new BoxLayout(cameraPanel, BoxLayout.X_AXIS));
cameraPanel.setBackground(Color.GREEN);

progressBar.setFocusable(false);
progressBar.setPreferredSize(new Dimension(100,0));
progressBar.setMaximumSize(new Dimension(150,20));
progressBar.setStringPainted(true);
//progressBar.setAlignmentY(Component.CENTER_ALIGNMENT);

progressPanel.add(progressBar);
//globalLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
//globalCoords.setAlignmentY(Component.CENTER_ALIGNMENT);
globalPanel.add(globalLabel);
globalPanel.add(globalCoords);

//cameraLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
//cameraCoords.setAlignmentY(Component.CENTER_ALIGNMENT);
cameraPanel.add(cameraLabel);
cameraPanel.add(cameraCoords);


this.setBorder(new EmptyBorder(5,5,5,5));
this.setPreferredSize(new Dimension(0,30));
this.add(progressPanel);
this.add(Box.createHorizontalGlue());
this.addSeparator();
this.add(globalPanel);
this.addSeparator();
this.add(cameraPanel);
this.setFloatable(false);
现在,当我设置progressPanel.setVisiblefalse时,JLabel的偏移为一个像素。我把对齐注释掉的地方就是我试图让它们对齐的地方,但这仍然不起作用。我做错了什么

public class StatusBar extends JPanel{ //Instead of JToolBar
    private JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL);
    private JPanel progressPanel = new JPanel();
    private JPanel globalPanel = new JPanel();
    private JPanel cameraPanel = new JPanel();
    private JLabel cameraLabel = new JLabel("Camera: ");
    private JLabel cameraCoords = new JLabel();
    private JLabel globalLabel = new JLabel("Global: ");
    private JLabel globalCoords = new JLabel();

    public StatusBar(){
        progressPanel.setLayout(new BoxLayout(progressPanel, BoxLayout.X_AXIS));
        globalPanel.setLayout(new BoxLayout(globalPanel, BoxLayout.X_AXIS));
        cameraPanel.setLayout(new BoxLayout(cameraPanel, BoxLayout.X_AXIS));

        progressBar.setFocusable(false);
        progressBar.setPreferredSize(new Dimension(100,0));
        progressBar.setMaximumSize(new Dimension(150,20));
        progressBar.setStringPainted(true);

        progressPanel.add(tileLoadingLabel);
        progressPanel.add(Box.createHorizontalStrut(5));
        progressPanel.add(progressBar);
        globalPanel.add(globalLabel);
        globalPanel.add(globalCoords);
        globalPanel.add(separator);
        globalCoords.setPreferredSize(new Dimension(150,0));

        //Here is where I made the change(after extending JPanel and not JToolBar
        //I used the BorderLayout instead of the BoxLayout  
        this.setLayout(new BorderLayout());
        this.setBorder(new EmptyBorder(5,5,5,5));
        this.setPreferredSize(new Dimension(0,30));
        this.add(globalPanel,BorderLayout.WEST);
        this.add(Box.createHorizontalGlue());
        this.add(progressPanel,BorderLayout.EAST);
    }
}

我没有想过自己发布答案,我会知道未来,谢谢你的洞察力:

水平还是垂直?如果你有地方可以放置小程序,它会很有用。工具栏是水平的,但是标签会垂直移动。当组件可见时,标签向上移动,然后在不可见时返回到正常位置。我给包含标签的面板背景上色,并确认它实际上是标签在移动,而不是整个面板本身。我没有地方可以托管它。我修复了我的问题,它是由我用于JToolBar的BoxLayout引起的,我还将JToolBar换成了JPanel。我仍然不知道是什么导致了这种行为,但切换到边框布局解决了我的问题。为什么不添加您的发现和更正的代码作为答案?