Java 当与另一个方块匹配时,尝试更改GUI中方块的颜色

Java 当与另一个方块匹配时,尝试更改GUI中方块的颜色,java,swing,colors,Java,Swing,Colors,这将创建一个包含3行4列的jframe。我正在尝试实现一个记忆游戏,它与字母匹配。到目前为止,代码匹配,字母被随机洗牌。我想把匹配的两个单词的正方形的颜色改成蓝色。我在网上找不到关于这个的任何信息 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionLis

这将创建一个包含3行4列的jframe。我正在尝试实现一个记忆游戏,它与字母匹配。到目前为止,代码匹配,字母被随机洗牌。我想把匹配的两个单词的正方形的颜色改成蓝色。我在网上找不到关于这个的任何信息

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.border.Border;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.BorderFactory;


public class matchinggame implements ActionListener {
    JPanel jp = new JPanel();
    JFrame jf = new JFrame();

    String[] matchList = { "dank", "dank", "memes", "memes", "360", "360", "headshot", "headshot", "doge", "doge",
            "mlg", "mlg", "pro", "pro" };
    String[] shuffledList;

    JButton[][] buttons;

    boolean flipping = true;

    int i = 0;
    int cardOne;
    int secIndex;

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread
        // to create application and display its GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                matchinggame app = new matchinggame();
                app.makeGUI();
            }
        });
    }

    public void dealCards(JPanel panel) {
        buttons = new JButton[3][]; // array of buttons used to represent cards
        shuffleCards();
        for (int i = 0; i < 3 * 4; i++) { // initialize 3 rows with 4 columns
                                            // each
            if (i % 4 == 0)
                buttons[i / 4] = new JButton[4];
            buttons[i / 4][i % 4] = new JButton("choose me"); // show face down
            buttons[i / 4][i % 4].addActionListener(this);
            panel.add(buttons[i / 4][i % 4]);
        }
    }

    public void shuffleCards() {
        List<String> list = Arrays.asList(matchList);
        Collections.shuffle(list);
        shuffledList = list.toArray(new String[list.size()]);
    }

    public void updateMatchList(String a, String b, boolean add) {
        String[] courseList;
        int oldLen = matchList.length;

        if (add) { // add the new item to the list
            courseList = new String[oldLen + 2];
            courseList[0] = new String(a); // new first course
            courseList[1] = new String(b); // new first course num
            for (int item = 2; item <= oldLen; item += 2) {
                courseList[item] = matchList[item - 2];
                courseList[item + 1] = matchList[item - 1];
            }
            matchList = courseList;
        } else { // delete matching item
            courseList = new String[oldLen - 2];
            int matchItem = 0;
            for (int item = 0; item <= oldLen; item += 2) {
                if (a != matchList[item]) { // no match so OK to copy over
                    courseList[item] = matchList[matchItem];
                    courseList[item + 1] = matchList[matchItem + 1];
                    matchItem += 2;
                }
            }
            matchList = courseList;
        }

    }

    /**
     * Creates the JFrame and its UI components.
     */
    public void makeGUI() {
        JFrame frame = new JFrame("matchinggame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jp = new JPanel(new GridLayout(3, 4));
        jp.setPreferredSize(new Dimension(500, 300));
        dealCards(jp);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(jp, BorderLayout.CENTER);

        // Display the window.
        frame.pack();
        frame.setSize(500, 500);
        frame.setVisible(true);
        jp.setBorder(BorderFactory.createLineBorder(Color.RED,16));

    }

    public void actionPerformed(ActionEvent e) {
        int r, c;

        if (i < 2) { // find the card clicked on and flip it over

            for (r = 0; r < 3; r++) {
                for (c = 0; c < 4; c++) {
                    // if the card is not face down (showing "choose me") don't
                    // flip it
                    if ((e.getSource() == buttons[r][c])
                            && buttons[r][c].getText().equals("choose me")) {
                        // flip the card face-up to show text from matchList
                        // looks up text based upon indexes
                        buttons[r][c].setText(shuffledList[(r * 4 + c)]);
                        i++; // increment number of cards flipped
                        if (i == 1)
                            cardOne = (r * 4 + c); // save which pattern was
                                                    // shown first
                        else
                            secIndex = (r * 4 + c); // save the pattern
                                                    // shown second
                        return;
                    }
                }
            }
        } else { // 2 cards already flipped, put all cards face down

            for (r = 0; r < 3; r++) {
                for (c = 0; c < 4; c++) {
                    // first and second cards flipped
                    if (shuffledList[cardOne].equals(shuffledList[secIndex])) {
                        // match
                        // don't change the face down cards
                        if (!buttons[r][c].getText().equals("choose me"))
                            // once matched, show the removed pattern
                            buttons[r][c].setText("dankified");
                    } else if ((!buttons[r][c].getText().equals("dankified"))
                            && (!buttons[r][c].getText().equals("choose me"))) {
                        // if 2 face up cards didn't match, flip face down again
                        buttons[r][c].setText("choose me");
                    }
                }
                i = 0; // new turn, no cards flipped face up
            }
        }
    }
}
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
导入javax.swing.border.border;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入java.awt.Color;
导入javax.swing.BorderFactory;
公共类matchinggame实现ActionListener{
JPanel jp=新的JPanel();
JFrame jf=新JFrame();
字符串[]匹配列表={“潮湿”、“潮湿”、“迷因”、“迷因”、“360”、“360”、“头像”、“头像”、“狗”、“狗”,
“mlg”、“mlg”、“pro”、“pro”};
字符串[]shuffledList;
JButton[]]按钮;
布尔翻转=真;
int i=0;
int卡顿;
int-secIndex;
公共静态void main(字符串[]args){
//为事件调度线程计划作业
//创建应用程序并显示其GUI
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
matchinggame应用程序=新建matchinggame();
app.makeGUI();
}
});
}
公共信用卡(JPanel面板){
buttons=new JButton[3][];//用于表示卡的按钮数组
shuffleCards();
对于(inti=0;i<3*4;i++){//初始化3行4列
//各
如果(i%4==0)
按钮[i/4]=新的JButton[4];
按钮[i/4][i%4]=新的JButton(“选择我”);//面朝下显示
按钮[i/4][i%4]。addActionListener(此);
面板。添加(按钮[i/4][i%4]);
}
}
公共文件{
List=Arrays.asList(匹配列表);
集合。洗牌(列表);
shuffledList=list.toArray(新字符串[list.size()]);
}
public void updateMatchList(字符串a、字符串b、布尔添加){
字符串[]课程列表;
int oldLen=matchList.length;
如果(添加){//将新项目添加到列表中
courseList=新字符串[oldLen+2];
courseList[0]=新字符串(a);//新的第一个课程
courseList[1]=新字符串(b);//新的第一个课程编号

对于(int item=2;item要更改匹配单元格的颜色,可以添加
setBackground()

在上述情况下,应将按钮颜色更改为蓝色。我认为这也是一个好的解决方案,添加:

buttons[r][c].setEnabled(false);
setBackground()
语句下面。这样,已经匹配的单元格将被停用(您不能再单击它)

还有12个单元格/按钮和14个wor

buttons[r][c].setEnabled(false);