我正在制作Java上的GUI匹配游戏,我想制作它以便在按下重复按钮时将其更改为橙色以外的颜色。例如,如果游戏板上有2个标记为“ 6”的按钮,我希望它将这些按钮更改为蓝色而不是橙色
任何帮助将不胜感激。
这里是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class link extends JFrame implements ActionListener{
JPanel panel1, panel2,panel3,panel4,panel5,panel6;
JLabel l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
JButton Reset;
int da=0;
int ea=0;
int ra=0;
int grass=0;
String track= "";
String rack="";
int fly=0;
int counter=5;
int count=0;
int winner=0;
Color blue;
Color orange;
Color lime;
JButton[]linx=new JButton[53];
int workspaceWidth=1200, workspaceHeight=800;
public static void main(String[ ] args)
{
new link();
}
public link(){
blue=new Color(19, 196, 255);
orange= new Color(255, 33, 44);
orange= new Color(255, 89, 0);
lime= new Color(110, 255, 110);
panel1=new JPanel();
panel2=new JPanel();
panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
Font buttons= new Font("Bahnschrift", Font.BOLD, 25);
Font apples= new Font("Bahnschrift", Font.BOLD, 20);
panel1.setLayout(new GridLayout(8,6)); // Sets Grid Layout
panel2.setLayout(new GridLayout(1,4)); // Sets Grid Layout
for(int q=0;q<52;q++)
{
da= (int)(Math.random()*20)+1;
linx[q]=new JButton(""+da);
linx[q].addActionListener (this);
linx[q].setFont(apples);
linx[q].setOpaque(true);
linx[q].setForeground(Color.WHITE);
linx[q].setBackground(Color.BLACK);
panel1.add(linx[q]);
}
Reset=new JButton("Reset");
Reset.addActionListener (this);
Reset.setFont(apples);
Reset.setOpaque(true);
Reset.setForeground(Color.WHITE);
Reset.setBackground(blue);
panel1.add(Reset);
l1=new JLabel(" "+counter+" Tries left");
l1.setOpaque(false);
l1.setForeground(Color.WHITE);
l1.setFont(buttons);
panel1.add(l1);
panel1.setBackground(Color.BLACK);
this.setLayout(new GridLayout(1,1));
this.add(panel1);
this.setSize(workspaceWidth,workspaceHeight);
this.setSize(1100,600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
for(int q=0;q<53;q++)
{
if(e.getSource()==linx[q])
{
linx[q].setBackground(orange);
l1.setText(" "+counter--+" Tries left");
if(linx[q].getText().equals[q]&&linx[q].getText().equals[q])
{
System.out.print("track ");
}
}
}
if(counter==0)
{
for(int q=0;q<53;q++)
{
l1.setText(" "+"You Lost");
l1.setForeground(Color.YELLOW);
linx[q].setEnabled(false);
}
}
if(winner==36)
{
l1.setText(" "+"You Won");
l1.setForeground(lime);
}
if(e.getSource()==Reset)
{
for(int q=0;q<53;q++)
{
linx[q].setEnabled(true);
linx[q].setBackground(Color.BLACK);
counter=5;
l1.setForeground(Color.WHITE);
l1.setText(" "+counter+" Tries left");
}
}
}
}