Почему компонент Java GUI сдвигается, когда другой не виден? - PullRequest
0 голосов
/ 29 июля 2010

Вот проблема, с которой я столкнулся. У меня есть JToolBar, который содержит 3 JPanel, и каждая панель содержит несколько различных компонентов. Панель с моим 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.setVisible(false) смещение JLabel на пиксель. То, где я прокомментировал выравнивание, - то, где я пытался заставить их выравниваться, но это все еще не работало. Что я тут не так делаю?

1 Ответ

0 голосов
/ 30 июля 2010
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);
    }
}

Я сам не думал о публикации ответа, буду знать на будущее, спасибо за понимание :)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...