【发布时间】:2016-11-24 21:14:46
【问题描述】:
当框架展开时,我需要展开 menuPanel 的组件。 MenuPanel 正在使用 GridBayLayout,我已经修改了面板的填充和权重值,但似乎没有什么真正导致组件随框架调整大小。
我怀疑我在显示类中添加 menuPanel 的方式有问题。此外,我还尝试为 TanksApplication 设置布局,但也没有用。我认为我没有理解一些基本的东西。
主应用
public class TanksApplication extends JFrame{
public TanksApplication(){
super("Tanks");
}
public static void main (String args[]){
Display display = new Display();
TanksApplication frame = new TanksApplication();
frame.add(display);
frame.setSize(600,425);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
}
显示类
public class Display extends JPanel{
public Display(){
menuPanel = new JPanel(new GridBagLayout());
GridBagConstraints con = new GridBagConstraints();
mainMenuImageP = new JPanel();
mainMenuImageP.setBackground(Color.BLACK);
con.fill = GridBagConstraints.BOTH;
con.gridy = 0;
con.gridx = 0;
con.gridwidth = 2;
con.weightx = 0.5;
con.weighty = 0.5;
con.anchor = GridBagConstraints.CENTER;
con.ipadx = 400;
con.ipady = 300;
con.insets = new Insets(0,20,0,20);
menuPanel.add(mainMenuImageP, con);
newGameB = new JButton("New Game");
con.fill = GridBagConstraints.HORIZONTAL;
con.gridy = 1;
con.gridx = 0;
con.gridwidth = 1;
con.weightx = 0.5;
con.weighty = 0.5;
con.anchor = GridBagConstraints.CENTER;
con.ipadx = 0;
con.ipady = 0;
con.insets = new Insets(10,10,10,10);
menuPanel.add(newGameB, con);
loadGameB = new JButton("Load Game");
con.fill = GridBagConstraints.HORIZONTAL;
con.gridy = 1;
con.gridx = 1;
con.gridwidth = 1;
con.weightx = 0.5;
con.weighty = 0.5;
con.anchor = GridBagConstraints.CENTER;
con.ipadx = 0;
con.ipady = 0;
con.insets = new Insets(10,10,10,10);
menuPanel.add(loadGameB, con);
add(menuPanel);
}
【问题讨论】: