Java GridBagLayout锚定不起作用,始终显示在JPanel的中间
我的应用程序处于全屏独占模式,我希望JPanel主面板处于最后一行,并在左右两侧进行一些填充。但当我运行这个程序时,它只显示屏幕中央的主面板。我也尝试过其他职位,但也不起作用Java GridBagLayout锚定不起作用,始终显示在JPanel的中间,java,swing,Java,Swing,我的应用程序处于全屏独占模式,我希望JPanel主面板处于最后一行,并在左右两侧进行一些填充。但当我运行这个程序时,它只显示屏幕中央的主面板。我也尝试过其他职位,但也不起作用 private void mainWindow() { gls =(JPanel) mainUi.getGlassPane(); gls.setLayout(new GridBagLayout()); JPanel mainPanel = new JPanel
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);
}
每当我看到
它仅显示屏幕中央的主面板
…与GridBagLayout关联,我总是首先查看用户是如何实现weightx和WeightyGridBagConstraint属性的,正如预期的那样,您没有。使用它们
i、 e
这是
GridBagLayout
的默认行为。它将希望将所有组件放置在容器的中心。。。
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx = 10;
gbc.ipady = 10;
gbc.anchor = GridBagConstraints.LAST_LINE_START;
gbc.weightx = 1.0;
gbc.weighty = 1.0;