[英]display custom twodimensional array in java GUI
我想显示此二维按钮数组,其中一行包含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
,例如
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();
}
}
}
看看如何使用GridLayout了解更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.