Change photo when you click a button

前端 未结 1 776
青春惊慌失措
青春惊慌失措 2021-01-28 11:31
import javax.swing.Icon;
import javax.swing.ImageIcon;


public class Stage1 extends javax.swing.JFrame {


    int score = 0;
    int iter = 1;


    public Stage1() {
         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-28 12:17

    You never call pic.setIcon(...) on the JLabel within your ActionListener. You only call it once within the Stage1 constructor, and so the JLabel's Icon will never change. The solution is to call this method within the listener.

    Your problem is one of "magical thinking", thinking that if you change the object that a variable refers to, all other references to the object will change as well, but that's not how Java works. When you change the Icon that ic refers to, this has no effect on the current object displayed in the JLabel. You have to write code to change it yourself.

    0 讨论(0)
提交回复
热议问题