我在网格中有18个按钮,我希望它们在点击时消失。当点击其下有两个相同字母的按钮时,我希望它们被删除或永久不可见。但如果按下两个按钮,其下方有不同的字母,则按钮应再次出现。我正在尝试制作一种“卡片匹配游戏”。我知道这段代码很冗长,并且可能有多种方法可以更有效地输入它。我只是一个初学者,试图弄清楚如何制作节目。谢谢!
public void init()
{
setLayout(new GridLayout(6,3));
r11 = new Rectangle(100,100,200,200);
r12 = new Rectangle(300,100,200,200);
r13 = new Rectangle(500,100,200,200);
r21 = new Rectangle(100,300,200,200);
r22 = new Rectangle(300,300,200,200);
r23 = new Rectangle(500,300,200,200);
r31 = new Rectangle(100,500,200,200);
r32 = new Rectangle(300,500,200,200);
r33 = new Rectangle(500,500,200,200);
r34 = new Rectangle(300,200,300,500);
a = new Button();
b = new Button();
c = new Button();
d = new Button();
e = new Button();
f = new Button();
g = new Button();
h = new Button();
i = new Button();
j = new Button();
k = new Button();
l = new Button();
m = new Button();
n = new Button();
o = new Button();
p = new Button();
q = new Button();
r = new Button();
add(a);
add(b);
add(c);
add(d);
add(e);
add(f);
add(g);
add(h);
add(i);
add(j);
add(k);
add(l);
add(m);
add(n);
add(o);
add(p);
add(q);
add(r);
}
public void setLocation(int x, int y)
{
}
public void setSize(int width, int height)
{
}
public void drawGrid(Graphics g)
{
g.setColor(Color.gray);
g.drawRect(100,100,800,600);
g.drawLine(367,100,367,700);
g.drawLine(634,100,634,700);
g.drawLine(100,300,900,300);
g.drawLine(100,500,900,500);
g.drawLine(100,400,900,400);
g.drawLine(100,200,900,200);
g.drawLine(100,600,900,600);
}
public void paint(Graphics g)
{
this.a.setLocation(100,100);
this.a.setSize(267,100);
this.b.setLocation(368,100);
this.b.setSize(267,100);
this.c.setLocation(634,100);
this.c.setSize(267,100);
this.d.setLocation(100,200);
this.d.setSize(267,100);
this.e.setLocation(368,200);
this.e.setSize(267,100);
this.f.setLocation(634,200);
this.f.setSize(267,100);
this.g.setLocation(100,300);
this.g.setSize(267,100);
this.h.setLocation(368,300);
this.h.setSize(267,100);
this.i.setLocation(634,300);
this.i.setSize(267,100);
this.j.setLocation(100,400);
this.j.setSize(267,100);
this.k.setLocation(368,400);
this.k.setSize(267,100);
this.l.setLocation(634,400);
this.l.setSize(267,100);
this.m.setLocation(100,500);
this.m.setSize(267,100);
this.n.setLocation(368,500);
this.n.setSize(267,100);
this.o.setLocation(634,500);
this.o.setSize(267,100);
this.p.setLocation(100,600);
this.p.setSize(267,100);
this.q.setLocation(368,600);
this.q.setSize(267,100);
this.r.setLocation(634,600);
this.r.setSize(267,100);
drawGrid(g);
g.setColor(Color.cyan);
drawLetter(g,"G",100,100);
drawLetter(g,"A",300,100);
drawLetter(g,"B",500,100);
drawLetter(g,"B",100,200);
drawLetter(g,"E",300,200);
drawLetter(g,"C",500,200);
drawLetter(g,"G",100,300);
drawLetter(g,"H",300,300);
drawLetter(g,"I",500,300);
drawLetter(g,"D",100,400);
drawLetter(g,"F",300,400);
drawLetter(g,"D",500,400);
drawLetter(g,"A",100,500);
drawLetter(g,"H",300,500);
drawLetter(g,"E",500,500);
drawLetter(g,"I",100,600);
drawLetter(g,"F",300,600);
drawLetter(g,"C",500,600);
}
public Rectangle[][] getClicked()
{
return clicked;
}
public void drawLetter(Graphics g,String letter, int x, int y)
{
int offSetX = x + 175;
int offSetY = y + 91;
g.setFont(new Font("Arial",Font.BOLD,100));
g.drawString(letter,offSetX,offSetY);
}
class buttonss implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}