开平
我想要实现的是点击按钮,图片会显示,然后我想保存该图片的ID,并能够点击另一个按钮,检查这两个图片ID是否相同。如果是的话,程序会让我继续玩,直到找到所有对(我不在乎找到的对被删除或只是留下)如果在两个clicsk中找不到一对,点击的卡就会重置......
thehandler类本身只是一个实验,我不认为有什么可以从那里打捞
谢谢你的帮助!
package memorygame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GUI extends JFrame {
private JButton nupp1, nupp2, nupp3, nupp4; // Kasutajaliidese nupud
ImageIcon A = new ImageIcon(this.getClass().getResource("num1.png"));
ImageIcon B = new ImageIcon(this.getClass().getResource("num2.png"));
public GUI() {
setTitle("MEMORY Game"); // Tiitel
setLayout(new GridLayout(2, 2, 3, 3));
nupp1 = new JButton(); // Peab manuaalselt nupud lisama displayle
nupp1.setName("piltyks");
add(nupp1);
nupp2 = new JButton();
nupp2.setName("piltkaks");
add(nupp2);
nupp3 = new JButton();
nupp3.setName("piltyks");
add(nupp3);
nupp4 = new JButton();
nupp4.setName("piltkaks");
add(nupp4);
thehandler handler = new thehandler();
nupp1.addActionListener(handler);
nupp2.addActionListener(handler);
nupp3.addActionListener(handler);
nupp4.addActionListener(handler);
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
boolean v22rtusnupp1 = true;
boolean v22rtusnupp2 = true;
boolean v22rtusnupp3 = true;
boolean v22rtusnupp4 = true;
String v22rtus1 = null;
String v22rtus2 = null;
int loendur = 0;
int tulemus = 0;
if (event.getSource() == nupp1) {
if (v22rtusnupp1 == true) {
if (loendur == 0) {
v22rtus1 = nupp1.getName();
}
if (loendur == 1) {
v22rtus2 = nupp1.getName();
}
loendur = loendur + 1;
nupp1.setIcon(A);
System.out.println(v22rtus1);
v22rtusnupp1 = false;
}
}
if (event.getSource() == nupp2) {
if (v22rtusnupp2 == true) {
v22rtusnupp2 = false;
if (loendur == 0) {
v22rtus1 = nupp2.getName();
}
if (loendur == 1) {
v22rtus2 = nupp2.getName();
}
loendur = loendur + 1;
nupp2.setIcon(B);
System.out.println(v22rtus1);
}
}
if (event.getSource() == nupp3) {
if (v22rtusnupp3 == true) {
v22rtusnupp3 = false;
if (loendur == 0) {
v22rtus1 = nupp3.getName();
}
if (loendur == 1) {
v22rtus2 = nupp3.getName();
}
loendur = loendur + 1;
nupp3.setIcon(A);
System.out.println(v22rtus1);
}
}
if (event.getSource() == nupp4) {
if (v22rtusnupp4 == true) {
v22rtusnupp4 = false;
if (loendur == 0) {
v22rtus1 = nupp4.getName();
}
if (loendur == 1) {
v22rtus2 = nupp4.getName();
}
loendur = loendur + 1;
nupp4.setIcon(B);
System.out.println(v22rtus1);
}
}
System.out.println(loendur);
// if(nupp4.getName() == nupp2.getName()){
//
// }
//
// if(event.getSource() == nupp1){
// nupp1.setIcon(A);
// }else{ if(event.getSource()== nupp3)
// nupp3.setIcon(A);
// if(event.getSource()== nupp2)
// nupp2.setIcon(B);
// if(event.getSource()== nupp4)
// nupp4.setIcon(B);
// }
//
//
//
}
}
}