0

好的,所以我尝试在该程序的对话中实现一个计时器,该计时器在继续下一个对话之前暂停一秒钟。当我尝试这个时,java会吐出很多错误,例如:非法的表达式开始,; 预期,.class 预期,并在解析时到达文件末尾。如何实现计时器,以便当我尝试在屏幕上显示特定时间的对话时窗口不会冻结?不要告诉我 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
4

1 回答 1

4

这似乎是你问题的开始......

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() {

没有这样的类TimerScheduleFixedRateDelayTimer没有默认构造函数,你似乎没有声明tasknew,我不知道为什么会@Override出现在这里......

让我们暂时先评论那些

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() {

下一个问题是您缺少右括号...

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

 // Something is amiss here...

public void run() {

它应该看起来更像...

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

}

public void run() {

下一个...

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是未定义的,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);
}

修复 很容易msg,不确定display.setText,但由于它正在msg使用 ,我认为它属于run方法,我不知道window并且main仍然需要解决......

这导致我们...

}

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

哦,我想我找到了我们丢失的大括号的位置......这意味着}上面代码中第一个大括号下面的所有内容实际上都是在 之外定义的class,对于这个问题的上下文来说,这是非法的.. .

所以,让我们评论一下...

//}

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

在那里,事情看起来“有点”好......我也想我找到window了,但它被定义在main......不是很有帮助;)

根据查看您的代码,我认为您应该避免java.util.Timerjavax.swing.Timer改为使用,这将阻止您从事件调度线程的内容之外更新 UI,这只是您可能希望避免的另一杯蠕虫...

于 2013-09-26T21:48:29.350 回答