无法从静态上下文引用非静态变量

时间:2014-05-11 16:02:57

标签: java static non-static

我在这里已经阅读了类似的问题,但我仍然无法弄清楚如何修复我的代码。我正在测试一个计时器,虽然我可以将计时器输出放在命令行,而gui在单独的线程中打开,我无法将计时器输出到我名为attArea的JtextArea中。我知道我没有引用导致问题的我的Gui类的实例,但我不知道如何修复我的代码,所以我可以。我甚至尝试在属于Gui类的buildGui方法中实例化我的计时器类。这种方式编译并且计时器正在运行我认为因为从Gui关闭的出口没有在命令行上给我一个新的提示,所以计时器必须仍然在它自己的线程上运行。但它不是在印刷品上印刷的。下面是我的代码(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();
 }
}

第3课

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

2 个答案:

答案 0 :(得分:1)

Gui的引用传递给TimerTest

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);
    }
}

答案 1 :(得分:0)

假设您创建了一个文档,并希望朋友阅读该文档。你是做什么?你给他的文件,对吗?

与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;
   }

   ...
}