- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在为一个项目做老虎机。我无法让我的 JButton
从我的 ArrayList
生成新的随机数。我可以在程序启动时随机化数字并设置 actionlistener
,但它无法满足我的需要。它仅用于测试目的。
我的 Actionlistener
在不同的 Java 文件中。一切正常,我只是不知道如何在按钮按下时在 plc1
、plc2
和 plc3
的占位符中生成新的随机数点击。
我最近才真正开始编写代码,大约 3 周前。请不要讨厌,这是我的第一个项目。
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);
}
}
最佳答案
尝试扩展 GGCGuiLotto
不会做您认为应该做的事情,也就是让您访问相同 实例变量。所以摆脱它。相反,您可以按引用传递,GGCGuiLotto
的当前实例给您的监听器。并有一些 getter 和 setter 来从 GGCGuiLotto
类访问您需要的变量。我的意思可能是这样的(不完全确定你想要完成什么,所以这只是一个例子)。
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.
}
}
当您创建监听器时,将 this
传递给它。 this
是 GGCGuiLotto
一些旁注
Swing 程序不像控制台程序。您不想在 main
方法中执行所有操作。对于初学者,您可以将 main
方法中的代码放入构造函数中。然后在 main
方法中创建一个 GGCGuiLotto
的实例。
Swing 应用程序应该在事件调度线程上运行。参见 Initial Threads
对于您的问题,也许更合适的解决方案是使用 pullSlot
方法创建一个 interface
,您可以在 GGCGuiLotto
类中覆盖该方法只需将 interface
传递给监听器并在 actionPerformed
中调用 pullSlot
方法。像这样
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();
}
}
关于java - 我的 JButton 的 ActionListener 如何访问另一个类中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22205821/
本质上,我编写的是一个益智游戏。 它包含一个图像,该图像进一步分为 9 block ,放置在包含 3x3 JButton GridLayout 的 JPanel 上。最初,9 个按钮是空的。当用户点击
我正在制作一个游戏,其中有一个名为 move(int x1, int y1, int x2, int y2) 的方法,该方法将棋子从第一个按钮 (x1,y1) 移动并将其放置在第二个按钮 (x2, y
我使用 NetBeans 创建了一个框架。该框架有两个按钮 A 和 B。按钮 A 最初被禁用。仅当单击按钮 B 时才启用。 public newFrame() { //newFrame is t
我想使用 MouseListener 从按钮矩阵中删除某个按钮,并在空白处添加 JLabel,所以我使用: import java.awt.*; import java.awt.event.*; im
我正在创建一个黑白棋游戏,想知道如何在单击时将按钮文本从 B 更改为 W,反之亦然。 我尝试将 Action 监听器添加到我的 forloop 中,这为游戏创建了我的板,当我单击标记为 W 的按钮时,
我的项目中有 7 个按钮。其中有 6 个类别,RandomSoru 按钮是随机选择其中一个类别的按钮。我想访问所选类别。 “r”是随机生成器。 RandomSoru.addActionListener
我对此感到好奇,因为我们正在 Swing 中制作游戏,并且出于某种原因将 map 图 block 制作为 jButtons 而不是 jPanels。现在我们想将单位放在它们上面,这样当单位位于它们上面
我创建了一个按钮数组: JButton bt[][]=new JButton[8][8]; 然后我通过以下方式调用一个名为 refreshBoard 的函数 public void refreshBo
我想在这篇文章的序言中告诉大家,这是我第一次发帖,所以如果有任何错误,请告诉我。 我正在尝试为我的高中计算机数学(编程)课编写一款二十一点游戏,但出现了很多错误,但真正令人烦恼的是这个错误。 我的 G
我正在创建一个“谁想成为百万富翁”游戏,并创建了一个半按钮,我想使用它来删除两个 JButton 答案。以下是两个作为答案选项的 JButton 的代码。 enter code here: Answe
我有一个 JButton,我想在按下它时创建一个新的 JButton,我添加了一个如下所示的 ActionListener,但它没有添加另一个 JButton。 public void actionP
如果在 JTextBox 中输入字符串,然后按“输入按钮”,它会在 JTextField 中返回正确的数据,如果我更改字符串,然后重新按输入,也会返回正确的数据。如果我按“输入按钮”然后按“添加”按钮
我想在单击另一个 jbutton 时获得一个 jbutton。 Here the link for sample code(Log in as jbutton,asdf as a password)
假设我创建了一个带有 jbuttons 的 2d tile map ,然后在 map 顶部创建了单位,当单位(也是一个 jbutton)位于 tile 顶部时,有没有办法显示 map 的背景,因为如何
我正在开发一个项目,我需要单击一个按钮来创建另一个按钮。最终,我希望对新按钮的位置有更多的控制,并能够多次创建新按钮,但现在......我希望只让一个 JButton 创建另一个 JButton。 使
JPanel 中有 12 个 JButton。我想单击其中一个并禁用所有其他(它将“突出显示”)。然后,如果我单击禁用的一个,则前一个将被禁用,并且过程是相同的。考虑到有 12 个按钮并且我想避免重复
我正忙于用 Java 制作游戏。然而,我正在努力让 GUI 正常工作。 我遇到的问题: 当按下按钮时,我希望它显示一个彩色圆圈,当再次按下按钮时,我希望它在旧圆圈周围画一个圆圈,这可能会发生,直到每个
大家好,我正在尝试使用 Java 创建一个简单的国际象棋游戏。我有一个 [8][8] 数组的 JButton 板。我为每个分配一个新的 SquareListener,它是一个扩展 BoardListn
我有 JButtons“暂停”和“取消暂停”。当用户暂停程序时,应禁用“暂停”按钮,并启用“取消暂停”按钮。我不知道怎么写。取消暂停按钮有效,但暂停按钮不起作用,因为“无法解决取消暂停”。怎么处理呢?
我设置了一个网格布局,中间有 16 个按钮。我在第一个按钮上放置了一个图标。 我将如何循环,当用户选择网格上的下一个按钮时,它将图标从旧位置 move 到新位置? private ArrayList
我是一名优秀的程序员,十分优秀!