简体   繁体   English

摇摆计时器GUI噩梦

[英]Swing Timer GUI Nightmare

Okay so I tried implementing a timer into the dialogue of this program which pauses for a second before moving on to the next bit of dialogue. 好的,所以我尝试在该程序的对话中实现一个计时器,该计时器会暂停一秒钟,然后再继续进行下一个对话。 When I try this out, java spits out errors o' plenty such as: illegal start of expression, ; 当我尝试这种方法时,java会吐出很多错误,例如:非法的表达式开始; expected, .class expected, and reached end of file while parsing. 预期,.class预期,并在解析时到达文件末尾。 How can I implement the timer so the window doesn't freeze when I try to have the dialogue on the screen for a specific amount of time? 当我尝试在屏幕上显示特定时间段的对话框时,如何实现计时器以使窗口不冻结? And don't tell me Thread.sleep() cause I tried that and all it does is freeze the application. 而且不要告诉我Thread.sleep(),因为我尝试过,它所做的只是冻结应用程序。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class RpsNuke extends JFrame
    implements ActionListener
{
  private final char moves[] = {'R', 'P', 'S', 'N'};
  private JRadioButton rock, paper, scissors, nuke;
  private JTextField display;

  public RpsNuke()
  {
    super("Rock, Paper, Scissors, Nuke");

    rock = new JRadioButton("   Rock   ", true);
    paper = new JRadioButton("   Paper  ");
    scissors = new JRadioButton(" Scissors ");
    nuke = new JRadioButton(" Nuke ");
    ButtonGroup rpsButtons = new ButtonGroup();
    rpsButtons.add(rock);
    rpsButtons.add(paper);
    rpsButtons.add(scissors);
    rpsButtons.add(nuke);

    JButton go = new JButton("         Go         ");
    go.addActionListener(this);

    display = new JTextField(25);
    display.setEditable(false);
    display.setBackground(Color.yellow);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.add(rock);
    c.add(paper);
    c.add(scissors);
    c.add(nuke);
    c.add(go);
    c.add(display);
    if (nuke.isSelected()){
    display.setText("Don't do it man");}
    else {
    display.setText("");}
  }

  /**
   *  returns -1 if the player wins,
   *  0 if it's a tie, and 1 if the computer wins
   */
  private int nextPlay(char computerMove, char playerMove)
  {
    if ((computerMove == 'R'&&playerMove == 'S')||(computerMove == 'S'&&playerMove=='P')||(computerMove=='P'&&playerMove=='R')){
     return 1;}
    else if ((computerMove == 'R'&&playerMove == 'R')||(computerMove=='S'&&playerMove=='S')||(computerMove=='P'&&playerMove=='P')){
     return 0;}
    else if (playerMove == 'N'){
     return 2;}
    return -1;  

  }

  public void actionPerformed(ActionEvent evt)
  {
    char playerMove, computerMove;
    playerMove = 0;
    if (rock.isSelected()){
      playerMove = 'R';}
    else if (paper.isSelected()){
      playerMove = 'P';}
    else if (scissors.isSelected()){
      playerMove = 'S';}
    else if (nuke.isSelected()){
      playerMove = 'N';}
    int k = (int)(Math.random() * 3);
    computerMove = moves[k];
    int result = nextPlay(computerMove, playerMove);
    String msg = "";
    if (result != 2)
    {msg = "  You said " + makeWord(playerMove) + ", I said " +
                 makeWord(computerMove);
    if (result < 0){

      msg += " -- you win.";}
    else if (result == 0){

      msg += " -- tie.";}
    else if (result > 0){
      msg += " -- I win.";}
    }
    if (result == 2)
    {
     TimerTask tasknew = new TimerScheduleFixedRateDelay();
     Timer timer = new Timer();

      // scheduling the task at fixed rate delay
     timer.scheduleAtFixedRate(tasknew,1000,1000);
     @Override
    }
     // this method performs the task

     public void run() {
      msg = "It's too late, we're all dead!";    
      msg = "...";

      msg = "Look at what you did, there's nothing left.";

      msg = "Looks like we have to start over again...";
      window.setVisible(false);
      main(null);                       
    }          
    display.setText(msg);
  }

  private String makeWord(char move)
  {
    String word = "";

    switch (move)
    {
      case 'R': word = "rock"; break;
      case 'P': word = "paper"; break;
      case 'S': word = "scissors"; break;
      case 'N': word = "nuke"; break;
    }
    return word;
  }

  public static void main(String[] args) //Here
  {
    RpsNuke window = new RpsNuke();
    window.setBounds(300, 300, 400, 140);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setVisible(true);
  }
} //And here

