2016-05-31 18 views
0

基本的には、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()と何も動作するように見える、何かが不足している?

誰でも手伝ってもらえますか?

+0

最初の

public void direita() { if (currentX+1 < colunas) ++currentX; JLabel label = labels[currentY][currentX]; label.setBackground(Color.YELLOW); } 

例 - 私はできません' frame.setVisible(true); ' second - フレームを更新すると思いますか? – krzydyn

+0

私のすべてのメインを含めるようにコードを更新しました – PandaGOD82

+0

ラベルボタンをクリックするたびにラベルを更新するはずです(メニューボタンを除く) – PandaGOD82

答えて

1
  1. ボードのインスタンスを作成し、 Board board=Board.painel();

  2. がクラス会でdireitaの ​​

  3. inplementataionがJLabelのプロパティに

  4. 削除を変更する必要があり、そのインスタンス上で動作し、このオブジェクトへの参照を保持while(true)ループ。 EDTがスイングウィジェットを更新します。

direitaの例(他のmovingsは同様の方法で行った)painel

public static Board painel(){ 
    // Painel Grelha 
    return new Board(); 
} 

Boardコンストラクタ

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; 
     } 
    } 
} 
+0

ボードボード= Board.painel();ペイントはJPanelを返すので、ボードをJPanelに変更する必要がありますか?またはpainel()の戻り値の型をBoardに変更しますか? – PandaGOD82

+0

また、Board.printは、現時点で取得したintの配列のテキスト表現です:prntscr。com/bb6nk9 – PandaGOD82

+0

あなたがボードのdireitaのinplementaionがJLabelのプロパティを変更すべきであると言うとき、私はボード上の "direita()"関数で "painel()"関数を実行しなければならないということですか?申し訳ありませんが、私はこれで非常にnoob、スイングに取り組んで初めて:/ – PandaGOD82

関連する問題