如何通过单击将具有相同值和相同颜色的两个jbutton删除?

时间:2018-12-30 17:06:13

标签: java swing jbutton jlabel tiles

我有一个彩色按钮矩阵,单击它们后需要比较它们的颜色和值。我所有的代码要做的是删除每个按钮而不搜索它们的值或颜色。此外,我需要添加jlabel而不重新排列jbuttons。我该如何解决该问题?

public class Legos2 extends JFrame implements ActionListener{

    private static final long serialVersionUID = 1L;

    public JPanel jp = (JPanel)this.getContentPane();
    public JButton[][] jb = new JButton[12][24];//my matrix
    public static JLabel jl = new JLabel("score",SwingConstants.CENTER);//a jlabel to count the scores every time two buttons are removed

    public Legos2() {
        super();
        this.setSize(2000,2000);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jp.setLayout(new GridLayout(12, 24));//size of my jpanel

        Random rnd = new Random();
        Color[] c = {Color.lightGray, Color.gray, Color.white,  Color.cyan};//colored jbuttons
        final int MAXCOLOR = c.length;
        JButton jb1;

        for(int i = 0;i <jb.length;i++) {
            for(int j = 0; j<jb[i].length; j++) {
                int k = (int) (Math.random()*9+1);//k is a random integer from 1 to 9
                jb1 = new JButton(Integer.toString(k));

                add(jb1);
                jb[i][j]=jb1;
                jb[i][j].addActionListener(this);
            }
        }

        for(int i=0; i<jb.length; i++){
            for(int j = 0; j<jb[i].length; j++) {
                jb[i][j].setBackground(c[rnd.nextInt(MAXCOLOR)]);//i add colors in here
            }
        }

        for (int row = 0; row < jb.length; row++) {
            for (int column = 0; column < jb[row].length; column++) {
                jb[row][column].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JButton button = (JButton) e.getSource();
                        String buttonText = button.getText();
                        // now iterate over all the jbuttons you have
                        for(int i=0;i<jb.length;i++){
                            for(int j=0;j<jb[0].length;j++){
                                JButton b = jb[i][j];
                                String bText = b.getText();
                                if(e.getSource()==b)
                                    if(buttonText.equals(bText)){
                                        b.setEnabled(false);

                                        jl.setText(Integer.toString(i));
                                    }
                            }
                        }
                    }
                }
                        );
                jp.add(jb[row][column]);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

以下是mcve所需的功能。请查看并要求进行澄清:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Legos2 extends JFrame{

    private static final long serialVersionUID = 1L;
    private static final int ROWS = 12, COLS =24;
    public JButton[][] jb = new JButton[ROWS][COLS];
    private int score = 0;
    public static JLabel jl;

    public Legos2() {
        super();
        this.setSize(2000,2000);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel jp= new JPanel();
        jp.setLayout(new GridLayout(ROWS, COLS));

        Random rnd = new Random();
        Color[] c = {Color.lightGray, Color.gray, Color.white,  Color.cyan};
        final int MAXCOLOR = c.length;
        //all can be done in one loop
        for(int i = 0;i <jb.length;i++) {
            for(int j = 0; j<jb[i].length; j++) {
                int k = (int) (Math.random()*9+1);//k is a random integer from 1 to 9
                JButton button = new JButton(Integer.toString(k));
                button.setBackground(c[rnd.nextInt(MAXCOLOR)]);
                button.addActionListener(e -> {
                    disableTwoMatchingButtons(button);
                });
                jb[i][j]=button;
                jp.add(button);
            }
        }

        add(jp); //add panel at content pane center
        jl= new JLabel("",SwingConstants.CENTER);
        updateScore(0);
        add(jl, BorderLayout.PAGE_END); //add label at content pane bottom
        pack();
        setVisible(true);
    }

    private void disableTwoMatchingButtons(JButton button) {

        for(int i=0;i<jb.length;i++){
            for(int j=0;j<jb[0].length;j++){
                JButton b = jb[i][j];
                if(b.isEnabled() && button.getText().equals(b.getText()) &&
                                button.getBackground().equals(b.getBackground())){
                    b.setEnabled(false);
                    button.setEnabled(false);
                    updateScore(2* Integer.valueOf(b.getText()));
                    return;
                }
            }
        }
    }

    private void updateScore(int value) {
        score += value;
        jl.setText("Score: "+ score );
    }

    public static void main(String[] args) {
        new Legos2();
    }
}

如果您需要禁用所有匹配按钮,而不仅仅是两个,请使用:

    private void disableAllMatchingButtons(JButton button) {

        for(int i=0;i<jb.length;i++){
            for(int j=0;j<jb[0].length;j++){
                JButton b = jb[i][j];
                if(b.isEnabled() && button.getText().equals(b.getText())
                                && button.getBackground().equals(b.getBackground())){
                    b.setEnabled(false);
                    updateScore(Integer.valueOf(b.getText()));
                }
            }
        }
    }

(而不是disableTwoMatchingButtons
对于将来的帖子,请始终考虑发布mcve,并在每个帖子中坚持一个问题。