1

我的应用程序处于全屏独占模式,我希望 JPanel 主面板位于 LAST_LINE_START 中,并在左右两侧进行一些填充。但是当我运行这个程序时,它只在屏幕中央显示主面板。我也尝试了其他位置,但它们也不起作用。

private void mainWindow()
    {   
        gls =(JPanel) mainUi.getGlassPane();
        gls.setLayout(new GridBagLayout());

        JPanel mainPanel = new JPanel(new GridLayout(4,1));
        nwGame = new JButton("New Game");
        nwGame.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                gls.setVisible(false);
                startGame(0);
                mainUi.repaint();
            }
        });

        ldGame = new JButton("Load Game");
        ldGame.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {

            }
        });

        changeProfile = new JButton("Change Profile");
        changeProfile.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {

            }
        });

        qtGame = new JButton("Quit Game");
        qtGame.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {

            }
        });

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.ipadx = 10;
        gbc.ipady = 10;
        gbc.anchor = GridBagConstraints.LAST_LINE_START;

        mainPanel.add(nwGame);
        mainPanel.add(ldGame);
        mainPanel.add(changeProfile);
        mainPanel.add(qtGame);

        gls.add(mainPanel,gbc);
        gls.setVisible(true);

        mainUi.add(gls);
        if(!mainUi.isVisible())
            mainUi.setVisible(true);

    }       
4

1 回答 1

2

每当我看到...

它只在屏幕中央显示主面板

...与 GridBagLayout 相关联,我总是首先查看用户如何实现 weightx 和 weighty GridBagConstraint 属性,而正如预期的那样,您不是。使用它们。

IE,

   GridBagConstraints gbc = new GridBagConstraints();
   gbc.ipadx = 10;
   gbc.ipady = 10;
   gbc.anchor = GridBagConstraints.LAST_LINE_START;
   gbc.weightx = 1.0;
   gbc.weighty = 1.0;
于 2013-09-29T03:53:22.040 回答