Varaible in If statement in ActionListener

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Varaible in If statement in ActionListener



I'm making an app, with multiple classes, and in one class i have some code to assign a random number from 1 - 6 to a variable. In another class i have a JLabel showing this variable. And in another class i have a JButton, with a ActionListener, within this is an IF Statement, with the variable with the random number in.



The variable is in 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);





The variable is 'DICEONE'



The JLabel is in Class2 (which rolls the dice).


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







When the button is clicked, the random number is assigned to the variable and then displayed.



Finally the variable is used in the ActionListener in Class3 (this is the bit i have a problem with).


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





The idea is that when the button is clicked, it will check if the variable is equal to a number, and if it is, to change the JLabel text.



But when I click the button nothing changes. The button to roll the dice works fine.





Something will only happen if one of the rolls is equal to 1. Have you debugged to check what they actually are?
– Carcigenicate
Aug 9 at 16:48





Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions.
– halfer
Aug 10 at 11:23





consider adding else block for debugging purpose
– Arvind
Aug 10 at 11:29


else









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard