我需要menuPanel的组件在框架展开时展开。 MenuPanel正在使用GridBayLayout,我已经对面板的填充和重量值进行了修改,但似乎没有任何东西真正导致组件使用框架调整大小。
我怀疑我在display class中添加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);
}
答案 0 :(得分:1)
您的问题是您使用menuPanel将GridBagLayout添加到Display JPanel中,后者使用JPanel的默认FlowLayout。 FlowLayout不会调整添加到其中的组件的大小。解决方案:获取menuPanel的 rid ,然后使用Display JPanel。给它一个GridBagLayout并向其添加组件。
在你的展示构建器
中也是如此setLayout(new GridBagLayout());
删除JPanel menuPanel = new JPanel();
以及menuPane.add(...);
的任何位置,将其更改为add(...)
;
例如,
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);
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);
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(loadGameB, con); // !!
// !! add(menuPanel);
}
}
请注意,如果这没有帮助,请考虑创建并发布minimal example program或SSCCE。