java - 图像堆叠在一起

标签 java image swing button stack

我正在完成我的 DiceRollGUI,当我运行它并按“滚动”彼此顶部的图像堆栈时,如何使其在再次按下按钮后消失?

我做了 MadProgrammer 告诉我的大部分内容,但遇到了以下错误:

--------------------Configuration: <Default>--------------------
G:\DiceRollGUI\src\DiceRollGUI.java:26: error: constructor RollButton in class RollButton cannot be applied to given types;
    button.addActionListener(new RollButton(diceRoll));
                             ^
required: JPanel
found: JLabel
reason: actual argument JLabel cannot be converted to JPanel by method invocation conversion
G:\DiceRollGUI\src\DiceRollGUI.java:42: error: cannot find symbol
        contentPane.add(diceRoll);
        ^
symbol:   variable contentPane
location: class RollButton
2 errors

Process completed.

Dice Stack

代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;

public class DiceRollGUI {
    public static JLabel label;
    public static JFrame frame;
    public static JPanel panel;
    private static JButton button;
    private static JButton buttonRollDie;
    private static JLabel diceRoll;

public static void main (String[] args) {
    JFrame frame = new JFrame("Dice Roll GUI");
    panel = new JPanel();
    JPanel contentPanel = new JPanel(new GridLayout(0,2,5,10));
    button = new JButton("Roll");;

    frame.setVisible(true);
    frame.setResizable(false);
    frame.setSize(1000, 1000);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    button.setActionCommand("Roll");
    button.addActionListener(new RollButton(diceRoll));
    button.setAlignmentX(Component.CENTER_ALIGNMENT);
    panel.setPreferredSize(new Dimension(750, 500));
    frame.setContentPane(panel);
    frame.pack();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    panel.add(button);
}

    private static class RollButton implements ActionListener {

    private JPanel diceRoll;

    public RollButton(JPanel diceRoll){
        this.diceRoll = diceRoll;
        contentPane.add(diceRoll);
    }           
        public void actionPerformed(ActionEvent event){
            int roll = (int) (Math.round((Math.random() * 5) + 1));
            ImageIcon dice = null;

                if(roll == 1){
                    dice = new ImageIcon("DiceRollGUI Pictures/die_face_1.png");
                }
                else if(roll == 2){
                    dice = new ImageIcon("DiceRollGUI Pictures/die_face_2.png");
                }
                else if(roll == 3){
                    dice = new ImageIcon("DiceRollGUI Pictures/die_face_3.png");
                }
                else if(roll == 4){
                    dice = new ImageIcon("DiceRollGUI Pictures/die_face_4.jpeg");
                }
                else if(roll == 5){
                    dice = new ImageIcon("DiceRollGUI Pictures/die_face_5.png");
                }
                else if(roll == 6){
                    dice = new ImageIcon("DiceRollGUI Pictures/die_face_6.png");
                }
                JLabel diceRoll = new JLabel("",dice, JLabel.CENTER);
                panel.add(diceRoll);
                panel.revalidate(); 
            }
    }
 }

最佳答案

1- 在构造函数中,创建 diceRoll JLabel 的实例并将其添加到 panel

diceRoll = new JLabel();
contentPane.add(diceRoll);

2- 不要将 contentPane 的引用传递给 RollButton ActionListener,而是传递对 diceRoll 的引用> 相反...

button.addActionListener(new RollButton(diceRoll));

3-更改您的 RollButton ActionListener 以支持使用 JLabel 而不是 JPanel...

private static class RollButton implements ActionListener {

    private JLabel diceRoll;

    public RollButton(JLabel diceRoll){
        this.diceRoll= diceRoll;
    }     

4- 在您的 actionPerformed 方法中,只需更改 diceRoll 的图标属性...

public void actionPerformed(ActionEvent event){
    int roll = (int) (Math.round((Math.random() * 5) + 1));
    ImageIcon dice = null;
    //...
    diceRoll.setIcon(dice);
}

您可能会发现在 contentPane 上使用 BorderLayoutGridBagLayout 会产生很好的效果。您可能还想研究 horizo​​ntalAligmentverticalAligment 属性

更新了可编译示例

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DiceRollGUI {

    public static JLabel label;
    public static JFrame frame;
    public static JPanel panel;
    private static JButton button;
    private static JButton buttonRollDie;
    private static JLabel diceRoll;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Dice Roll GUI");
        panel = new JPanel();
        JPanel contentPanel = new JPanel(new GridLayout(0, 2, 5, 10));
        button = new JButton("Roll");

        // !! Add this !! //
        diceRoll = new JLabel();
        contentPanel.add(diceRoll);

        frame.setVisible(true);
        frame.setResizable(false);
        frame.setSize(1000, 1000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.setActionCommand("Roll");
        button.addActionListener(new RollButton(diceRoll));
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.setPreferredSize(new Dimension(750, 500));
        frame.setContentPane(panel);
        frame.pack();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        panel.add(button);
    }

    private static class RollButton implements ActionListener {

        // !! Change This !! //
        private JLabel diceRoll;

        // !! Change This !! //
        public RollButton(JLabel diceRoll) {
            // !! Change This !! //
            this.diceRoll = diceRoll;
        }

        public void actionPerformed(ActionEvent event) {
            int roll = (int) (Math.round((Math.random() * 5) + 1));
            ImageIcon dice = null;

            if (roll == 1) {
                dice = new ImageIcon("DiceRollGUI Pictures/die_face_1.png");
            } else if (roll == 2) {
                dice = new ImageIcon("DiceRollGUI Pictures/die_face_2.png");
            } else if (roll == 3) {
                dice = new ImageIcon("DiceRollGUI Pictures/die_face_3.png");
            } else if (roll == 4) {
                dice = new ImageIcon("DiceRollGUI Pictures/die_face_4.jpeg");
            } else if (roll == 5) {
                dice = new ImageIcon("DiceRollGUI Pictures/die_face_5.png");
            } else if (roll == 6) {
                dice = new ImageIcon("DiceRollGUI Pictures/die_face_6.png");
            }
            // !! Change This !! //
            diceRoll.setIcon(dice);
        }
    }
}

5-您实际上从未将 contentPane 添加到任何内容...更改您的 main 使其看起来更像..

public static void main(String[] args) {
    JFrame frame = new JFrame("Dice Roll GUI");
    panel = new JPanel();
    JPanel contentPanel = new JPanel(new GridLayout(0, 2, 5, 10));
    button = new JButton("Roll");

    // !! Add this !! //
    diceRoll = new JLabel();
    contentPanel.add(diceRoll);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    button.setActionCommand("Roll");
    button.addActionListener(new RollButton(diceRoll));
    button.setAlignmentX(Component.CENTER_ALIGNMENT);
    panel.setPreferredSize(new Dimension(750, 500));
    panel.setLayout(new BorderLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    panel.add(button, BorderLayout.NORTH);
    panel.add(contentPanel);
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
}

关于java - 图像堆叠在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21841545/

相关文章:

java - 我如何用空行对 java JTable 进行排序并强制空行始终排在最后?

java - 制作更新的 JLabel

java - 不同类中不同类型的数据

java - 如何知道何时停止 InputStream 缓冲

c# - 以编程方式将一组图像连接在一起以形成背景

html - 如何为外部托管的照片指定缩略图的大小?

java - JTable 列宽仅在两次请求后更改

java - Java 使用哪种序列化格式?

java - 我如何在 Java 中比较字符串?

java - 图像在一个全新的 jframe 中?