我试图做一个棋盘游戏,我有玩家1个标签和玩家2标签集的东部和西部。 我有麻烦我中心3×10个按钮。 而有一次让所有30个按钮的任何简单的方法? 下面是我的代码:
JPanel panel = new JPanel();
JButton button1 = new JButton();JButton button2 = new JButton();
JButton button3 = new JButton();JButton button4 = new JButton();
JButton button5 = new JButton();JButton button6 = new JButton();
JButton button7 = new JButton();JButton button8 = new JButton();
JButton button9 = new JButton();JButton button10 = new JButton();
JButton button11 = new JButton();JButton button12 = new JButton();
JButton button13 = new JButton();JButton button14 = new JButton();
JButton button15 = new JButton();JButton button16 = new JButton();
JButton button17 = new JButton();JButton button18 = new JButton();
JButton button19 = new JButton();JButton button20 = new JButton();
JButton button21 = new JButton();JButton button22 = new JButton();
JButton button23 = new JButton();JButton button24 = new JButton();
JButton button25 = new JButton();JButton button26 = new JButton();
JButton button27 = new JButton();JButton button28 = new JButton();
JButton button29 = new JButton();JButton button30 = new JButton();
panel.add(button1);panel.add(button2);panel.add(button3);panel.add(button4);
panel.add(button5);panel.add(button6);panel.add(button7);panel.add(button8);
panel.add(button9);panel.add(button10);panel.add(button11);panel.add(button12);
panel.add(button13);panel.add(button14);panel.add(button15);panel.add(button16);
panel.add(button17);panel.add(button18);panel.add(button19);panel.add(button20);
panel.add(button21);panel.add(button22);panel.add(button23);panel.add(button24);
panel.add(button25);panel.add(button26);panel.add(button27);panel.add(button28);
panel.add(button29);panel.add(button30);
frame.add(panel);
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(new Button("Throw dice"), BorderLayout.SOUTH);
//p.add(new Button("dice Draw"), BorderLayout.SOUTH);
p.add(new Label("Player 1"), BorderLayout.EAST);
p.add(new Label("Player 2"), BorderLayout.WEST);
frame.add(p);
panel.setLayout(new GridLayout(3,10));
panel.setSize(new Dimension(500, 200));
frame.setSize(new Dimension(600, 300));
}
}