用于对话的GUI应用程序中的Swing Timer

时间:2013-09-26 19:58:01

标签: java swing user-interface timer

好的伙伴,回答另一个问题。为什么我会在计时器周围的第64行获得预期的标识符。我输入了Timer标识符,它向我抱怨。有人请告诉我这段代码有什么问题:

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

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(go);
    c.add(nuke);
    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;  

  }

timer = new Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    timer:stop();
    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.";}
    }
    else if (result == 2)
    {
     timer:start();     
     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...";
     timer.stop();
     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)
  {
    Rps window = new Rps();
    window.setBounds(300, 300, 300, 140);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setVisible(true);
  }
}

1 个答案:

答案 0 :(得分:0)

我不确定这是什么语言,但是这个:

timer:stop();

Java无效的syntext。我想你的意思是

timer.stop();

你是两次,但怀疑你能识别它;)