I am doing a slot machine for a project. I am having trouble getting my JButton
to generate new random numbers from my ArrayList
. I can randomize the numbers when the program starts and have an actionlistener
set up, but it doesn't do what I need. It was merely for testing purposes.
My Actionlistener
is in a different Java file. Everything works, I just can't figure out how to generate new randoms in the placeholder of plc1
, plc2
, and plc3
when the button is clicked.
I have just recently began to really code as about 3 weeks ago.
No haters please, this is my first project ever.
package GGCGuiLotto;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Image;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.Random;
public class GGCGuiLotto {
public static void main(String[] args) {
//Arraylist of images
ImageIcon pic0 = new ImageIcon("pics/pic1.png");
ImageIcon pic1 = new ImageIcon("pics/pic2.png");
ImageIcon pic2 = new ImageIcon("pics/pic3.png");
ImageIcon pic3 = new ImageIcon("pics/pic4.png");
ImageIcon pic4 = new ImageIcon("pics/pic5.png");
ImageIcon pic5 = new ImageIcon("pics/pic6.png");
ImageIcon pic6 = new ImageIcon("pics/pic7.png");
final ArrayList<ImageIcon> slotlist = new ArrayList<ImageIcon>();
slotlist.add(pic0);
slotlist.add(pic1);
slotlist.add(pic2);
slotlist.add(pic3);
slotlist.add(pic4);
slotlist.add(pic5);
slotlist.add(pic6);
Random ran = new Random();
int plc1 = ran.nextInt(4);
int plc2 = ran.nextInt(4);
int plc3 = ran.nextInt(4);
//generates the frame and the labels are added.
JFrame frame = new JFrame();
frame.setSize (400,275);
frame.setTitle("GGC Lotto Slots Rcorbin");
frame.setResizable(false);
frame.setVisible(true);
JPanel pnlReels = new JPanel();
frame.add(pnlReels);
JPanel aReel1 = new JPanel();
aReel1.setBackground(new Color(25,25,112));
aReel1.setBounds(15,10,100,100);
JPanel bReel2 = new JPanel();
bReel2.setBackground(new Color(25,25,112));
bReel2.setBounds(145,10,100,100);
JPanel cReel3 = new JPanel();
cReel3.setBackground(new Color(25,25,112));
cReel3.setBounds(275,10,100,100);
pnlReels.add(aReel1);
pnlReels.add(bReel2);
pnlReels.add(cReel3);
JLabel aReel1lbl = new JLabel();
JLabel bReel2lbl = new JLabel();
JLabel cReel3lbl = new JLabel();
aReel1.add(aReel1lbl);
bReel2.add(bReel2lbl);
cReel3.add(cReel3lbl);
aReel1lbl.setIcon(slotlist.get(plc1));
bReel2lbl.setIcon(slotlist.get(plc2));
cReel3lbl.setIcon(slotlist.get(plc3));
//jbutton
JButton slotbtn1 = new JButton();
slotbtn1.setText("GGC LOTTO Click ME");
pnlReels.add(slotbtn1);
slotbtn1.setBounds(145,50,100,75);
//FirstGuiListener act = new FirstGuiListener();
//slotbtn1.addActionListener((ActionListener) act);
GenPLCListener genPLC = new GenPLCListener();
slotbtn1.addActionListener((genPLC));
{}
if (plc1 == plc2 && plc1 == plc3 && plc2 == plc3)
{
JOptionPane.showConfirmDialog(null,"Winner! Play Again? ","GGC Lotto Slots RCorbin ",JOptionPane.YES_NO_OPTION);
//System.out.println("Winner");
}
else
{
//JOptionPane.showMessageDialog(null,"No Winner Winner Chicken Dinner ! ");
System.out.println("Crazy"); }
}
}
package GGCGuiLotto;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JOptionPane;
class GenPLCListener extends GGCGuiLotto implements ActionListener{
public void actionPerformed(ActionEvent event){
System.out.println("Works");
JOptionPane.showConfirmDialog(null,"Choose Wisely. ","Click If you Trust!",JOptionPane.YES_NO_OPTION);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…