簡體   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