我正在尝试创建一个带有一堆 Jbuttons 的 JFrame,。中间有 40 个,每行 4 个,而 6 个 Jbuttons 在左侧向下的垂直线上。问题是,侧面按钮下方的第五个 Jbutton 出现在所有其他按钮的后面,并占据了整个窗口。我不知道这是从哪里来的,因为我从未将 JButton 的尺寸设置为那么大,或者适合屏幕。
import javax.swing.*;
public class Buttons {
static JButton[][] middle = new JButton [10][4]; //middle buttons
static JButton[] colours = new JButton[6]; //colour buttons
public static void main (String arg[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,700);
int xcor = 100; //x coordinate
int ycor = 5; //y coordinate
//middle buttons
for (int y = 0; y<10; y++) {
for (int x = 0; x<4; x++) {
middle[y][x] = new JButton();
middle[y][x].setBounds(xcor, ycor, 100, 60);
middle[y][x].setVisible(true);
frame.getContentPane().add(middle[y][x]);
xcor+=100;
}
xcor=100;
ycor+=60;
}
//colour buttons
ycor=65;
for (int y = 0; y<5; y++){
colours[y] = new JButton();
colours[y].setBounds(5, ycor, 90, 60);
switch (y) {
case 0: colours[y].setText("Red");
break;
case 1: colours[y].setText("Blue");
break;
case 2: colours[y].setText("Green");
break;
case 3: colours[y].setText("Yellow");
break;
case 4: colours[y].setText("Purple");
break;
case 5: colours[y].setText("Black");
break;
}
colours[y].setVisible(true);
frame.getContentPane().add(colours[y]);
ycor += 60;
}
frame.setVisible(true);
}