Java JButton的ActionListener如何访问另一个类中的变量?
我正在为一个项目做老虎机。我无法让我的Java JButton的ActionListener如何访问另一个类中的变量?,java,swing,jframe,jbutton,actionlistener,Java,Swing,Jframe,Jbutton,Actionlistener,我正在为一个项目做老虎机。我无法让我的JButton从我的ArrayList生成新的随机数。我可以在程序启动时随机化这些数字,并设置一个actionlistener,但它不能满足我的需要。这仅仅是为了测试目的 我的Actionlistener位于不同的Java文件中。一切正常,我只是不知道当点击按钮时如何在plc1、plc2和plc3的占位符中生成新的随机数 我最近才真正开始编写代码,大约是在3周前。 请不要憎恨,这是我的第一个项目 尝试扩展GGCGuiLotto并不能达到您认为应该达到的效果,
JButton
从我的ArrayList
生成新的随机数。我可以在程序启动时随机化这些数字,并设置一个actionlistener
,但它不能满足我的需要。这仅仅是为了测试目的
我的Actionlistener
位于不同的Java文件中。一切正常,我只是不知道当点击按钮时如何在plc1
、plc2
和plc3
的占位符中生成新的随机数
我最近才真正开始编写代码,大约是在3周前。
请不要憎恨,这是我的第一个项目
尝试
扩展GGCGuiLotto
并不能达到您认为应该达到的效果,即允许您访问相同的实例变量。所以把它扔掉。相反,您可以通过引用将GGCGuiLotto
的当前实例传递给您的侦听器。并使用一些getter和setter从GGCGuiLotto
类访问所需的变量。我的意思可能是这样的(不完全确定你想要完成什么,所以这只是一个例子)
在中,通过
URL
访问发布的图像,如图所示;如图所示使用合成图像;或者使用UIManager
图标,如图所示。我只想指出,将变量命名为pic1
,pic2
,pic3
和plc1
,plc2
,plc3
有点令人困惑。您可能会考虑不这样做。我想做的是,当按下按钮时,它会从数组列表中生成随机数到AeLe1LBL。就像是一台老虎机。因此,每次单击按钮时,plc1 2和3个位置会出现3个新图像。您可以在GGCGuiLotto
类中使用一个pullReel
方法来执行洗牌。然后在侦听器中,只需调用lotto.pullReel
。似乎完整的答案超出了您发布的问题的范围。但这就是如何访问其他实例变量。我建议您尝试并解决pullReel
方法,如果您遇到问题,请在此处发布另一个问题。我已经在此处设置了它,它似乎正在工作。剩下的唯一部分是pullSlot方法()。不知道如何实现它,所以它会从数组中生成一组新的图像到标签中的占位符中。您似乎已经知道如何实现它,因为您第一次使用它。您有三个随机数(实际上应该是nextInt(7)
,因为您有7个图像),然后将图标设置为标签。在方法中做完全相同的事情。您可能遇到的问题是范围界定。我不知道您是如何声明所有变量的,但是如果它们是在构造函数中声明的,那么它们是本地范围的,不能在其他地方访问。因此,您需要给它们一个全局范围(在构造函数之外声明它们),以便可以从方法访问它们。请参阅编辑,谢谢大家的帮助。对这个项目有100%的评价。
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);
}
}
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.
}
}
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();
}
}