大家好,我是堆栈,所以如果有人可以以任何方式提供帮助,那就太好了。我正在使用eclipse,程序正在编译和运行。我有 3 个类,它们在同一个包中。因此,我想将 ThreadQuizCountdown 类中 i 的值传递给其他类 PanelQuizCountdown int 名为 timeField 的 JTextField 目前 i 显示在控制台中我一直在尝试这样做,但如果有人能帮忙,我就做不到。这是代码
/**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();
}
}