gpt4 book ai didi

java - GridBagLayout 锚定不起作用,始终显示在 JPanel 的中心

转载 作者:行者123 更新时间:2023-11-30 04:13:18 26 4
gpt4 key购买 nike

我的应用程序处于全屏独占模式,我希望 JPanel mainPanel 位于 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);

}

最佳答案

每当我看到...

it only shows the mainPanel in center of the screen

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

即,

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

关于java - GridBagLayout 锚定不起作用,始终显示在 JPanel 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19074126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com