简体   繁体   English

Java GridBagLayout调整大小和固定大小

[英]Java GridBagLayout resizing and fixed size

This is what i'm trying to achieve 这就是我想要实现的

------------------------| 150px |
|                       | width |
|                       |   p2  |
|                       |       |
|      Fill frame       |       |
|      p1               |       |
|                       |       |
|                       |       |
------------------------|       |
150px height            |       |
|       p3              |       |
---------------------------------

The 'Fill frame' has to scale dynamic, but the other two squares have a fixed width / height. “填充框”必须动态缩放,但其他两个正方形具有固定的宽度/高度。

My code does produce this layout, but p1 is not scaling to fill up the entire frame. 我的代码确实产生了这种布局,但是p1无法缩放以填满整个框架。 Could someone please tell me how to make p1 dynamic? 有人可以告诉我如何使p1动态吗?

Thanks in advance. 提前致谢。

This is what i have so far: 这是我到目前为止所拥有的:

    setTitle(Rolit.TITLE);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

//P1    FILL P1 WITH 64 BUTTONS
    p1 = new JPanel();
    p1.setLayout(new GridLayout(8, 8));

    p1.setMinimumSize(new Dimension(600, 600));
    p1.setMaximumSize(new Dimension(600, 600));
    p1.setPreferredSize(new Dimension(600, 600));

    for(int y = 0; y < 8; y++){
        for(int x = 0; x < 8; x++){
            JButton btn = new JButton(x + " , " + y);
            p1.add(btn);
        }
    }

//P2    FILL P2 WITH 4 BUTTONS
    p2 = new JPanel();
    p2.setLayout(new GridLayout(4, 1));
    p2.setMinimumSize(new Dimension(150, 100));
    p2.setMaximumSize(new Dimension(150, 100));
    p2.setPreferredSize(new Dimension(150, 100));

    for(int i = 0; i < 4; i++){
        p2.add(new JButton("Button"));
    }

//P3
    p3 = new JPanel();
    p3.setMinimumSize(new Dimension(100, 150));
    p3.setMaximumSize(new Dimension(100, 150));
    p3.setPreferredSize(new Dimension(100, 150));
    p3.setBackground(Color.BLUE);


//ADD ALL TO mainPanel

    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH; 
    mainPanel.add(p1, c);        

    c.gridx = 1;
    c.gridheight = 2;  
    c.fill = GridBagConstraints.VERTICAL;
    mainPanel.add(p2, c);

    c.gridx = 0;
    c.gridy = 1;
    c.gridheight = 1;        
    c.gridwidth = 2;
    c.fill = GridBagConstraints.HORIZONTAL;
    mainPanel.add(p3, c);

    setContentPane(mainPanel);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);

I would consider using nested JPanels that use BorderLayouts instead. 我会考虑使用使用BorderLayouts的嵌套JPanels。 For instance: 例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class EmielGui extends JPanel {
   protected static final int DELTA_DIMEN = 1;
   protected static final int MAX_DIMEN = 600;
   private static final int DELAY = 20;
   private JPanel rightPanel = new JPanel();
   private JPanel bottomPanel = new JPanel();
   private JPanel centerPanel = new JPanel();
   private int dimen = 100;

   public EmielGui() {
      JPanel tempCenterPanel = new JPanel(new BorderLayout());
      tempCenterPanel.add(centerPanel, BorderLayout.CENTER);
      tempCenterPanel.add(bottomPanel, BorderLayout.SOUTH);

      setLayout(new BorderLayout());
      add(rightPanel, BorderLayout.EAST);
      add(tempCenterPanel, BorderLayout.CENTER);

      rightPanel.setMinimumSize(new Dimension(150, 0));
      bottomPanel.setMinimumSize(new Dimension(0, 150));

      rightPanel.setPreferredSize(rightPanel.getMinimumSize());
      bottomPanel.setPreferredSize(bottomPanel.getMinimumSize());

      rightPanel.setBorder(BorderFactory.createLineBorder(Color.red));
      bottomPanel.setBorder(BorderFactory.createLineBorder(Color.blue));
      centerPanel.setBorder(BorderFactory.createLineBorder(Color.green));

      new Timer(DELAY, new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            dimen += DELTA_DIMEN;
            if (dimen < MAX_DIMEN) {
               centerPanel.setPreferredSize(new Dimension(dimen, dimen));
               SwingUtilities.getWindowAncestor(EmielGui.this).pack();

            } else {
               ((Timer) e.getSource()).stop();
            }
         }
      }).start();
   }

   private static void createAndShowGui() {
      EmielGui mainPanel = new EmielGui();

      JFrame frame = new JFrame("EmielGui");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

You need to supply weightx and weighty values with the GridBagConstraints which affect how the individual components react to changes in the size the parent container and how much "weight" the take up within the given cell 您需要提供GridBagConstraints weightxweighty值,这些值会影响各个组件对父容器大小变化的反应以及给定单元格中所占的“重量”

Take a look at How to use GridBagLayout for more details 看看如何使用GridBagLayout了解更多详细信息

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestGridBagLayout {

    public static void main(String[] args) {
        new TestGridBagLayout();
    }

    private JPanel mainPanel;
    private JPanel p1;
    private JPanel p2;
    private JPanel p3;

    public TestGridBagLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                mainPanel = new JPanel();
                mainPanel.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();

                //P1    FILL P1 WITH 64 BUTTONS
                p1 = new JPanel();
                p1.setLayout(new GridLayout(8, 8));

                p1.setMinimumSize(new Dimension(600, 600));
                p1.setMaximumSize(new Dimension(600, 600));
                p1.setPreferredSize(new Dimension(600, 600));

                for (int y = 0; y < 8; y++) {
                    for (int x = 0; x < 8; x++) {
                        JButton btn = new JButton(x + " , " + y);
                        p1.add(btn);
                    }
                }

                //P2    FILL P2 WITH 4 BUTTONS
                p2 = new JPanel();
                p2.setLayout(new GridLayout(4, 1));
                p2.setMinimumSize(new Dimension(150, 100));
                p2.setMaximumSize(new Dimension(150, 100));
                p2.setPreferredSize(new Dimension(150, 100));

                for (int i = 0; i < 4; i++) {
                    p2.add(new JButton("Button"));
                }

                //P3
                p3 = new JPanel();
                p3.setMinimumSize(new Dimension(100, 150));
                p3.setMaximumSize(new Dimension(100, 150));
                p3.setPreferredSize(new Dimension(100, 150));
                p3.setBackground(Color.BLUE);

                //ADD ALL TO mainPanel
                c.gridx = 0;
                c.gridy = 0;
                c.fill = GridBagConstraints.BOTH;
                c.weightx = 1;
                c.weighty = 1;
                mainPanel.add(p1, c);

                c.gridx = 1;
                c.gridheight = 2;
                c.weighty = 1;
                c.weightx = 0;
                c.fill = GridBagConstraints.VERTICAL;
                mainPanel.add(p2, c);

                c.gridx = 0;
                c.gridy = 1;
                c.gridheight = 1;
                c.gridwidth = 2;
                c.fill = GridBagConstraints.HORIZONTAL;
                c.weighty = 0;
                c.weightx = 1;
                mainPanel.add(p3, c);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(mainPanel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM