0
我想與下一行中的一行,5 3個的按鈕和在最後一行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);
}
}
退房https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html,尤其是GridBagLayout的可能是有趣的。 – LordAnomander
[如何使用表(http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) – MadProgrammer
這tutorialspoint文章舉了一個例子,這是非常相似,你想達到什麼樣的: http://www.tutorialspoint.com/awt/awt_gridlayout.htm –