我在java中制作一个记忆卡游戏它由10个ImageIcons组成,前5个ImageIcons是原始的,另外5个是副本,我找不到一种方法来显示其他图像的另一面复制时按下按钮,我做了一个for循环,以便制作2个图像副本,并使它们具有相同的索引,但按钮仅适用于前5个ImageIcons,但它不适用于ImageIcons的其他5个副本(虽然他们有相同的指数)
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.*;
public class MemoControl extends JFrame{
JPanel game;
EventClass event;
private JButton button[];
private JLabel label[];
private ImageIcon image[];
private ImageIcon flippedImg;
private String imgNames[];//not used yet
int num;
public MemoControl(){
setTitle("MyMemo Game");
setLayout(new GridLayout(5,2));
game = new JPanel();
image = new ImageIcon[50];
button = new JButton[50];
label = new JLabel[50];
flippedImg = new ImageIcon(getClass().getResource("card_cover1.jpg"));;
for(int j = 1; j<6; j++)
{
image[j] = new ImageIcon(getClass().getResource(j+".jpg"));
button[j] = new JButton("");
button[j].addActionListener(new MemoControl.EventClass());
button[j].setIcon(flippedImg);
add(button[j]);
image[j] = new ImageIcon(getClass().getResource(j+".jpg"));
button[j] = new JButton("");
button[j].addActionListener(new MemoControl.EventClass());
button[j].setIcon(flippedImg);
add(button[j]);
/*label[j] = new JLabel(image[j]);
add(label[j]);*/
}
EventClass event = new EventClass();
/*button = new JButton[50];
for(int i = 1; i<=5; i++)
{
button[i] = new JButton("");
add(button[i]);
button[i].addActionListener(event);
}*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}//MyMemo constr end //event class
private class EventClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i <6; i++) {
if (e.getSource() == button[i]) {
button[i].setIcon(image[i]);
}
}
}
}
public static void main(String args[]){
MemoControl gui = new MemoControl();
}//main end
}//AMYMemo class end
******************更新:
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.*;
public class MemoControl extends JFrame{
JPanel game;
EventClass event;
private JButton button[][];
private JLabel label[][];
private ImageIcon image[][];
private ImageIcon flippedImg;
private String imgNames[];//not used yet
int num;
public MemoControl(){
setTitle("MyMemo Game");
setLayout(new GridLayout(5,2));
game = new JPanel();
image = new ImageIcon[2][5];
button = new JButton[2][5];
label = new JLabel[2][5];
flippedImg = new ImageIcon(getClass().getResource("card_cover1.jpg"));;
for(int i = 0; i < image.length(); i++) {
for(int j = 0; j < image.length(); j++) {
image[i][j] = new ImageIcon(getClass().getResource(j+".jpg"));
button[i][j] = new JButton("");
button[i][j].addActionListener(new MemoControl.EventClass());
button[i][j].setIcon(flippedImg);
add(button[i][j]);
}
}
EventClass event = new EventClass();
/*button = new JButton[50];
for(int i = 1; i<=5; i++)
{
button[i] = new JButton("");
add(button[i]);
button[i].addActionListener(event);
}*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}//MyMemo constr end //event class
private class EventClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < image.length(); i++) {
for(int j = 0; j < image[0].length(); j++) {
if (e.getSource() == button[i][j])
button[i].setIcon(image[i][j]);
}
}
}
}
public static void main(String args[]){
MemoControl gui = new MemoControl();
}//main end
}//AMYMemo class end
更新后错误:
G:\MYMemo\MemoControl.java:31: error: cannot find symbol
for(int i = 0; i < image.length(); i++) {
^
symbol: method length()
location: variable image of type ImageIcon[][]
G:\MYMemo\MemoControl.java:32: error: cannot find symbol
for(int j = 0; j < image.length(); j++) {
^
symbol: method length()
location: variable image of type ImageIcon[][]
G:\MYMemo\MemoControl.java:64: error: cannot find symbol
for(int i = 0; i < image.length(); i++) {
^
symbol: method length()
location: variable image of type ImageIcon[][]
G:\MYMemo\MemoControl.java:65: error: cannot find symbol
for(int j = 0; j < image[0].length(); j++) {
^
symbol: method length()
location: class ImageIcon[]
G:\MYMemo\MemoControl.java:67: error: cannot find symbol
button[i].setIcon(image[i][j]);
^
symbol: method setIcon(ImageIcon)
location: class JButton[]
5 errors
答案 0 :(得分:0)
我在这里看到很多问题,而不是所有与问题有关的问题。
首先,for(int j = 1; j<6; j++)
将循环6次。在循环内部,您运行add(button[j])
两次,这意味着它将总共添加12个项目,而不是10个。
其次,你为每个循环分配button[j]
两次。这意味着您将覆盖button[j]
的第一个值。例如,请查看以下代码:
int[] arr = new int[5];
for(int i = 0; i < arr.length; i++) {
arr[i] = 10
arr[i] = 20
}
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ", ");
}
//20, 20, 20, 20, 20,
注意这些都是20,而且没有10。所以你只有一半对象的原因是因为你覆盖了上半部分。你说“虽然他们有相同的索引”。数组只是对象的集合。因此array[0]
基本上是someVar
,将其设置为对象会删除之前存储的内容。
如果您要做的是拥有2x5网格,那么您应该使用2D数组。在你的情况下:
images = new ImageIcon[2][5];
buttons = new JButton[2][5];
labels = new JLabel[2][5];
现在,要迭代,你会做
for(int i = 0; i < images.length; i++) {
for(int j = 0; j < images[0].length(); j++) {
images[i][j] = new ImageIcon(getClass().getResource(j+".jpg"));
buttons[i][j] = new JButton("");
button[i][j].addActionListener(new MemoControl.EventClass());
button[j].setIcon(flippedImg);
add(button[i][j]);
}
}
现在,我不了解程序的整体结构,但上面会创建10个按钮,ImageIcon
在行之间匹配。防爆。 button[0][0]
的图片与button[1][0]
的图片相同。
希望我能相对清楚地解释一下。我的java有点生疏,所以可能会有一些语法错误。