2012-03-10 197 views
1

大家好我是堆棧,所以如果有人能以任何方式提供幫助,那就太好了。我正在使用eclipse,程序正在編譯和運行。我有3班,他們在同一個包。所以我想將類ThreadQuizCountdown中的值傳遞給其他類PanelQuizCountdown int名稱爲timeField的JTextField當前我在控制檯中顯示我一直在嘗試這樣做,但我不能這樣如果任何人都可以伸出援助之手。下面是代碼將變量從一個類傳遞到另一個類

/**The driver class of the program. Here is the JFrame 
* class name RunQuizCountdown.java 
* @author Kiril Anastasov 
* @date 09/03/2012 
*/ 

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

public class RunQuizCountdown 
{ 
    public static void main(String[] args) 
    { 

     JFrame application = new JFrame(); 
     PanelQuizCountdown panel = new PanelQuizCountdown(); 
     application.add(panel); 
     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     application.setSize(200,300); 
     application.setLocationByPlatform(true); 
     application.setVisible(true); 
    } 

} 



/** Here is the GUI of the program 
* class name PanelQuizCountdown.java 
* @author Kiril Anastasov 
* @date 09/03/2012 
*/ 

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

public class PanelQuizCountdown extends JPanel implements ActionListener 
{ 
    JTextField timeField, answerField; 
    JLabel messageLabel, correctLabel, totalLabel; 
    int x, y; 
    int correct; 
    int total; 

    ThreadQuizCountdown myQuiz; 

    PanelQuizCountdown() 
    { 
     timeField = new JTextField(5); 
     myQuiz = new ThreadQuizCountdown(timeField); 
     this.add(timeField); 
     myQuiz.begin(); 


     messageLabel = new JLabel("What is the result of " + x + " * " + y); 
     this.add(messageLabel); 

     answerField = new JTextField(5); 
     this.add(answerField); 

     correctLabel = new JLabel("You gave : " + correct + " correct answers"); 
     this.add(correctLabel); 

     totalLabel = new JLabel("You answered: " + total + " questions"); 
     this.add(totalLabel); 





    } 


    public void actionPerformed(ActionEvent ae) 
    { 

    } 
} 

/** Here is the thread of the program 
* class name ThreadQuizCountdown.java 
* @author Kiril Anastasov 
* @date 09/03/2012 
*/ 

import javax.swing.JTextField; 

public class ThreadQuizCountdown implements Runnable 
{ 
    JTextField timeField; 
    Thread myThread = new Thread(this); 

    int i = 30; 
    boolean go = true; 

    ThreadQuizCountdown(JTextField theTimeField) 
    { 
     timeField = theTimeField; 
    } 

    public void run() 
    { 


     while(go) 
     {   
      System.out.println(i);  

      try 
      { 
       myThread.sleep(1000);   
      } 
      catch (InterruptedException ie) 
      { 
       System.out.println("thread exception"); 
      } 

      timeField = new JTextField(26); 

      if(i == 0) 
      { 
       go = false; 
      } 
      i--; 
     } 

    } 

    public void begin() 
    { 
     myThread.start(); 
    } 

    public void finish() 
    { 
     myThread.stop(); 
    } 
} 

回答

2

使用代表團,添加到您的委託類的開始()方法的參數符合接口,像

interface DelegationInterface { 
    void countdownTick(int i); 
} 

在ThreadQuizCountdown: 增加私人領域和修改開始方法:

private DelegationInterface delegate; 

public void begin(DelegationInterface delegate) { 
    this.delegate = delegate; 
    myThread.start(); 
} 

接下來,修改的run():(我們稱之爲倒計時的關鍵部分,在這種情況下,它並不重要,但如果你將有許多計時器通知,它將有助於避免出現問題)

public void run() { 
.... 
    myThread.sleep(1000); 
    if (delegate != null) { 
     synchronized(delegate) { 
      delegate.countdownTick(i); 
     } 
    } 
.... 
} 

最後,增加實現的接口到面板:

public class PanelQuizCountdown extends JPanel implements ActionListener, DelegationInterface { 
    .... 
    public void countdownTick(int i) { 
     // place i to to timeField 
    } 
    .... 
} 

這就是它!

+0

большоеспасибо – Kiril 2012-03-10 18:10:11

+0

你WELCOM)пожалуйста – Antigluk 2012-03-10 18:11:43

2

在您可以選擇使用標籤的情況下,在文本字段中顯示倒計時並不是一種好的做法。無論如何,我調試了你的代碼,並在應用了下面的步驟之後,它會以你想要的方式運行。

ThreadQuizCountdown類,在run()方法的while循環,加入這一行

timeField.setText(i +""); 

其中設置時間值的文本框,這是你的第一個明顯的缺失。您可以在try-catch塊之前添加此行。

其次,從相同的while循環中刪除此行:timeField = new JTextField(26);,將文本字段分配給每個新對象都很愚蠢。

應用這些將使您的工作完成。

+0

我認爲這是在UI類好習慣設置文本用戶界面領域,而不是線程。 – Antigluk 2012-03-10 18:20:16

+0

我做到了,它給我的主題「線程1」顯示java.lang.NullPointerException \t在ThreadQuizCountdown.run(ThreadQuizCountdown.java:30) \t在java.lang.Thread.run(螺紋==>異常.java:722) – Kiril 2012-03-10 18:25:22

+0

那麼如何更新UI字段呢? – Juvanis 2012-03-10 18:25:38

相關問題