在ActionListener中的If语句中可变

时间:2018-08-09 16:44:31

标签: java variables jframe jbutton jlabel

我正在制作一个具有多个类的应用程序,在一个类中,我有一些代码可以为变量分配1到6的随机数。在另一个类中,我有一个JLabel显示此变量。在另一个类中,我有一个带有ActionListener的JButton,它是一个IF语句,其中的变量带有随机数。

该变量在Class1中。

package Dice;

public class DiceRoll {

public int DICEONE;
public int DICETWO;

private int max = 6;
private int min = 1;

public void DiceRollMethod() {

    DICEONE = (int) (Math.floor(Math.random() * (max - min + 1)) + min);
    DICETWO = (int) (Math.floor(Math.random() * (max - min + 1)) + min);

    System.out.println(DICEONE);
    System.out.println(DICETWO);

}
}

变量为'DICEONE'

JLabel在Class2中(掷骰子)。

package Dice;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DiceButton extends JPanel{

    private static final long serialVersionUID = 1L;

    DiceRoll roll = new DiceRoll();

    JButton btnRoll;
    public JLabel DB1, DB2;

    public DiceButton() {
        DB1 = new JLabel("Dice 1: " + roll.DICEONE);
        DB2 = new JLabel("Dice 2: " + roll.DICETWO);
        DB1.setVisible(true);
        DB2.setVisible(true);

        btnRoll = new JButton("Roll Dice");
        btnRoll.setVisible(true);
        btnRoll.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                roll.DiceRollMethod();
                DB1.setText("Dice 1: " + roll.DICEONE);
                DB2.setText("Dice 2: " + roll.DICETWO);

            }

        });
        add(DB1);
        add(DB2);
        add(btnRoll);


    }

}

单击按钮时,随机数将分配给变量,然后显示。

最后,该变量在Class3的ActionListener中使用(这是我遇到的问题)。

package Tiles;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import Dice.DiceButton;
import Dice.DiceRoll;

public class SpaceOne extends JPanel{

    private static final long serialVersionUID = 1L;

    DiceRoll roll = new DiceRoll();
    DiceButton dButton = new DiceButton();

    JButton btn1;
    JLabel lbl1;



    Font lblFont = new Font("Helvetica", Font.BOLD, 90);
    Border lblBorder = BorderFactory.createLineBorder(Color.BLACK, 3);

    public SpaceOne() {

        setSize(50,100);
        setLayout(new GridLayout(1,2));

        lbl1 = new JLabel("1");
        lbl1.setVisible(true);
        lbl1.setFont(lblFont);
        lbl1.setBorder(lblBorder);
        lbl1.setLocation(1, 1);


        btn1 = new JButton("1");
        btn1.setVisible(true);
        btn1.setLocation(1, 2);
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                if(roll.DICEONE == 1) {
                    lbl1.setText(".");
                    System.out.println("DICEONE");
                }else if(roll.DICETWO == 1) {
                    lbl1.setText(".");
                    System.out.println("DICETWO");
                }

            }

        });
        add(btn1);
        add(lbl1);
    }

}

这个想法是,当单击按钮时,它将检查变量是否等于数字,如果是,则更改JLabel文本。

但是当我单击按钮时,没有任何变化。掷骰子的按钮工作正常。

0 个答案:

没有答案