1
我的應用程序是全屏獨佔模式,我希望JPanel mainPanel在LAST_LINE_START中有一些左右填充。但是當我運行這個程序時,它只顯示屏幕中央的mainPanel。我也試過其他職位,但他們也沒有工作。GridBagLayout錨定不工作,總是顯示在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'的'的默認行爲。它會將所有組件放在容器的中心內...... – MadProgrammer