图像堆叠在一起

时间:2014-02-17 23:41:33

标签: java image swing button stack

我正在完成我的DiceRollGUI,当我运行它并按" Roll"彼此重叠的图像堆栈,如何再次按下按钮后它会消失?

我完成了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(); 
            }
    }
 }

2 个答案:

答案 0 :(得分:0)

按照您的代码,每次调用actionPerformed时,似乎都会添加一个新的JLabel。 将Jlabel添加到JPanel后,您必须删除前一个:

panel.removeAll()

答案 1 :(得分:0)

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

您可能会发现在BorderLayout上使用GridBagLayoutcontentPane会产生很好的效果。您可能还想调查horizontalAligmentverticalAligment属性

更新了可编辑示例

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