java - 单击 JButton 时更改 JPanel 图标

标签 java swing jpanel jbutton imageicon

JFrame 窗口需要显示随机骰子图像。单击按钮时,随机骰子图像需要更改。我已经弄清楚如何显示随机骰子图像,但我无法弄清楚如何使用 Action 监听器生成新的随机骰子图像。我只上第三节 Java 课,因此我们将不胜感激!

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;
    }
}

最佳答案

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

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;
    }
}

关于java - 单击 JButton 时更改 JPanel 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59005159/

相关文章:

java - JScrollPane 不显示 BorderLayout 约束是否为 CENTER?

java - Android 菜单未出现在 Android Studio 的 "Tools"菜单下。怎么提出来呢?

java - SQL 将日期插入数据库

c# - Java 中 XMLSignature 中的 DigestValue 与 C# 不同

java - 那里有 javax.imageio 的好替代品吗?

java - JTable 在初始化时不显示

java - JFrame 中有多个带背景的 JPanel?

java - 从 Jtable 中删除行时出错

java - 在 Mac OS X 上最小化后 JFrame 不刷新

java - JScrollPane 未显示在继承的 JPanel 上