我正在尝试制作一个记忆卡游戏,其中有2个玩家,轮流翻转2张卡。我编写了以下内容,并且代码有效,但是当我单击JButton时,它不显示图标。这是我写的。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import net.proteanit.sql.DbUtils;
public class doFirst extends JFrame{
public doFirst(){
p = new JPanel(new BorderLayout());
iconarray[0] = new ImageIcon("1.png");
iconarray[1] = new ImageIcon("2.png");
iconarray[2] = new ImageIcon("3.png");
iconarray[3] = new ImageIcon("4.png");
iconarray[4] = new ImageIcon("5.png");
iconarray[5] = new ImageIcon("6.png");
iconarray[6] = new ImageIcon("1.png");
iconarray[7] = new ImageIcon("2.png");
iconarray[8] = new ImageIcon("3.png");
iconarray[9] = new ImageIcon("4.png");
iconarray[10] = new ImageIcon("5.png");
iconarray[11] = new ImageIcon("6.png");
iconarray[12] = new ImageIcon("flipped.png");
p2 = new JPanel(new GridLayout(3,4));
for (int i = 0; i <= 11; i++) {
list1.add(0);
}
b = new JButton[12];
for(int x =0; x<b.length; x++){
b[x] = new JButton();
b[x].setBackground(new Color(102, 0, 153));
b[x].setIcon(new ImageIcon(getClass().getResource("flipped.png")));
b[x].setPreferredSize(new Dimension(150, 150));
list.add(b[x]);
b[x].addActionListener(new ImageButtonListener());
}
Collections.shuffle(list);
for(int x = 0; x < list.size(); x++)
{
p2.add((JButton)list.get(x));
}
int y = 0;
while (y<12) {
int x = rand1.nextInt(6);
list1.set(x, list1.get(x).intValue() + 1);
if (list1.get(x) <= 2) {
b[y].setName(Integer.toString(x));
y++;
}
}
timer1 = new Timer(1000, new TimerListener());
p3 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5));
player = new JLabel("Player");
scor1 = new JLabel("0");
computer = new JLabel("Computer");
scor2 = new JLabel("0");
p3.add(player);
p3.add(scor1);
p3.add(computer);
p3.add(scor2);
p.add(p2, BorderLayout.CENTER);
p.add(p3, BorderLayout.SOUTH);
add(p);
setLocationRelativeTo(null);
setVisible(true);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
class ImageButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (timer1.isRunning())
return;
for (int i = 0; i < 12; i++)
if (e.getSource() == b[i]) {
int x = Integer.parseInt(b[i].getName());
b[i].setIcon(iconarray[x]);
if (counter == 1) {
button1 = b[i];
counter++;
}
if (counter == 2 && b[i] != button1) {
button2 = b[i];
compareicons();
}
}
}
private void compareicons() {
if (button1.getIcon() == button2.getIcon()) {
button1.setIcon(new ImageIcon(getClass().getResource("empty.png")));
button2.setIcon(new ImageIcon(getClass().getResource("empty.png")));
button1.setEnabled(false);
button2.setEnabled(false);
if (player1 == true) {
points1++;
scor1.setText(Integer.toString(points1));
} else {
points2++;
scor2.setText(Integer.toString(points2));
}
}
else {
if (player1 == true) {
player1 = false;
} else {
player1 = true;
}
timer1.start();
}
//reset counter
counter = 1;
}
}
public void shuffleButtons() {
if (b != null) {
Collections.shuffle(Arrays.asList(b));
layoutButtons();
}
}
public void layoutButtons() {
p2.removeAll();
for (JButton button : b) {
p2.add(button);
}
p2.revalidate();
p2.repaint();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
button1.setIcon(new ImageIcon(getClass().getResource("flipped.png")));
button2.setIcon(new ImageIcon(getClass().getResource("flipped.png")));
timer1.stop();
// active = true;
}
}
}
public static void main(String[] args){
new doFirst().setVisible(true);
}
private JPanel p, p1, p2, p3, p4;
private ArrayList list = new ArrayList();
private JMenuBar mb;
private final JButton[] b;
private ImageIcon[] iconarray = new ImageIcon[13];
private JMenu game, options, help;
private JMenuItem newGame, restart, exit, showHistory, hideHistory, about;
private JLabel player, computer, scor1, scor2, blank1, blank2;
private int counter = 1;
private Timer timer1;
private boolean player1;
private int points1 = 0;
private int points2 = 0;
private ArrayList<Integer> list1 = new ArrayList<Integer>();
private Random rand1 = new Random();
private JButton[] but = new JButton[2];
private int[] x = new int[2];
private JButton button1, button2;
}
下面应该使图标可见的行。
if (e.getSource() == b[i]) {
int x = Integer.parseInt(b[i].getName());
b[i].setIcon(iconarray[x]);
当我运行文件并单击2个JButton时,它显示jframe下面的图片。 我试过将b [i] .setIcon(iconarray [x])替换为b [i] .setIcon(iconarray [i]),但没有成功。关于如何使其运作的任何想法?