This seems to be the start of your problems... 这似乎是您问题的开始...

if (result == 2) {
    TimerTask tasknew = new TimerScheduleFixedRateDelay();
    Timer timer = new Timer();
    // scheduling the task at fixed rate delay
    timer.scheduleAtFixedRate(tasknew, 1000, 1000);
    @Override
}

public void run() {

There's no such class called TimerScheduleFixedRateDelay , Timer doesn't have a default constructor, you don't seem to have declared tasknew and I have no idea why @Override appears here... 没有这样的类TimerScheduleFixedRateDelayTimer没有默认的构造函数,您似乎没有声明tasknew ,我也不知道为什么@Override出现在这里...

Let's start by commenting those out for the moment 让我们首先评论一下那些

if (result == 2) {
    //TimerTask tasknew = new TimerScheduleFixedRateDelay();
    //Timer timer = new Timer();
    // scheduling the task at fixed rate delay
    //timer.scheduleAtFixedRate(tasknew, 1000, 1000);
    //@Override
}

public void run() {

The next problem is you're missing a closing bracket... 下一个问题是您缺少右括号...

if (result == 2) {
    ...
 }

 // Something is amiss here...

public void run() {

It should look more like... 它看起来应该更像...

    if (result == 2) {
        ...
     }

}

public void run() {

Next... 下一个...

public void run() {
    msg = "It's too late, we're all dead!";
    msg = "...";

    msg = "Look at what you did, there's nothing left.";

    msg = "Looks like we have to start over again...";
    window.setVisible(false);
    main(null);
}

display.setText (msg);

msg is undefined, window is undefined and main is undefined and display.setText is being callout outside of a method context, which is illegal... msg未定义, window未定义, main未定义, display.setText在方法上下文之外被调出,这是非法的。

public void run() {
    String msg = "It's too late, we're all dead!";
    msg = "...";

    msg = "Look at what you did, there's nothing left.";

    msg = "Looks like we have to start over again...";
    display.setText (msg);
    //window.setVisible(false);
    //main(null);
}

It's easy enough to fix the msg , not sure about the display.setText , but since it's taking msg , I assume it belongs in the run method and I have no idea about window and main still needs to be resolved... 修复msg很容易,不确定display.setText ,但是由于它使用msg ,因此我认为它属于run方法,并且我不了解window并且main仍需要解决...

Which leads us onto... 这导致我们进入...

}

private String makeWord(char move)
  {
    String word = "";

    switch (move)
    {
      case 'R': word = "rock"; break;
      case 'P': word = "paper"; break;
      case 'S': word = "scissors"; break;
      case 'N': word = "nuke"; break;
    }
    return word;
  }

  public static void main(String[] args) //Here
  {
    RpsNuke window = new RpsNuke();
    window.setBounds(300, 300, 400, 140);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setVisible(true);
  }
} //And here

Oh, I think I found where our missing brace went to....This means that everything below the } the first brace in the above code is actually being defined outside of the class , which, for the context of this question, is illegal... 哦,我想我发现在那里我们缺少的支柱去....这意味着下的所有内容}在上面的代码中的第一支撑实际上已经在外面定义的class ,其中,对于这个问题的背景下,是非法的...

So, lets comment that out... 所以,让我们评论一下...

//}

private String makeWord(char move)
  {
    String word = "";

    switch (move)
    {
      case 'R': word = "rock"; break;
      case 'P': word = "paper"; break;
      case 'S': word = "scissors"; break;
      case 'N': word = "nuke"; break;
    }
    return word;
  }

  public static void main(String[] args) //Here
  {
    RpsNuke window = new RpsNuke();
    window.setBounds(300, 300, 400, 140);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setVisible(true);
  }
} //And here

There, things look a "little" better...I also think I found window , but it's being defined in main ...not very helpful ;) 那里的情况看起来更好“一点” ...我也认为我找到了window ,但是它是在main中定义的...不是很有帮助;)

Based on look at your code, I think you should avoid java.util.Timer and use javax.swing.Timer instead, this will prevent you from updating the UI from outside the content of the Event Dispatching Thread which is just another cup of worms you would probably wish to avoid... 根据您的代码,我认为您应该避免使用java.util.Timer ,而应使用javax.swing.Timer ,这将防止您从事件分发线程的内容之外更新UI,这只是另一种蠕虫您可能希望避免...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM