Hi, I'm new to Java and I am trying to write the code required for function roll to be called when rollButton is pressed and I have no idea how to do this. So I would appreciate if someone could help me. Thanks
[CODE]mport javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
[CODE][CODE][CODE][CODE]public class Dice extends JFrame implements ActionListener {
private JButton rollButton;
private JLabel leftDice,rightDice;
private Random randomGen;
public Dice() {
super("Dice machine");
rollButton= new JButton("Roll!");
leftDice=new JLabel("",JLabel.CENTER);
rightDice=new JLabel("",JLabel.CENTER);
Container pane=this.getContentPane();
pane.add(leftDice,BorderLayout.NORTH);
pane.add(rightDice,BorderLayout.CENTER);
pane.add(rollButton,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,100);
setVisible(true);
randomGen=new Random(); // init number generator
roll();
}
private void roll() {
// generate random numbers between 1 and 6
int left=randomGen.nextInt(5)+1;
int right=randomGen.nextInt(5)+1;
leftDice.setText(""+left);
rightDice.setText(""+right);
}
public void actionPerformed(ActionEvent e){
//not sure what to write here exactly
}
public static void main (String[] args) {
new Dice();