如何使用 GridBagLayout 强制 JPanel 使用 JFrame 调整大小 [英] How to force JPanels using GridBagLayout to resize with JFrame

查看:35
本文介绍了如何使用 GridBagLayout 强制 JPanel 使用 JFrame 调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的 menuPanel 组件在框架展开时展开.MenuPanel 正在使用 GridBayLayout,我已经修改了面板的填充和权重值,但似乎没有真正导致组件随框架调整大小.

I need the components of my menuPanel to expand when the frame expands. MenuPanel is using GridBayLayout, and I've tinkered with both the fill and weight values for the panel, but nothing really seems to cause the components to resize with the frame.

我怀疑我在显示类中添加 menuPanel 的方式有问题.此外,我还尝试将布局设置为 TanksApplication,但这也不起作用.我想我没有理解一些基本的东西.

I suspect something is wrong with the way I'm adding menuPanel in the display class. Furthermore, I also tried setting a layout to TanksApplication, and that didn't work either. I'm not understanding something fundamental, I think.

主应用

    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);
   }

推荐答案

您的问题是您使用 menuPanel 将 GridBagLayout 添加到 Display JPanel 中,后者使用 JPanel 的默认 FlowLayout.FlowLayout 不会调整添加到其中的组件的大小.解决方案:去掉menuPanel,只使用Display JPanel.给它一个 GridBagLayout 并向其添加组件.

Your problem is that you're adding your GridBagLayout using menuPanel into the Display JPanel, the latter of which uses JPanel's default FlowLayout. FlowLayout will not resize components added to it. The solution: get rid of menuPanel and just use the Display JPanel. Give it a GridBagLayout and add the components to it.

在你的 Display 构造函数中也有

so have within your Display's constructor

setLayout(new GridBagLayout());

删除 JPanel menuPanel = new JPanel(); 和任何你有 menuPane.add(...); 的地方,把它改成简单的 add(...);

delete the JPanel menuPanel = new JPanel(); and anywhere you have menuPane.add(...);, change it to simply 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);
    }

}

请注意,如果这没有帮助,请考虑创建并发布最小示例程序SSCCE.

Note that if this doesn't help, then please consider creating and posting a minimal example program or SSCCE.

这篇关于如何使用 GridBagLayout 强制 JPanel 使用 JFrame 调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