使用GridLayout 5x5更新JPanel中的JLabel数组

时间:2016-05-31 22:11:23

标签: java swing jpanel labels

Basicaly我有一个带有GridLayout 5x5的JPanel和每个“square”作为JLabel,带有数字和不同的背景颜色,具体取决于它的数量。

我每次点击其中一个按钮(菜单1除外)时都需要更新面板,因为它会改变整数数组的数组,这是我从中获取数字的位置。

我的问题是如何更新每个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;

这是在适当的网格等中创建整个面板的功能...... (这也是在一个不同的类中,从主要的

延伸
public class Board extends Game{....}

我的想法是使用repaint()函数仅更新网格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){}
    }
}

它没有更新面板,事实上它没有更新任何东西:( 我尝试了grelha.validate(),grelha.repaint(),似乎什么都没有用,我错过了什么?

任何人都可以帮助我?

1 个答案:

答案 0 :(得分:1)

  1. 创建Board实例并保持对此对象的引用 Board board=Board.painel();

  2. 对该实例进行操作 board.direita(); board.print();

  3. 班级董事会中的direita应该改变JLabel属性

  4. 删除while(true)循环。 EDT将更新swing小部件。

  5. direita的示例(其他移动以类似方式制作)

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

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