Java 如何为骰子制作动作侦听器按钮?

Java 如何为骰子制作动作侦听器按钮?,java,swing,jbutton,actionlistener,dice,Java,Swing,Jbutton,Actionlistener,Dice,我正在做一个棋盘游戏,我需要操作一个像骰子一样运行的按钮。在我的框架中,我有掷骰子按钮,旁边是一个JLabel,它将显示骰子的结果。问题是我在另一个类中有一个骰子编码,在另一个类中有一个按钮。我有一个action类,它将保存action listener的代码。我如何实现它?下面是我的代码 GameView类: 骰子等级: 行动类: 在这样一个简单的例子中,我建议您使用匿名类的优点,它实现了ActionListener,如下所示 BorderLayout borderlayout = n

我正在做一个棋盘游戏,我需要操作一个像骰子一样运行的按钮。在我的框架中,我有掷骰子按钮,旁边是一个JLabel,它将显示骰子的结果。问题是我在另一个类中有一个骰子编码,在另一个类中有一个按钮。我有一个action类,它将保存action listener的代码。我如何实现它?下面是我的代码

GameView类: 骰子等级: 行动类:
在这样一个简单的例子中,我建议您使用匿名类的优点,它实现了
ActionListener
,如下所示

    BorderLayout borderlayout = new BorderLayout();      

    //Creates the frame, set the visibility to true, sets the size and exit on close
    JFrame frame = new JFrame("Games");
    frame.setVisible(true);
    frame.setSize(600,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    //Creates the Throw Dice button
    Panel p = new Panel();
    p.setLayout(new GridLayout());

    final Dice dice = new Dice();
    Button button = new Button("Throw Dice");
    final Label label = new Label("Dice Draw");

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dice.randomGenerator();
            label.setText(Integer.toString(dice.getDiceResult()));
        }
    });

    p.add(button);
    p.add(label); //This is where I want the dice result to be shown when hit the button
    p.setBackground(new Color(156, 93, 82));
    frame.add(p, BorderLayout.SOUTH);

此外,您还必须将
getDiceResult
方法添加到
Dice
类中。

要更快获得更好的帮助,请发布您的最佳尝试。
public class Dice
{   
    //stores the dice result in the variable
    private int diceResult;

    /**
     * Constructor for objects of class Dice
     */
    public Dice()
    {
        this.diceResult = diceResult;
    }

    /**
     * Generates a random number between 1 and 5
     */
    public void randomGenerator ()
    {
        Random dice = new Random();
        diceResult = dice.nextInt(5)+1;
        System.out.println(diceResult);
    }
}
public class Action
{
    public void actionPerformed (ActionEvent e) 
    {
    }
}
    BorderLayout borderlayout = new BorderLayout();      

    //Creates the frame, set the visibility to true, sets the size and exit on close
    JFrame frame = new JFrame("Games");
    frame.setVisible(true);
    frame.setSize(600,400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    //Creates the Throw Dice button
    Panel p = new Panel();
    p.setLayout(new GridLayout());

    final Dice dice = new Dice();
    Button button = new Button("Throw Dice");
    final Label label = new Label("Dice Draw");

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dice.randomGenerator();
            label.setText(Integer.toString(dice.getDiceResult()));
        }
    });

    p.add(button);
    p.add(label); //This is where I want the dice result to be shown when hit the button
    p.setBackground(new Color(156, 93, 82));
    frame.add(p, BorderLayout.SOUTH);