Basicaly i got a JPanel with a GridLayout 5x5 and each "square" as a JLabel with a number and a diferent background color, depending on the number that its there.
I need to update the panel everytime i click one of the buttons (except menu one) because it will change the array of array of ints that is where i get the numbers from to put in the labels.
My Question is how do i update every JLabel (including color) JFrame Layout
public static JPanel painel(){
novo_jogo();
// Painel Grelha
JPanel grelha = new JPanel();
GridLayout grid_grelha = new GridLayout(linhas, colunas, 3, 3);
grelha.setLayout(grid_grelha);
grelha.setOpaque(true);
grelha.setSize(janela_x - 140, janela_y-140);
grelha.setLocation(70, 20);
grelha.setBackground(Color.GREEN);
// criar JLabels
for (int num = 0; num < linhas; num++){
for (int num2 = 0; num2 < colunas; num2++){
JLabel label = new JLabel();
label.setText(String.valueOf(tabuleiro[num][num2]));
label.setOpaque(true);
label.setBackground(select_cor(tabuleiro[num][num2]));
label.setHorizontalAlignment(SwingConstants.CENTER);
grelha.add(label);
}
}
return grelha;
this is the function that creates the entire panel in the proper grid etc... (This is also made in a diferent class that extends from the main one
public class Board extends Game{....}
my idea was to use the repaint() function to update only the panel with the grid JPanel grelha = Board.painel();
and then frame.getContentPane().add(grelha);
later my idea was to update the grid (grelha) panel only but when i do this:
/* Main */
public static void main(String[] args){
frame.setTitle("Jogo 2048 em Java"); // Titulo da janela
frame.setSize(janela_x, janela_y); // Define o tamanho da janela
frame.setLocationRelativeTo(null); // Centraliza a janela no ecrã
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setBackground(Color.WHITE);
frame.setResizable(false); // Não deixa a janela ser aumentada
// Painel Fundo
JPanel fundo = new JPanel();
fundo.setBackground(Color.WHITE);
// Painel Botões
JPanel botoes = new JPanel();
GridLayout grid_botoes = new GridLayout(1, 5, 5, 5);
botoes.setLayout(grid_botoes);
botoes.setOpaque(true);
botoes.setSize(360, 50);
botoes.setLocation(70, 390);
botoes.setBackground(Color.WHITE);
JPanel grelha = Board.painel();
// Botões e colocar no painel
JButton init = new JButton("Init");
JButton left = new JButton("Left");
JButton down = new JButton("Down");
JButton right = new JButton("Right");
JButton menu = new JButton("Menu");
botoes.add(init);
botoes.add(left);
botoes.add(down);
botoes.add(right);
botoes.add(menu);
// Adicionar Panels à janela
frame.getContentPane().add(botoes);
frame.getContentPane().add(grelha);
frame.getContentPane().add(fundo);
frame.setVisible(true);
// ActionListener dos botões
ActionListener accao_botoes = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = -1;
if (e.getSource().equals(init)) n = 0;
else if (e.getSource().equals(left)) n = 1;
else if (e.getSource().equals(down)) n = 2;
else if (e.getSource().equals(right)) n = 3;
else n = 4;
switch (n){
case 0:
Board.novo_jogo();
Board.print();
break;
case 1:
Board.esquerda();
Board.print();
break;
case 2:
Board.baixo();
Board.print();
break;
case 3:
Board.direita();
Board.print();
break;
case 4:
janela_menu();
break;
}
}
};
init.addActionListener(accao_botoes);
left.addActionListener(accao_botoes);
down.addActionListener(accao_botoes);
right.addActionListener(accao_botoes);
menu.addActionListener(accao_botoes);
while(true){
frame.repaint();
try{
Thread.sleep(10);
}catch(Exception e){}
}
}
it doesnt update the panel, infact it doesnt update anything :( i tryed grelha.validate(), grelha.repaint() and nothing seems to work, am i missing anything?
Anyone can help me?
create Board instance and keep reference to this object Board board=Board.painel();
operate on that instance board.direita(); board.print();
board.direita(); board.print();
inplementataion of direita in class Board should change JLabel properties
remove while(true)
loop. EDT will update swing widgets.
Example of direita
(other movings make in similar way)
public void direita() {
if (currentX+1 < colunas) ++currentX;
JLabel label = labels[currentY][currentX];
label.setBackground(Color.YELLOW);
}
Example of painel
public static Board painel(){
// Painel Grelha
return new Board();
}
and Board
constructor
public Board() {
GridLayout grid_grelha = new GridLayout(linhas, colunas, 3, 3);
setLayout(grid_grelha);
setOpaque(true);
setSize(janela_x - 140, janela_y-140);
setLocation(70, 20);
setBackground(Color.GREEN);
// criar JLabels
for (int num = 0; num < linhas; num++){
for (int num2 = 0; num2 < colunas; num2++){
JLabel label = new JLabel();
//label.setText(String.format("%d,%d",num,num2));
label.setOpaque(true);
label.setBackground(select_cor(num,num2));
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label);
labels[num][num2]=label;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.