如何使用 actionListener 更改图像标签

问题描述 投票:0回答:1

我正在尝试创建一个简单的骰子 GUI,其中一个骰子值的随机图像将在单击时出现,而不会连续出现两次相同的值。我到了需要在 actionListener 中定义代码的地步,但是单击后,图像不会改变。应该导致图像更改的代码行是

lblNewLabel.setIcon(dice[verifiedNum])
方法中的
defineBtn
但我收到无法解析为类型的错误。我的问题是:为了让 JLabel 随机更改图像而不重复相同的图像,我需要进行哪些更改?我得到的错误是 Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: lblNewLabel 无法解析。


public class LabGuiDice extends JFrame {
    
    final ImageIcon image1 = new ImageIcon(LabGuiDice.class.getResource("/guiDice/img/die-1.png"));

    final ImageIcon image2 = new ImageIcon(LabGuiDice.class.getResource("/guiDice/img/die-2.png"));

    final ImageIcon image3 = new ImageIcon(LabGuiDice.class.getResource("/guiDice/img/die-3.png"));

    final ImageIcon image4 = new ImageIcon(LabGuiDice.class.getResource("/guiDice/img/die-4.png"));

    final ImageIcon image5 = new ImageIcon(LabGuiDice.class.getResource("/guiDice/img/die-5.png"));

    final ImageIcon image6 = new ImageIcon(LabGuiDice.class.getResource("/guiDice/img/die-6.png"));

    final JPanel contentPane;

    final ImageIcon[] dice = { image1, image2, image3, image4, image5, image6 };

    

    final Random rand = new Random();

    int randNum = rand.nextInt(6);

    int verifiedNum = 0;

    

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LabGuiDice frame = new LabGuiDice();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        
    }
        
    /**
     * Create the frame.
     */
    public LabGuiDice() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));
        
        JButton btnNewButton = defineBtn();
        contentPane.add(btnNewButton, BorderLayout.SOUTH);
        
        JLabel lblNewLabel = diceLbl();
        contentPane.add(lblNewLabel, BorderLayout.CENTER);
        
        
        
    }
    
    private JButton defineBtn() {
        JButton btnNewButton = new JButton("Roll 'Em");
        btnNewButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        btnNewButton.setAlignmentY(Component.TOP_ALIGNMENT);
        btnNewButton.setFont(new Font("Javanese Text", Font.PLAIN, 15));
        btnNewButton.setBackground(new Color(178, 172, 136));
        btnNewButton.setForeground(new Color(255, 255, 255));
        btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { 
            do{randNum = rand.nextInt(6);} 
            while (randNum == verifiedNum);
            verifiedNum = randNum;
            lblNewLabel.setIcon(dice[verifiedNum]);
            
            }});
        return btnNewButton;
    }

    

    private JLabel diceLbl() {
        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setOpaque(true);
        lblNewLabel.setBackground(new Color(192, 192, 192));
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setIcon(dice[randNum]);
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        
        return lblNewLabel;
    }

    
}


当我尝试将

lblNewLabel.setIcon(dice[verifiedNum])
放置在
diceLbl
方法中时,单击时图像仍然没有改变。

java user-interface actionlistener
1个回答
0
投票

你有...

private JLabel diceLbl() {
    JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setOpaque(true);
    lblNewLabel.setBackground(new Color(192, 192, 192));
    lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    lblNewLabel.setIcon(dice[randNum]);
    lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    
    return lblNewLabel;
}

这很有趣,但每次调用它时,它都会创建一个新的

JLabel
实例。

然后在构造函数中,你有...

JLabel lblNewLabel = diceLbl();
contentPane.add(lblNewLabel, BorderLayout.CENTER);

定义了一个局部变量

lblNewLabel
,它将(新创建的实例)添加到框架中。

但是,

lblNewLabel
在您的
ActionListener
的上下文中未定义。

首先创建一个实例变量(下面

verifiedNum
的定义)

private JLabel lblNewLabel;

然后修改

diceLbl
方法只创建一个
JLabel
的实例,比如...

    private JLabel diceLbl() {
        if (lblNewLabel == null) {
            lblNewLabel = new JLabel("");
            lblNewLabel.setOpaque(true);
            lblNewLabel.setBackground(new Color(192, 192, 192));
            lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
            lblNewLabel.setIcon(dice[randNum]);
            lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        }
        return lblNewLabel;
    }

然后,在您的

ActionListener
中,您可以简单地做...

diceLbl().setIcon(dice[verifiedNum]);
© www.soinside.com 2019 - 2024. All rights reserved.