繁体   English   中英

将变量从一个类传递到另一个类

[英]pass variable from one class to another

大家好,我是堆叠,所以如果任何人都可以以任何方式提供帮助,那就太棒了。 我正在使用eclipse,程序正在编译和运行。 我有3个课程,他们在同一个包中。 所以我想将类ThreadTizCountdown中的i值传递给其他类PanelQuizCountdown,在JTextField中使用名称timeField当前我在控制台中显示我一直在尝试这样做,但我不能这样,如果有人可以伸出援助之手。 这是代码

/**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();
    }
}

使用委托,添加到符合接口的委托类的begin()方法参数,例如

interface DelegationInterface {
   void countdownTick(int i);
}

在ThreadQuizCountdown中:添加私有字段并修改begin方法:

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
    }
    ....
}

而已!

当您可以选择使用标签时,在文本字段中显示倒计时并不是一个好习惯。 无论如何,我调试了你的代码,在应用下面的步骤后,它将以你想要的方式运行。

ThreadQuizCountdown类中,在run()方法的while循环中,添加此行

timeField.setText( i +"" );

它将时间值设置为文本字段,这是您第一次明显缺失。 您可以在try-catch块之前添加此行。

其次,删除这一行: timeField = new JTextField(26); 从同一个while循环中,每次将文本字段分配给新对象都是愚蠢的。

应用这些将使您的工作完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM