java - 在java GUI中显示自定义二维数组

标签 java user-interface multidimensional-array

我想显示这个二维按钮数组,其中一行有 3 个按钮,下一行有 5 个按钮,最后一行有 3 个按钮,但它们都出现在一行上 - 我做错了什么?

public class GUICustombuttonstwodimarray extends JFrame {
    static final String title = "Custom Buttons";
    int row, col;
    JPanel panel;

    JButton[][] jbut = { { new JButton("0"), new JButton("1"), new JButton("2") },
        { new JButton("3"), new JButton("4"), new JButton("5"), new JButton("6"), new JButton("7") },
        { new JButton("8"), new JButton("9") }, { new JButton("10") }
    };

    public GUICustombuttonstwodimarray(String title) {
        super(title);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(500, 500);
        this.setVisible(true);

        panel = new JPanel();

        for (row = 0; row < jbut.length; row++) {
            for (col = 0; col < jbut[row].length; col++) {
                panel.add(jbut[row][col]);
            }
        }
        getContentPane().add(panel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        GUICustombuttonstwodimarray g = new GUICustombuttonstwodimarray(title);
    }
}

最佳答案

可能有几种方法可以实现,但最简单的可能就是使用 GridBagLayout,例如

GridBagLayout

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GUICustombuttonstwodimarray extends JPanel {

    static final String TITLE = "Custom Buttons";
    int row, col;

    JButton[][] jbut = {{new JButton("0"), new JButton("1"), new JButton("2")},
    {new JButton("3"), new JButton("4"), new JButton("5"), new JButton("6"), new JButton("7")},
    {new JButton("8"), new JButton("9")}, {new JButton("10")}

    };

    public GUICustombuttonstwodimarray() {

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;

        for (row = 0; row < jbut.length; row++) {
            for (col = 0; col < jbut[row].length; col++) {
                add(jbut[row][col], gbc);

                gbc.gridx++;
                if (gbc.gridy == 0 && gbc.gridx >= 3) {
                    gbc.gridy++;
                    gbc.gridx = 0;
                } else if (gbc.gridy == 1 && gbc.gridx >= 5) {
                    gbc.gridy++;
                    gbc.gridx = 0;
                }

            }

        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame(TITLE);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new GUICustombuttonstwodimarray());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

    }
}

看看How to Use GridLayout了解更多详情

关于java - 在java GUI中显示自定义二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34588431/

相关文章:

arrays - 如何从矩阵中获取 ndgrid?

c++ - 访问动态二维字符数组 C++

java - SpringSocial LinkedIn 集成错误

c# - 如何限制编码的 UI 测试控件搜索

java - JTextArea画Java?

php - 使用 MySQL 中的键将数组的 JSON 数组创建为单个 JSON 对象

java - 编写依赖于 Groovy 的库是个好主意吗?

java - 如何从 android studio 上的 fragment 更改 TextView

java - sysout 中的 concat char 给出奇怪的输出?

Javascript:有没有办法清除用户输入的历史记录?