基本的には、GridLayout 5x5を持つJPanelと、そこにある数に応じて、番号と背景色が異なるJLabelとしてそれぞれ「正方形」があります。GridLayout 5x5でJPanel内のJLabelの配列を更新
ボタンをクリックするたびにパネルを更新する必要があります(メニュー1を除く)。これはラベルに入れる数値を取得するintの配列の配列を変更するためです。
私の質問は、(これもで作られて ...これは、適切なグリッドなどでパネル全体を作成する機能であるどのように私は(色を含む)すべてのJLabelのを更新しない 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;
ですメイン1
public class Board extends Game{....}
私の考えから延びdiferentクラスはグリッドでのみパネルを更新するために再描画()関数を使用することでしたJPanel grelha = Board.painel();
、その後frame.getContentPane().add(grelha);
後の私の考えは、グリッド(grelha)パネルのみを更新することでしたが、私が行うときに、この:
/* 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){}
}
}
それはパネルを更新doesntの、Infactはそれは(私はgrelha.validateをしようと試みた:( 何を更新doesntの)、 grelha.repaint()と何も動作するように見える、何かが不足している?
誰でも手伝ってもらえますか?
最初の
例 - 私はできません' frame.setVisible(true); ' second - フレームを更新すると思いますか? – krzydyn
私のすべてのメインを含めるようにコードを更新しました – PandaGOD82
ラベルボタンをクリックするたびにラベルを更新するはずです(メニューボタンを除く) – PandaGOD82