我正在尝试在Java中创建老虎机。在这台老虎机上,我已经完成了项目的基本开始。我的动画师会在随机的空间(樱桃,空白,七个等),背景,开始和下注的按钮上打赌,但是我必须弄清楚插槽的结尾;如何在没有用户单击按钮的情况下显示投币机的结果。为此,我认为最好的方法是进入线程领域。但是,当我尝试创建一个简单的线程时,动画制作人员停止工作,而当我取出线程时,动画制作人员便开始了工作。这是一个难题。
如果可以帮助说明,请参考以下代码:
public class SlotMachineOpeningGraphic extends JPanel implements Runnable
{
JLayeredPane layeredPane= new JLayeredPane ();
JFrame frame = new JFrame();
public Thread thread;
public volatile boolean running = false;
Player plyr = new Player ();
public static final long startTime = System.currentTimeMillis ();
JLabel numberBet = new JLabel ("" + plyr.getBet()); //things that change on JFrame
JLabel numberAccount = new JLabel (""+ plyr.getAccount ());
JButton betButton = new JButton ("Bet ++");
JButton playAgain = new JButton ("Start");
public SlotMachineOpeningGraphic()
{
layeredPane.setPreferredSize(new Dimension(750, 460));//changes size to image with border for buttons
//andlabels
ImageIcon bG = new ImageIcon ("/Users/Documents/slotmachine.png");//background file
JLabel backGround = new JLabel (bG);
backGround.setBounds (70,0, bG.getIconWidth(), bG.getIconHeight());//won't display if you do not set bounds
layeredPane.add (backGround, new Integer (0));//add to first layer
/*add buttons and labels to give user options
* and information, placed in layer 2 */
playAgain.setBounds (110,420, 100, 25);
layeredPane.add (playAgain, new Integer (2));
playAgain.addActionListener (new Start());
betButton.setBounds (320,420, 100, 25);
layeredPane.add (betButton, new Integer (2));
betButton.addActionListener (new SlotBB ());
JButton mainMenu = new JButton ("Main Menu");
mainMenu.setBounds (520,420, 100, 25);
layeredPane.add (mainMenu, new Integer (2));
long checkTime = System.currentTimeMillis ();
System.out.println ("Total execution time : " + (checkTime - startTime));
JLabel bet = new JLabel ("Bet:");
bet.setBounds (620, 320, 100, 30);
Font font = new Font ("Corsiva Hebrew",bet.getFont().getStyle(),30); //desired font & size
bet.setFont (font);
layeredPane.add (bet, new Integer (2));
numberBet.setBounds (620, 340, 100, 30);
layeredPane.add (numberBet, new Integer (2));
numberBet.setFont (font);
JLabel account = new JLabel ("Account: ");
account.setBounds (5, 320, 150, 30);
account.setFont (font);
layeredPane.add (account, new Integer (2));
numberAccount.setBounds (5, 340, 150, 30);
numberAccount.setFont (font);
layeredPane.add (numberAccount, new Integer (2));
add(layeredPane);
JComponent newContentPane = layeredPane;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.setBackground (Color.white);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void run ()
{
try {
Thread.sleep (20);
System.out.println ("works");
}
catch (Exception e) {
System.out.println ("doesn't work");
}
}
class Start extends JPanel implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
SlotAnimator a0 = new SlotAnimator (40);
a0.setBounds(155, 85, 100, 90);
layeredPane.add (a0, new Integer (1));
SlotAnimator a1 = new SlotAnimator (85);
a1.setBounds(320, 85, 100, 90);
layeredPane.add (a1, new Integer (1));
SlotAnimator a2 = new SlotAnimator (135);
a2.setBounds(470, 85, 100, 90);
layeredPane.add (a2, new Integer (1));
playAgain.setText ("play again?");
hearSound();
thread = new Thread (new SlotMachineOpeningGraphic ());
thread.start ();
}
}
public static void main (String [] args)
{
new SlotMachineOpeningGraphic();
}
}
关于方法有什么建议吗?这将不胜感激!
参考方案
@HFOE对于使用javax.swing.Timer
是正确的,该Model-View-Controller管理其自己的线程以安全的方式生成定期的GUI事件。您可能还想看看采用here模式,例如。
在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…
Java:验证TAR文件 - java是否可以在Java中验证tar文件,以便确保其他tar实用程序(即UNIX tar命令)可以提取tar?我想在代码中执行此操作,而不是从应用程序中实际执行tar -tf并根据返回代码进行报告。我一直在使用Apache Commons compress读取tars条目(而不是提取它们)。我已经使用文本编辑器手动修改了一些tar,以表示“损坏的” tar文件。 …
Java:线程池如何将线程映射到可运行对象 - java试图绕过Java并发问题,并且很难理解线程池,线程以及它们正在执行的可运行“任务”之间的关系。如果我创建一个有10个线程的线程池,那么我是否必须将相同的任务传递给池中的每个线程,或者池化的线程实际上只是与任务无关的“工人无人机”可用于执行任何任务?无论哪种方式,Executor / ExecutorService如何将正确的任务分配给正确的线程? 参考方案 …
JAVA:字节码和二进制有什么区别? - javajava字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…
java:继承 - java有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…