我正在尝试在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();
}
}
有关方法的任何建议吗?非常感谢!
答案 0 :(得分:3)
这至少是你的一个错误:
thread = new Thread(new SlotMachineOpeningGraphic());
您正在创建一个新的SlotMachineOpeningGraphic对象,该对象与原始对象完全不同。不要这样做。如果你必须在这里使用SlotMachineOpeningGraphic对象,那么你应该使用对已经存在的原始类的引用并保存Start private内部类,例如:
thread = new Thread(SlotMachineOpeningGraphic.this);
所以这不是一个“线程”问题本身,而是一个引用问题 - 你使用错误的引用作为你的线程的Runnable。
但是我强烈建议你不要在ActionListeners,OtherListeners或Runnables中使用你的GUI类,因为你要求类做太多。最好使用匿名内部类或单独的独立类。
答案 1 :(得分:3)
@HFOE正确使用javax.swing.Timer
,它管理自己的线程以安全的方式生成定期的GUI事件。您可能还希望考虑采用Model-View-Controller模式,例如here。
答案 2 :(得分:1)
您可以尝试使用Future或Runnable类进行处理,并在完成后立即检索结果。只需确保将这些线程放在线程池中即可!