代码之家  ›  专栏  ›  技术社区  ›  Asaf

调整JPanel的大小,以获取在最大化屏幕时使用GridBagConstraints的GridBagLayout

  •  1
  • Asaf  · 技术社区  · 7 年前

    谢谢

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Insets;
    
    import javax.swing.JTextField;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import javax.swing.WindowConstants;
    
    public class Calculator extends JFrame
    {
    
        protected JTextField display;
    
        protected JButton b1 = new JButton("1");
        protected JButton b2 = new JButton("2");
        protected JButton b3 = new JButton("3");
        protected JButton b4 = new JButton("4");
        protected JButton b5 = new JButton("5");
        protected JButton b6 = new JButton("6");
        protected JButton b7 = new JButton("7");
        protected JButton b8 = new JButton("8");
        protected JButton b9 = new JButton("9");
        protected JButton b0 = new JButton("0");
    
        protected JButton addition = new JButton("+");
        protected JButton subtraction = new JButton("-");
        protected JButton multiplication = new JButton("*");
        protected JButton division = new JButton("/");
        protected JButton result = new JButton("=");
        protected JButton allClear = new JButton("C");
        protected JButton decimalPoint = new JButton(".");
    
        public Calculator()
        {
            super("Calculator");
            Handler handler = new Handler();
    
            allClear.addActionListener(handler);
            addition.addActionListener(handler);
            subtraction.addActionListener(handler);
            multiplication.addActionListener(handler);
            division.addActionListener(handler);
            result.addActionListener(handler);
            decimalPoint.addActionListener(handler);
    
            b0.addActionListener(handler);
            b1.addActionListener(handler);
            b2.addActionListener(handler);
            b3.addActionListener(handler);
            b4.addActionListener(handler);
            b5.addActionListener(handler);
            b6.addActionListener(handler);
            b7.addActionListener(handler);
            b8.addActionListener(handler);
            b9.addActionListener(handler);
    
            addition.setToolTipText("Addition");
            subtraction.setToolTipText("Subtraction");
            multiplication.setToolTipText("Multiplication");
            division.setToolTipText("Division");
            result.setToolTipText("Result");
            allClear.setToolTipText("All Clear");
            decimalPoint.setToolTipText("Decimal point");
    
            display = new JTextField(10);
            display.setBorder(BorderFactory.createLineBorder(Color.CYAN));
    
            // Set a panel's layout manager using the JPanel constructor
    
            JPanel mainPanel = new JPanel(new GridBagLayout());
    
            GridBagConstraints c = new GridBagConstraints();
            c.gridy = 0;
            c.gridwidth = 3;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(display, c);
    
            c = new GridBagConstraints();
            c.gridy = 0;
            c.gridwidth = 1;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(allClear, c);
    
            c = new GridBagConstraints();
            c.gridy = 1;
            c.gridwidth = 1;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b1, c);
    
            c = new GridBagConstraints();
            c.gridy = 1;
            c.gridwidth = 1;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b2, c);
    
            c = new GridBagConstraints();
            c.gridy = 1;
            c.gridwidth = 1;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b3, c);
    
            c = new GridBagConstraints();
            c.gridy = 1;
            c.gridwidth = 1;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(division, c);
    
            c = new GridBagConstraints();
            c.gridy = 2;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b4, c);
    
            c = new GridBagConstraints();
            c.gridy = 2;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b5, c);
    
            c = new GridBagConstraints();
            c.gridy = 2;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b6, c);
    
            c = new GridBagConstraints();
            c.gridy = 2;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(multiplication, c);
    
            c = new GridBagConstraints();
            c.gridy = 3;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b7, c);
    
            c = new GridBagConstraints();
            c.gridy = 3;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b8, c);
    
            c = new GridBagConstraints();
            c.gridy = 3;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b9, c);
    
            c = new GridBagConstraints();
            c.gridy = 3;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(subtraction, c);
    
            c = new GridBagConstraints();
            c.gridy = 4;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(b0, c);
    
            c = new GridBagConstraints();
            c.gridy = 4;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(decimalPoint, c);
    
            c = new GridBagConstraints();
            c.gridy = 4;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(result, c);
    
            c = new GridBagConstraints();
            c.gridy = 4;
            c.insets = new Insets(5, 5, 5, 5);
            mainPanel.add(addition, c);
    
            add(mainPanel);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            pack();
        }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   user85421    7 年前

    你需要设置 weightx weighty 的约束。这些值之间的关系控制“未使用”空间沿单元格的分布方式。请参阅 GridBagConstraints documentation .

    你可以设置 沉重的

    这个 fill 字段必须设置为“两者”,以便每个按钮占用其整个单元格。

    迷你示例,减少可读性:

    public class MiniCalculator extends JFrame {
    
        public static void main(String[] args) {
            new MiniCalculator().setVisible(true);
        }
    
        private JTextField display = new JTextField(10);
    
        private JButton clear = new JButton("C");
        private JButton b1 = new JButton("1");
        private JButton b2 = new JButton("2");
        private JButton b3 = new JButton("3");
    
        public MiniCalculator() {
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            setLayout(new GridBagLayout());
    
            GridBagConstraints constraint = new GridBagConstraints();
            constraint.insets = new Insets(5, 5, 5, 5);
            constraint.weighty = 1.0;
            constraint.weightx = 1.0;
    
            constraint.gridy = 0;
            constraint.gridwidth = 2;
            constraint.fill = GridBagConstraints.HORIZONTAL;
            add(display, constraint);
    
            constraint.gridwidth = 1;
            constraint.fill = GridBagConstraints.BOTH;
            add(clear, constraint);
    
            constraint.gridy = 1;
            add(b1, constraint);
            add(b2, constraint);
            add(b3, constraint);
    
            pack();
            setLocationRelativeTo(null);
        }
    }
    

    注意:不需要创建所有约束,只需创建一个约束并在添加组件之间进行更改。添加组件时克隆约束。

        2
  •  0
  •   Nandha    7 年前

    尝试更改 c.fill = GridBagConstraints.HORIZONTAL; c.fill = GridBagConstraints.BOTH;