带有Border Layout的JPanel内的组件没有占用完整的可用空间

时间:2014-09-19 08:00:06

标签: java swing border-layout

我使用BorderLayout.CENTER在另一个panel1中添加了panel2。但是panel2并没有占用完整的可用空间。这就是我到目前为止所做的事情

       //constructor
       SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try{
                new JFXPanel();

                add(getJPanel(), BorderLayout.CENTER);
                add(getJBottomPanel(),BorderLayout.SOUTH);
                getJBottomPanel().add(getTestPanel(),BorderLayout.CENTER);

                Platform.runLater((new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        try {
                            initFX(fxPanel);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }   
                    }
                })) ;

            }
            catch(Exception e){
                System.out.println("Error in swing utilities thread :" + e.getMessage());
            }

        }
    });

getJPanel方法

    private JPanel getJPanel(){
    if(panel == null){
    panel = new JPanel();
    panel.setSize(applicationWidth_600,upperPanelHeight_535);
    panel.setBackground(Color.gray);
    }
    return panel;
}

getJBottomPanel方法

    private JPanel getJBottomPanel(){


    if(bPanel == null){
        bPanel = new JPanel();
        bPanel.setSize(200,bottomPanelHeight_65);
        bPanel.setPreferredSize(new Dimension(300, 65));
        bPanel.setBackground(Color.pink);
    }
    return bPanel;
}

getTestPanel方法

    private JPanel getTestPanel(){
    if(testPanel == null){
        testPanel = new JPanel();
        testPanel.setBackground(Color.black);
    }

    return testPanel;
}

这是输出: - https://imagizer.imageshack.us/v2/195x383q90/908/wndsCC.png

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。我犯了一个非常小的错误,它证明了一个简单的错误也会对你的应用程序造成很大的影响。我忘了将bPanel设置为Borderlayout。以下是方法getJBottomPanel

的更新代码
private JPanel getJBottomPanel()
{
    if(bPanel == null)
    {
      bPanel = new JPanel();
      bPanel.setPreferredSize(new Dimension(300, 65));
      bPanel.setBackground(Color.pink);
      bPanel.setLayout(new BorderLayout());
    }
     return bPanel;
}