gpt4 book ai didi

Java 线程问题

转载 作者:行者123 更新时间:2023-12-03 23:06:57 24 4
gpt4 key购买 nike

我正在尝试用 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 是正确的,它管理自己的线程以安全的方式生成周期性的 GUI 事件。您可能还想看看采用 Model-View-Controller图案,示例 here .

关于Java 线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com