Changing size of Java button GridBayLayout

前端 未结 3 1379
庸人自扰
庸人自扰 2020-12-20 07:24

Hello I have a big problem. I would like to make \"=\" button 2*height and the \"0\" button 2*width(OTHER buttons should be just normal size), that\'s all I tried many combi

相关标签:
3条回答
  • 2020-12-20 07:46

    I can't see any issue with that, please read

    • How to Use GridBagLayout

    • GridBagConstraints and anchor too

    then

    enter image description here

    from code

    import java.awt.*;
    import javax.swing.*;
    
    public class GridBagButtons {
    
        private static final Insets insets = new Insets(0, 0, 0, 0);
    
        public static void main(final String args[]) {
            final JFrame frame = new JFrame("GridBagLayout");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new GridBagLayout());
            JButton button;
            // Row One - Three Buttons
            button = new JButton("One");
            addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
            button = new JButton("Two");
            addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
            button = new JButton("Three");
            addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
            // Row Two - Two Buttons
            button = new JButton("Four");
            addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
            button = new JButton("Five");
            addComponent(frame, button, 2, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
            // Row Three - Two Buttons
            button = new JButton("Six");
            addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
            button = new JButton("Seven");
            addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
            frame.setSize(500, 200);
            frame.setVisible(true);
        }
    
        private static void addComponent(Container container, Component component, int gridx, int gridy,
                int gridwidth, int gridheight, int anchor, int fill) {
            GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
                    anchor, fill, insets, 0, 0);
            container.add(component, gbc);
        }
    
        private GridBagButtons() {
        }
    }
    
    • I'd be use NestedLayout rather than bothering with GrigBagLayout, SpringLayout or MigLayout
    0 讨论(0)
  • 2020-12-20 07:51

    Try your hands on this code example and ask any questions that may arise :

    import java.awt.*;
    import javax.swing.*;
    
    public class GridBagTest
    {
        private String[] buttonText = { "C", ".", "/", "*", "7", "8", "9", "-", "4", "5",
                "6", "+", "1", "2", "3", "=", "0", "+/-" };
        private JButton[] button = new JButton[18];
        private int counter = 0;
    
        private void createAndDisplayGUI()
        {
            JFrame frame = new JFrame("GridBagLayout Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout());
    
            JPanel northPanel = new JPanel();
            northPanel.setLayout(new BorderLayout(2, 2));
            JTextField tfield = new JTextField();
            northPanel.add(tfield, BorderLayout.CENTER);
    
            JPanel centerPanel = new JPanel();
            centerPanel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.PAGE_START;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.insets = new Insets(2, 2, 2, 2);
            for (int i = 0; i < button.length; i++)
            {
                System.out.println("Button Text : " + buttonText[i]);
                button[i] = new JButton(buttonText[i]);
            }
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                        gbc.gridx = j;
                        gbc.gridy = i;
                        centerPanel.add(button[counter++], gbc);
                }
            }
            gbc.gridx = 0;
            gbc.gridy = 3;
            centerPanel.add(button[counter++], gbc);
            gbc.gridx = 1;
            gbc.gridy = 3;
            centerPanel.add(button[counter++], gbc);
            gbc.gridx = 2;
            gbc.gridy = 3;
            centerPanel.add(button[counter++], gbc);
            gbc.gridx = 3;
            gbc.gridy = 3;  
            gbc.gridwidth = 1;
            gbc.gridheight = 2;
            centerPanel.add(button[counter++], gbc);
            int count = counter;
            System.out.println(button[--count].getText());
            gbc.gridx = 0;
            gbc.gridy = 4;
            gbc.gridheight = 1;
            gbc.gridwidth = 2;
            centerPanel.add(button[counter++], gbc);
            gbc.gridwidth = 1;
            gbc.gridx = 2;
            gbc.gridy = 4;
            centerPanel.add(button[counter++], gbc);
    
            contentPane.add(northPanel, BorderLayout.PAGE_START);
            contentPane.add(centerPanel, BorderLayout.CENTER);
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new GridBagTest().createAndDisplayGUI();
                }
            });
        }
    }
    

    Here is the output :

    CALCULATOR

    0 讨论(0)
  • 2020-12-20 08:10

    The problem is that you don't set weightx and weighty on the GridBagConstraints. Setting them to 1.0 will allocate the (horizontal/vertical) extra space to each component equally.

    0 讨论(0)
提交回复
热议问题