[英]How can my ActionListener for a JButton access variables in another class?
I am doing a slot machine for a project. 我正在为一个项目做老虎机。 I am having trouble getting my JButton
to generate new random numbers from my ArrayList
. 我无法让JButton
从ArrayList
生成新的随机数。 I can randomize the numbers when the program starts and have an actionlistener
set up, but it doesn't do what I need. 我可以在程序启动时将数字随机化,并设置一个动作actionlistener
器,但是它不能满足我的需求。 It was merely for testing purposes. 它仅用于测试目的。
My Actionlistener
is in a different Java file. 我的Actionlistener
位于另一个Java文件中。 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. 一切正常,我只是想不出如何在单击按钮时在plc1
, plc2
和plc3
的占位符中生成新的随机数。
I have just recently began to really code as about 3 weeks ago. 我刚刚在大约3周前才真正开始编写代码。 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);
}
}
Trying to extends GGCGuiLotto
isn't going to do what you think it's supposed to, which is give you access to the same instance variables. 尝试extends GGCGuiLotto
并不会做您认为应该做的事情,这使您可以访问相同的实例变量。 So get rid of that. 所以摆脱它。 Instead you can pass-by-reference , the current instance of GGCGuiLotto
to your listener. 相反,您可以通过引用将GGCGuiLotto
的当前实例GGCGuiLotto
给您的侦听器。 And have some getters and setters to access the variables you need from the GGCGuiLotto
class. 并具有一些getter和setter来访问GGCGuiLotto
类中所需的变量。 What I mean is maybe something like this (not completely sure what you're trying to accomplish, so this is just an example). 我的意思可能是这样的事情(不能完全确定您要完成的工作,因此仅是示例)。
public class GenPLCListener implements ActionListener {
private GGCGuiLotto lotto;
public GenPLCListener(GGCGuiLotto lotto) {
this.lotto = lotto;
}
@Override
public void actionPerfomred(ActionEvent e) {
List<ImageIcon> slotList = lotto.getSlotList();
Collections.shuffle(slotList); // shuffle the list
// do something else if need be.
}
}
When you create the listener, pass this
to it. 当您创建的侦听器,通过this
给它。 this
being the instance of GGCGuiLotto
this
是GGCGuiLotto
的实例
Few Side Notes 很少注意事项
Swing programs aren't like console programs. Swing程序与控制台程序不同。 You don't want to do everything inside your main
method. 您不想在main
方法中做任何事情。 For starters, the code in your main
method, you could put in the constructor instead. 首先,您可以将main
方法中的代码放入构造函数中。 Then create an instance of GGCGuiLotto
in the main
method. 然后在main
方法中创建一个GGCGuiLotto
实例。
Swing apps should be run on the Event Dispatch Thread. Swing应用程序应在事件调度线程上运行。 See Initial Threads 请参阅初始线程
Maybe a more proper solution to your problem would be to have an interface
with the pullSlot
method that you can override in the GGCGuiLotto
class and just pass the interface
to the listener and call the pullSlot
method inf your actionPerformed
. 也许您的问题更妥善的解决办法是有一个interface
与pullSlot
方法,你可以在覆盖GGCGuiLotto
类,只是通过interface
来监听器和调用pullSlot
方法INF你actionPerformed
。 Something like this 像这样
public interface PullInterface {
public void pullSlot();
}
public class GGCGuiLotto implements PullInterface {
ArrayList<ImageIcon> slotList = new ArrayList<>(); // global scope.
JLabel aReel1lbl = new JLabel();
JLabel bReel2lbl = new JLabel();
JLabel cReel3lbl = new JLabel();
Random rand = new Random();
public GGCGuiLotto() {
GenPLCListener listener = new GenPLCListener(this);
}
@Override
public void pullSlot() {
// do what you need to do here to implement a pulling of the lever
int r1 = rand.nextInt(slotList.size());
int r2 = rand.nextInt(slotList.size());
int r3 = rand.nextInt(slotList.size());
reel1lbl.setIcon(slotList.get(r1));
}
}
public class GenPLCListener implement ActionListener {
private PullInterface pull;
public GenPLCListener(PullInterface pull) {
this.pull = pull;
}
@Override
public void actionPerformed(ActionEvent e) {
pull.pullSlot();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.