2012-03-10 198 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

相关问题