I am coding a program wich should contain multiple JButtons set next to each other. At first I wanted a 4 by 6 Grid. I have 3 classes: a Main clas containing the main method and a constructor call of the Gui class constructor. The Gui class should include all JObjects (JButtons) + the JFrame setup. Then there is a third ButtonPlacement class wich includes the setBounds method calls. As I ran the Code in Eclipse ervery Button in my Button Array was placed at the right position except the last one: btn[3][5]. It is as big as the whole JFrame.
class Main:
package pack1;
public class Main {
public static void main(String[] args) {
new Gui();
}
}
class Gui:
package pack1;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import pack1.ActionHandler;
import pack1.ButtonPlacement;
public class Gui {
static JFrame jf;
static JButton btn[][] = new JButton[4][6];
JButton btnReset;
public Gui() {
jf = new JFrame();
jf.setSize(500, 600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
jf.setLocationRelativeTo(null);
for(int i = 0; i<4;++i) {
for(int j = 0; j<6; ++j) {
btn[i][j] = new JButton("btn" + i + j);
btn[i][j].setVisible(true);
btn[i][j].addActionListener(new ActionHandler());
btn[i][j].setFocusPainted(false);
btn[i][j].setContentAreaFilled(true);
btn[i][j].setBorder(null);
btn[i][j].setFont(new Font("Century Gothic", Font.PLAIN, 20));
jf.add(btn[i][j]);
}
}
ButtonPlacement.place();
jf.setVisible(true);
}
}
class ButtonPlacement:
package pack1;
import pack1.Gui;
public class ButtonPlacement {
public static void place() {
Gui.btn[0][0].setBounds(0, 140, 100, 60);
Gui.btn[1][0].setBounds(100, 140, 100, 60);
Gui.btn[2][0].setBounds(200, 140, 100, 60);
Gui.btn[3][0].setBounds(300, 140, 100, 60);
Gui.btn[0][1].setBounds(0, 200, 100, 60);
Gui.btn[1][1].setBounds(100, 200, 100, 60);
Gui.btn[2][1].setBounds(200, 200, 100, 60);
Gui.btn[3][1].setBounds(300, 200, 100, 60);
Gui.btn[0][2].setBounds(0, 260, 100, 60);
Gui.btn[1][2].setBounds(100, 260, 100, 60);
Gui.btn[2][2].setBounds(200, 260, 100, 60);
Gui.btn[3][2].setBounds(300, 260, 100, 60);
Gui.btn[0][3].setBounds(0, 320, 100, 60);
Gui.btn[1][3].setBounds(100, 320, 100, 60);
Gui.btn[2][3].setBounds(200, 320, 100, 60);
Gui.btn[3][3].setBounds(300, 320, 100, 60);
Gui.btn[0][4].setBounds(0, 380, 100, 60);
Gui.btn[1][4].setBounds(100, 380, 100, 60);
Gui.btn[2][4].setBounds(200, 380, 100, 60);
Gui.btn[3][4].setBounds(300, 380, 100, 60);
Gui.btn[0][5].setBounds(0, 440, 100, 60);
Gui.btn[1][5].setBounds(100, 440, 100, 60);
Gui.btn[2][5].setBounds(200, 440, 100, 60);
Gui.btn[3][5].setBounds(300, 440, 100, 60);
}
}
I expect the Button btn[3][5] at its position 300, 440 with its size 100, 60, but the button is as big as the JFrame.