Java 线程问题

发布于 2024-12-08 13:07:14 字数 3926 浏览 0 评论 0原文

我正在尝试用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();
 }
}

对方法有什么建议吗?我们将不胜感激!

I am trying to create a slot machine in java. In this slot machine I have completed the basic beginning of the project. I have the animators that lands on a random space (cherry, blank, seven, etc), the background, buttons to start and bet in the opening, however I have to figure out the ending of the slot; how to make the results of the slot machine appear without the user clicking a button. To do this I figure the best way is to dive into the realm of threads. However when I tried to create a simple thread, the animators stopped working and when I took the thread out, the animators did work. It is quite the conundrum.
Here is some of the code if that helps explanation :

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();
 }
}

Any suggestions on an approach? It would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽月 2024-12-15 13:07:14

@HFOE 关于使用 javax.swing 是正确的.Timer,它管理自己的线程以安全的方式生成周期性的GUI事件。您可能还想考虑采用 Model-View-Controller 模式,举例这里

@HFOE is right about using javax.swing.Timer, which manages its own thread to generate periodic GUI events in a safe way. You may also want to look at adopting the Model-View-Controller pattern, exemplified here.

但可醉心 2024-12-15 13:07:14

这至少是您的错误之一:

thread = new Thread(new SlotMachineOpeningGraphic());

您正在创建一个新的 SlotMachineOpeningGraphic 对象,该对象与原始对象完全不同。不要这样做。如果您必须在此处使用 SlotMachineOpeningGraphic 对象,那么您应该使用对已存在并保存 Start 私有内部类的原始类的引用,例如:

thread = new Thread(SlotMachineOpeningGraphic.this);

因此,这本身不是“线程”问题,而是一个引用问题——您使用了错误的引用作为线程的 Runnable。

但我强烈建议您不要将 GUI 类用于 ActionListeners、OtherListeners 或 Runnables,因为您要求类做太多事情。最好使用匿名内部类或单独的独立类。

This is at least one of your mistakes:

thread = new Thread(new SlotMachineOpeningGraphic());

You are creating a new SlotMachineOpeningGraphic object, one that is completely distinct from the original one. Don't do this. If you had to use a SlotMachineOpeningGraphic object here, then you should use a reference to the original class that already exists and holds the Start private inner class, such as:

thread = new Thread(SlotMachineOpeningGraphic.this);

So this isn't a "threading" issue per se, but rather a reference issue -- you use the wrong reference as the Runnable for your thread.

But I strongly advise you against using your GUI classes for ActionListeners, OtherListeners or Runnables as you're asking the class to do too much. Better to use an anonymous inner class or a separate stand-alone class.

゛时过境迁 2024-12-15 13:07:14

您可以尝试使用 Future 或 Runnable 类进行处理,并在完成后立即检索结果。只要确保将这些线程放入线程池即可!

You could try to use a Future or Runnable classes for doing the processing, and retriving their result as soon as they finish. Just make sure to put those threads in a Thread pool!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文