我正在制作一款记忆卡游戏。我首先添加了5个具有翻转状态的卡片初始值(图像)的imageicon,添加了一个通过Action Listener翻转卡片的按钮,但当我单击按钮时似乎无法翻转卡片。我还是GUI的初学者,我不想使用applet。
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.*;
//this class gonna control the basic ops of the game
public class MemoControl extends JFrame{
public JLabel label;
public JButton button;
//images
public ImageIcon image1;
public JLabel label1;
public ImageIcon image2;
public JLabel label2;
public ImageIcon image3;
public JLabel label3;
public ImageIcon image4;
public JLabel label4;
public ImageIcon image5;
public JLabel label5;
public MemoControl(){
setLayout(new FlowLayout());
image1 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label1 = new JLabel(image1);
add(label1);
image2 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label2 = new JLabel(image2);
add(label2);
image3 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label3 = new JLabel(image3);
add(label3);
image4 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label4 = new JLabel(image4);
add(label4);
image5 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label5 = new JLabel(image5);
add(label5);
/*label = new JLabel("Welcome to AMY Memo Game");
add(label);*/
/*textField = new JTextField(15);
add(textField);*/
button = new JButton("Flip");
add(button);
EventClass event = new EventClass();
button.addActionListener(event);
}//MyMemo constr end
private class EventClass implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == button){
image1 = new ImageIcon(getClass().getResource("deer_card.jpg"));
label1 = new JLabel(image1);}
}
}//Event class end
public static void main(String args[]){
MemoControl gui = new MemoControl();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);
gui.setTitle("My Memo");
}//main end
}//AMYMemo class end
更新后的代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.*;
//this class gonna control the basic ops of the game
public class MemoControl extends JFrame{
public JLabel label;
public JButton button;
//images
public ImageIcon image1;
public JLabel label1;
public ImageIcon image2;
public JLabel label2;
public ImageIcon image3;
public JLabel label3;
public ImageIcon image4;
public JLabel label4;
public ImageIcon image5;
public JLabel label5;
public MemoControl(){
setLayout(new FlowLayout());
image1 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label1 = new JLabel(image1);
add(label1);
image2 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label2 = new JLabel(image2);
add(label2);
image3 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label3 = new JLabel(image3);
add(label3);
image4 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label4 = new JLabel(image4);
add(label4);
image5 = new ImageIcon(getClass().getResource("card_cover1.jpg"));
label5 = new JLabel(image5);
add(label5);
/*label = new JLabel("Welcome to AMY Memo Game");
add(label);*/
/*textField = new JTextField(15);
add(textField);*/
button = new JButton("Flip");
add(button);
EventClass event = new EventClass();
button.addActionListener(event);
}//MyMemo constr end
private class EventClass implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == button){
image1 = new ImageIcon(getClass().getResource("deer_card.jpg"));
label1.setIcon(image1);
}
}
}//Event class end
public static void main(String args[]){
MemoControl gui = new MemoControl();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);
gui.setTitle("My Memo");
}//main end
}//AMYMemo class end
在EventClass
中尝试label1.setIcon(image1);
代替label1 = new JLabel(image1);
。因为您使用新的Icon
创建了一个新的JLabel
实例,该实例没有添加到您的JFrame
中。
扩展@alex2410的答案,
在java中,你需要理解变量、引用和对象之间的区别。
label1
是一个变量。它是一个变量,可以保存对"JLabel"的引用。new JLabel(..)
创建一个对象并返回一个reference
。
:
label1 = new JLabel()
将新建JLabel
的reference
分配给label1
。
执行add(label1)
时,将label1
的值传递给add
。取值为reference
到之前创建的JLabel
。
当您在此之后将新对象的reference
赋值给label1
时,它不会改变最初传递给add
的对象。因此你的屏幕不会改变。
当你调用label1.setIcon(...)
时,你对label1
指向的对象做了一个改变。该对象恰好是您添加到JFrame中的对象,因此更改该对象将对屏幕进行更改。