gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:03:50 24 4
gpt4 key购买 nike

我想显示这个二维按钮数组,其中一行有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com