单击 JButton 时更改 JPanel 图标

Changing JPanel Icon when JButton is clicked

JFrame window 需要显示随机骰子图像。单击该按钮时,随机骰子图像需要更改。我已经想出如何显示随机骰子图像,但我不知道如何使用 actionlistener 生成新的随机骰子图像。我只有第三个 Java class,所以任何指导将不胜感激!

package guiDice;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;

public class LabGuiDice extends JFrame {

    private JPanel contentPane;

    /**
     * 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));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JButton btnRollem = newDiceRoll();
        contentPane.add(btnRollem, BorderLayout.SOUTH);

        JLabel lblDice = newDiceImage();
        contentPane.add(lblDice, BorderLayout.CENTER);
    }

    private JLabel newDiceImage() {
        Random rnd = new Random();
        int rand1 = 0;
        rand1 = rnd.nextInt(6)+1;
        JLabel lblDice = new JLabel("");
        switch (rand1) {
        case 1:
            lblDice.setHorizontalAlignment(SwingConstants.CENTER);
            lblDice.setIcon(new ImageIcon(LabGuiDice.class.getResource("/Dice/die-1.png")));
            break;
        case 2:
            lblDice.setHorizontalAlignment(SwingConstants.CENTER);
            lblDice.setIcon(new ImageIcon(LabGuiDice.class.getResource("/Dice/die-2.png")));
            break;
        case 3:
            lblDice.setHorizontalAlignment(SwingConstants.CENTER);
            lblDice.setIcon(new ImageIcon(LabGuiDice.class.getResource("/Dice/die-3.png")));
            break;
        case 4:
            lblDice.setHorizontalAlignment(SwingConstants.CENTER);
            lblDice.setIcon(new ImageIcon(LabGuiDice.class.getResource("/Dice/die-4.png")));
            break;
        case 5:
            lblDice.setHorizontalAlignment(SwingConstants.CENTER);
            lblDice.setIcon(new ImageIcon(LabGuiDice.class.getResource("/Dice/die-5.png")));
            break;
        case 6:
            lblDice.setHorizontalAlignment(SwingConstants.CENTER);
            lblDice.setIcon(new ImageIcon(LabGuiDice.class.getResource("/Dice/die-6.png")));
            break;
        }
        return lblDice;
    }

    private JButton newDiceRoll() {
        JButton btnRollem = new JButton("Roll 'Em");
        btnRollem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });
        btnRollem.setBorderPainted(false);
        btnRollem.setFont(new Font("Bodoni 72 Smallcaps", Font.PLAIN, 27));
        btnRollem.setOpaque(true);
        btnRollem.setBackground(new Color(255, 0, 0));
        return btnRollem;
    }
}

创建一个生成整数并将图标设置为标签的方法。但为了做到这一点,label 应该是 class 中的一个字段,因此所有方法都可以访问它。例如:

private void rollDice() {
    Random random = new Random();
    int randomInt = random.nextInt(6) + 1;
    String resource = String.format("/Dice/die-%d.png", randomInt);
    Icon icon = new ImageIcon(LabGuiDice.class.getResource(resource));
    diceIconLabel.setIcon(icon);
}

然后:

btnRollem.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        rollDice();
    }
});

完整代码:

public class LabGuiDice extends JFrame {

    private JPanel contentPane;
    private JLabel diceIconLabel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            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));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JButton btnRollem = newDiceRoll();
        contentPane.add(btnRollem, BorderLayout.SOUTH);

        diceIconLabel = newDiceImage();
        contentPane.add(diceIconLabel, BorderLayout.CENTER);
        rollDice();
        pack();
    }

    private void rollDice() {
        Random random = new Random();
        int randomInt = random.nextInt(6) + 1;
        String resource = String.format("/Dice/die-%d.png", randomInt);
        Icon icon = new ImageIcon(LabGuiDice.class.getResource(resource));
        diceIconLabel.setIcon(icon);
    }

    private JLabel newDiceImage() {
        JLabel lblDice = new JLabel("");
        lblDice.setHorizontalAlignment(SwingConstants.CENTER);
        return lblDice;
    }

    private JButton newDiceRoll() {
        JButton btnRollem = new JButton("Roll 'Em");
        btnRollem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                rollDice();
            }
        });
        btnRollem.setBorderPainted(false);
        btnRollem.setFont(new Font("Bodoni 72 Smallcaps", Font.PLAIN, 27));
        btnRollem.setOpaque(true);
        btnRollem.setBackground(new Color(255, 0, 0));
        return btnRollem;
    }
}