Pessoal, tenho uma matriz 3x3, int matriz [] [] = new int[3] [3];
E preciso add ela no meu JPanel dessa forma:
5 8 11
4 7 10
3 6 9
Alguém poderia me ajudar?
Pessoal, tenho uma matriz 3x3, int matriz [] [] = new int[3] [3];
E preciso add ela no meu JPanel dessa forma:
5 8 11
4 7 10
3 6 9
Alguém poderia me ajudar?
eu resolveria + ou - assim:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Matriz extends JFrame{
public JPanel panel;
public JLabel[][] matriz;
public Matriz(){
this.setSize(300,200);
componentes();
this.setVisible(true);
}
public void componentes(){
Random r = new Random();
this.setLayout(new BorderLayout());
matriz = new JLabel[3][3];
panel = new JPanel();
panel.setLayout(new GridLayout(3,3));
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
matriz[i][j] = new JLabel(String.valueOf(r.nextInt(10)));
panel.add(matriz[i][j]);
}
}
this.add(panel, BorderLayout.CENTER);
}
public static void main(String[] args){
Matriz m = new Matriz();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Essa é a idéia mesmo.
Mas os números estão mto distantes Marlos.
Não tem como ficarem mais próximos?
De repente definir o tamanho do Panel
1º de tudo: meu nome é MARLON
rsrsrs
e 2º é que to usando borderlayout então as dimensões do componente se ajustam com o tamanho da janela
mas pra deixar a fonte cetralizada faz assim:
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
JLabel l = new JLabel(String.valueOf(r.nextInt(10)));
l.setHorizontalAlignment(JLabel.CENTER);
matriz[i][j] = l;
panel.add(matriz[i][j]);
}
}
Legal, melhorou.
Obrigado Marlon =))
Abraço