0
Hello, I’m trying to build a simple interface to implement a minefield. The end result should be something like a grid of buttons, very simple. The problem I’m having is that when I create a class instance, only one button appears as in the following image:. follows the code I’m writing.
package ms;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FrameBasico extends JFrame {
JPanel painelMinas = new JPanel();
int x = 9; int y = 9;
MyJToggleButton[][] botoes = new MyJToggleButton[x][y];
FrameBasico(){
this.setExtendedState(MAXIMIZED_BOTH);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
painelMinas.setSize(this.getSize());
painelMinas.setBackground(Color.red);
colocarBotoes();
this.add(painelMinas);
}
public void colocarBotoes(){
for(int ipslon = 0; ipslon < y; ipslon++){
for(int xis = 0; xis < x; xis++){
botoes[xis][ipslon] = new MyJToggleButton();
painelMinas.add(botoes[xis][ipslon]);
botoes[xis][ipslon].setVisible(true);
}
}
this.setVisible(true);
}
public static void main(String args[]){
FrameBasico f = new FrameBasico();
}
}
Julian, your code is not executable, please provide a code that is [mcve] so that we can test the problem.
– user28595
I imagine your problem is doing what I didn’t do at this link, if it is, confirm in the comments.
– user28595
If it is similar, gridLayout is used in the link code. I wanted to try to avoid the use of layouts, so I tried to position the buttons with the setLocation()... As for the minimum example, Oce referred to having a main class?
– Julian Villega
You should not use absolute values, layouts exist precisely to facilitate this kind of problem that Oce is having. I recommend you see the other question, from a look at the layout la suggested, it meets and does what Voce intends.
– user28595
really, seeing the other question I realize that solution is better. Can you recommend me al tuturial on layouts ? I still find it very difficult to understand how they work and how to use them
– Julian Villega
You have their documentation: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
– user28595