2014-05-11 98 views
0

我已經閱讀過類似的問題,但我仍然無法弄清楚如何修復我的代碼。我正在測試一個計時器,雖然我可以把定時器輸出放在命令行中,而gui在單獨線程中打開,但我無法將計時器的輸出放到我的JtextArea中,名爲attArea。我明白我不是在引用導致問題的Gui類的實例,但我不知道如何修復我的代碼,所以我可以。我甚至嘗試在屬於Gui類的buildGui方法中實例化我的timer類。這種方式編譯和計時器正在運行,我認爲,因爲從桂關閉出口不給我一個新的命令行提示,所以計時器必須仍然運行在它自己的線程。但是它不在印刷機中印刷。下面是我的代碼(3班)非靜態變量不能從靜態上下文引用

class Main{ 
    public static void main(String[] args){ 
    new Gui(); 
    new TimerTest(); 
    } 
    } 

下一個類

import javax.swing.*; 
import java.awt.*; 
class Gui extends JFrame{ 
    int fieldWidth = 20; 
    String[] choose = {"Choose"}; 
    JPanel pnlBack = new JPanel(); 
    JPanel topPanel = new JPanel(); 
    JPanel citiesPanel = new JPanel(); 
     JLabel citiesLabel = new JLabel("Cities: "); 
     JComboBox citiesBox = new JComboBox(choose); 
     //citiesBox.add("choose"); 
    JPanel typePanel = new JPanel(); 
     JLabel typeLabel = new JLabel("Type: "); 
     JComboBox typeBox = new JComboBox(choose); 
    JPanel namePanel = new JPanel(); 
     JLabel nameLabel = new JLabel("Name: "); 
     JComboBox nameBox = new JComboBox(choose); 
    JPanel midPanel = new JPanel(); 
    JPanel attPanel = new JPanel(); 
     JTextArea attArea = new JTextArea(12, 50); 
    JPanel bottomPanel = new JPanel(); 
    JPanel gasPricePanel = new JPanel(); 
     JLabel gasPriceLabel = new JLabel("    Price of gas: "); 
     JTextField gasPriceBox = new JTextField(fieldWidth); 
    JPanel mpgPanel = new JPanel(); 
     JLabel mpgLabel = new JLabel("   mpg of vehicle: "); 
     JTextField mpgBox = new JTextField(fieldWidth); 
    JPanel moneyPanel = new JPanel(); 
     JLabel moneyLabel = new JLabel("  Money you have:"); 
     JTextField moneyBox = new JTextField(fieldWidth); 
    JPanel costPanel = new JPanel(); 
     JLabel costLabel = new JLabel("     Cost of trip: "); 
     JTextField costBox = new JTextField(fieldWidth); 
    JPanel distancePanel = new JPanel(); 
     JLabel distanceLabel = new JLabel(" Distance you can go: "); 
     JTextField distanceBox = new JTextField(fieldWidth); 

Gui(){ 
    buildGui(); 
} 

//method to buld the gui 
void buildGui(){ 
    this.setSize(600,500); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setTitle("Travel Calc"); 
    this.setResizable(false); 
    this.setVisible(true); 
    //System.out.println("buiding the gui."); 
    pnlBack.setLayout(new BoxLayout(pnlBack, BoxLayout.Y_AXIS)); 
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS)); 
    topPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 
    midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS)); 
    midPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 
    bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS)); 
    bottomPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 
    //add back panel to Jframe 
    add(pnlBack); 
    //add panels to back panel 
    pnlBack.add(topPanel); 
     topPanel.add(citiesPanel); 
      citiesPanel.add(citiesLabel); 
      citiesPanel.add(citiesBox); 
     topPanel.add(typePanel); 
      typePanel.add(typeLabel); 
      typePanel.add(typeBox); 
     topPanel.add(namePanel); 
      namePanel.add(nameLabel); 
      namePanel.add(nameBox);  
    pnlBack.add(midPanel); 
     midPanel.add(attPanel); 
      attPanel.add(attArea); 
    pnlBack.add(bottomPanel); 
     bottomPanel.add(gasPricePanel); 
      gasPricePanel.add(gasPriceLabel); 
      gasPricePanel.add(gasPriceBox); 
     bottomPanel.add(mpgPanel); 
      mpgPanel.add(mpgLabel); 
      mpgPanel.add(mpgBox); 
     bottomPanel.add(moneyPanel); 
      moneyPanel.add(moneyLabel); 
      moneyPanel.add(moneyBox); 
     bottomPanel.add(costPanel); 
      costPanel.add(costLabel); 
      costPanel.add(costBox); 
      costBox.setEditable(false); 
     bottomPanel.add(distancePanel); 
      distancePanel.add(distanceLabel); 
      distancePanel.add(distanceBox); 
      distanceBox.setEditable(false); 

     // add connection method goes here 
     // new TimerTest(); 
} 
} 

第三類

import java.util.Timer; 
import java.util.TimerTask; 


public class TimerTest { 

static int counter = 0; 

public TimerTest(){ 

     TimerTask timerTask = new TimerTask() { 

     @Override 
     public void run() { 
      Gui.attArea.append("TimerTask executing counter is: " + counter); 
      //System.out.println("TimerTask executing counter is: " +  counter); 
      counter++;//increments the counter 
     } 
    }; 

    Timer timer = new Timer("MyTimer");//create a new Timer 

    timer.scheduleAtFixedRate(timerTask, 0, 3000);//this line starts the timer at the same time its executed 
} 
} 

回答

1

傳遞給TimerTestGui參考

class TimerTest { 

    static int counter = 0; 

    public TimerTest(final Gui gui) { 

     TimerTask timerTask = new TimerTask() { 

      @Override 
      public void run() { 
       gui.attArea.append("TimerTask executing counter is: " + counter); 
       ... 
      } 
     }; 
     ... 
    } 
} 

class Main { 
    public static void main(String[] args) { 
     Gui gui = new Gui(); 
     new TimerTest(gui); 
    } 
} 
0

讓我們SA您創建文檔,並希望朋友閱讀文檔。你是做什麼?你給他的文件,對吧?

與Java對象相同:您有一個GUI,並且您希望Timer訪問GUI。所以你給GUI定時器:

class Main{ 
    public static void main(String[] args){ 
     Gui theGui = new Gui(); 
     new TimerTest(theGui); 
    } 
} 

現在,TimerTest構造函數接收它將不得不訪問的gui。它只需要在一個字段中保留對這個gui的引用:

public class TimerTest { 

    private Gui theGui; 

    public TimerTest(Gui gui) { 
     this.gui = gui; 
    } 

    ... 
} 
相關問題