2014-05-11 97 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; 
    } 

    ... 
} 
相关问题