Our goal is to create a slot machine with 2 panels and it seemed that the button is not working and not displaying anything in the text field on the panel. The text field in the panel should display the random number generator when the button is clicked. I tried the Action Listener code but it is not doing anything as well. Please help! Thank you!
package slotMachineMain;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class slot1 extends JFrame implements ActionListener {
JButton button1;
JTextField text1;
slot1(){
Image icon = Toolkit.getDefaultToolkit().getImage("D:\\11.png");
ImageIcon image1 = new ImageIcon("D:\\12.png");
ImageIcon image2 = new ImageIcon("D:\\13.gif");
ImageIcon image3 = new ImageIcon("D:\\14.png");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Take your Chance Here!");
JLabel label1 = new JLabel();
label1.setIcon(image1);
label1.setBounds(120, 10, 138, 87);
JLabel label2 = new JLabel();
label2.setIcon(image2);
label2.setBounds(0, 0, 400, 290);
JTextField text1 = new JTextField();
text1.setPreferredSize(new Dimension(80,80));
text1.setEditable(false);
JTextField text2 = new JTextField();
text2.setPreferredSize(new Dimension(80,80));
text2.setEditable(false);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.white);
panel1.setBounds(83, 100, 90, 90);
panel1.add(text1);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.setBounds(207, 100, 90, 90);
panel2.add(text2);
JButton button1 = new JButton();
button1.setBounds(152, 200, 75, 25);
button1.setIcon(image3);
button1.addActionListener(this);
this.setResizable(false);
this.setSize(400, 290);
this.setLayout(null);
this.setIconImage(icon);
this.add(panel1);
this.add(panel2);
this.add(button1);
this.add(label1);
this.add(label2);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Random rn = new Random();
if(e.getSource()==button1) {
text1.setText(Integer.toString(rn.nextInt()));
}
}
}