Java – why didn’t this gridbaglayout appear as planned?

I try to achieve the final result required by setting an arbitrary width in gridbaglayout

For reference, here is:

This is the current result:

The button number and row are displayed as 1,1, followed by the number of columns declared for this cell (2)

As you can see, it starts with buttons 1,1 (3), below which 1,2 (4) have the same width and declare a different number of columns

Can anyone determine how to correct the code?

Current code:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class KeyBoardLayout {

    private JComponent ui = null;

    KeyBoardLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new EmptyBorder(4,4,4));

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        ui.add(new JButton("1,1 (3)"),gbc);

        gbc.gridx = 3;
        gbc.gridwidth = 2;
        ui.add(new JButton("2,1 (2)"),gbc);

        gbc.gridx = 5;
        ui.add(new JButton("3,gbc);

        gbc.gridx = 7;
        ui.add(new JButton("4,gbc);

        gbc.gridx = 9;
        ui.add(new JButton("5,gbc);

        gbc.gridx = 11;
        ui.add(new JButton("6,gbc);

        gbc.gridx = 13;
        ui.add(new JButton("7,gbc);

        gbc.gridx = 15;
        gbc.gridwidth = 3;
        ui.add(new JButton("8,gbc);

        gbc.gridx = 18;
        gbc.gridwidth = 4;
        ui.add(new JButton("9,1 (4)"),gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        ui.add(new JButton("1,2 (4)"),gbc);

        gbc.gridx = 4;
        gbc.gridwidth = 2;
        ui.add(new JButton("2,2 (2)"),gbc);

        gbc.gridx = 6;
        ui.add(new JButton("3,gbc);

        gbc.gridx = 8;
        ui.add(new JButton("4,gbc);

        gbc.gridx = 10;
        ui.add(new JButton("5,gbc);

        gbc.gridx = 12;
        ui.add(new JButton("6,gbc);

        gbc.gridx = 14;
        ui.add(new JButton("7,gbc);

        gbc.gridx = 16;
        ui.add(new JButton("8,gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 5;
        ui.add(new JButton("1,3 (5)"),gbc);

        gbc.gridx = 5;
        gbc.gridwidth = 2;
        ui.add(new JButton("2,3 (2)"),gbc);

        gbc.gridx = 7;
        ui.add(new JButton("3,gbc);

        gbc.gridx = 9;
        ui.add(new JButton("4,gbc);

        gbc.gridx = 11;
        ui.add(new JButton("5,gbc);

        gbc.gridx = 13;
        ui.add(new JButton("6,gbc);

        gbc.gridx = 15;
        ui.add(new JButton("7,gbc);

        gbc.gridx = 17;
        ui.add(new JButton("8,gbc);

        gbc.gridx = 19;
        gbc.gridwidth = 3;
        ui.add(new JButton("9,3 (3)"),gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 3;
        ui.add(new JButton("1,4 (3)"),gbc);

        gbc.gridx = 3;
        ui.add(new JButton("2,gbc);

        gbc.gridx = 6;
        gbc.gridwidth = 10;
        ui.add(new JButton("3,4 (10)"),gbc);

        gbc.gridx = 16;
        gbc.gridwidth = 3;
        ui.add(new JButton("4,gbc);

        gbc.gridx = 19;
        ui.add(new JButton("5,gbc);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                KeyBoardLayout o = new KeyBoardLayout();

                JFrame f = new JFrame("Keyboard Layout");
                f.setDefaultCloSEOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokelater(r);
    }
}

Solution

I think I got its job I created a row containing 22 components, each of which occupies a column:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class KeyBoardLayout {

    private JComponent ui = null;

    KeyBoardLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new EmptyBorder(4,4));

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
//        gbc.gridy = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        ui.add(new JButton("1,gbc);

        gbc.gridx = 0;
//        gbc.gridy = 1;
        gbc.gridy = 2;
        ui.add(new JButton("1,gbc);

        gbc.gridx = 0;
//        gbc.gridy = 2;
        gbc.gridy = 3;
        gbc.gridwidth = 5;
        ui.add(new JButton("1,gbc);

        gbc.gridx = 0;
//        gbc.gridy = 3;
        gbc.gridy = 4;
        gbc.gridwidth = 3;
        ui.add(new JButton("1,gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 1;

        for (int i = 0; i < 22; i++)
        {
            gbc.gridx = i;
            gbc.gridy = 4;
            ui.add(new JButton());
//          ui.add(@R_838_2419@.createHorizontalStrut(15));
        }


    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                KeyBoardLayout o = new KeyBoardLayout();

                JFrame f = new JFrame("Keyboard Layout");
                f.setDefaultCloSEOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokelater(r);
    }
}

Of course, you don't want the button to display, so you can also use invisible components

Now, I have a problem of my own Why does the button appear on the first line even if I set gridy = 4?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>