在Java内存游戏中按下按钮时,无法显示图像图标的其他副本
我正在用java制作一个记忆卡游戏,它由10个图像图标组成,前5个图像图标是原始的,其他5个是副本,当按下按钮时,我找不到显示复制的其他图像的另一面的方法,我制作了一个for循环,以便制作两个图像副本,并使它们具有相同的索引,但按钮仅对前5个图像图标有效,但对其他5个图像图标无效(尽管它们具有相同的索引)在Java内存游戏中按下按钮时,无法显示图像图标的其他副本,java,button,imageicon,Java,Button,Imageicon,我正在用java制作一个记忆卡游戏,它由10个图像图标组成,前5个图像图标是原始的,其他5个是副本,当按下按钮时,我找不到显示复制的其他图像的另一面的方法,我制作了一个for循环,以便制作两个图像副本,并使它们具有相同的索引,但按钮仅对前5个图像图标有效,但对其他5个图像图标无效(尽管它们具有相同的索引) import java.awt.*; 导入javax.swing.*; 导入javax.swing.JFrame; 导入java.awt.event.*; 公共类控件扩展了JFrame{ JP
import java.awt.*;
导入javax.swing.*;
导入javax.swing.JFrame;
导入java.awt.event.*;
公共类控件扩展了JFrame{
JPanel游戏;
事件类事件;
私有JButton按钮[];
私人JLabel标签[];
私有图像图标图像[];
私有图像图标;EDIMG;
私有字符串imgNames[];//尚未使用
int-num;
公共交通管制(){
setTitle(“我的备忘录游戏”);
setLayout(新网格布局(5,2));
游戏=新JPanel();
图像=新图像图标[50];
按钮=新的JButton[50];
标签=新的JLabel[50];
flippedImg=newImageIcon(getClass().getResource(“card_cover1.jpg”);;
对于(intj=1;j,我在这里看到了很多问题,并非所有问题都与这个问题有关
首先,用于(int j=1;j该程序是一种记忆卡游戏,你必须找到所有匹配的卡对才能获胜,这就是为什么我想制作每张图像的另一份副本,每张卡只有一份副本。是的,我收集了那么多。仍然不起作用或不知道如何做。对不起,我还是一名初学者。你是否更改了你的actionPerformed
方法来解释2D数组?是,但一直说无法在所有图像中找到符号。长度。
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
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,
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]);
}
}