2013-09-26 31 views
0

好吧,所以我試圖實現一個計時器進入這個程序的對話,暫停一秒鐘,然後移動到下一個對話位。當我嘗試這樣做時,java吐出大量錯誤,例如:表達式的非法開始,預計,.class預期,並在解析時達到文件結尾。我如何實現計時器,以便在我嘗試在屏幕上進行特定時間的對話時,窗口不會凍結?並不要告訴我Thread.sleep()因爲我試過了,它所做的只是凍結應用程序。擺動計時器GUI噩夢

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 
+0

和錯誤層層疊疊都是....... – KevinDTimm

+0

@KevinDTimm我編輯它,因此錯誤顯示 – Showman

+0

這可能只不過是一個粗心的錯誤更多。你有計算你的花括號嗎?是否所有的開口花括號都預計會匹配右花括號?我認爲你可能會留下一兩個。 –

回答

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

有沒有叫TimerScheduleFixedRateDelay這樣的類,Timer沒有一個默認的構造函數,你似乎沒有宣佈tasknew,我不知道爲什麼會出現@Override這裏......

讓我們通過註釋者淘汰的那一刻

啓動

下一個問題是你缺少一個右括號...

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方法和我不知道有關windowmain仍然需要解決......

這使我們在......

} 

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.Timer並使用javax.swing.Timer而不是這樣做,這將阻止您從Event Dispatching Thread的內容之外更新UI,這是您可能希望避免的另一個蠕蟲...

+3

+1哇,我已經開始寫一個答案,但沒有計劃任何這個徹底。很好,我足夠慢了。 – kiheru

+0

@kiheru不知道是否我抓住了所有東西,所以如果你還有其他東西要添加,不要猶豫,把另一個答案放在一起,這是一個真正的混亂,所以我很可能錯過了一些東西:P – MadProgrammer

+0

沒有什麼添加。 '@ Override'和'public void run()'可能是TimerTask的一部分,它不知何故被分散了一些(並且被一個不存在的構造函數調用)。但正如所說的,OP不應該使用java.util.Timer。 – kiheru