“run()”方法后,我无法访问对象

时间:2014-02-16 21:07:19

标签: java swing

我正在使用NetBeans创建此程序 我使用MyMain使用FrameDemo创建JForm。 然后我将一个JPanel(SensorUI SensorLL = new SensorUI)添加到JForm。 (注意我对此进行了简化,实际上我添加了四个JPanel实例。)

面板包含标签“传感器”。

最终我的程序将运行一个计时器,每秒一次,它将对传感器进行采样并在传感器标签中显示采样值。

我在Sensor中有一个名为write(value)的方法,它将在Sensor中写入(值)。

我的问题是,NetBeans创建的Panel被置于“run()”方法中(我假设)。我可以: Sensor.write("xxx");如果它在run()卷曲中成功, 但是,如果我放置 Sensor.write("xxx"); 我得到的是卷曲之外的

  

无法找到符号   符号:变量传感器   位置:类MyMain

我的代码如下。我删除了很多不必要的信息,希望不要太多。 如果这意味着什么,每个类都在一个单独的NetBeans类文件中。 出于某种原因,当我查看预览时,会出现阴影,导致类的早期行不在阴影中。我不知道最后的帖子是否会以这种方式出现。如果是,抱歉。

public class MyMain {

public static void main(String[] args) {

    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            FrameDemo.createAndShowGUI();
            SensorUI SensorLL=new SensorUI("SensorLL");
            FrameDemo.addObject(SensorLL);
            SensorLL.writeSensor("xxx");//<<<<<This is OK
        }
    }
    );

            SensorLL.writeSensor(SensorLL, "xxx");//<<<This is not
}

public class FrameDemo {

  /**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
static        JFrame frame = new JFrame("BrewTool");  

public FrameDemo(){

}
static void createAndShowGUI() {
    //Create and set up the window.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.pack();

    frame.setVisible(true);

}
static void addObject(SensorUI Sensor){
    frame.add(Sensor);

}
}

public class SensorUI extends javax.swing.JPanel {

/**
 * Creates new form SensorUI
 */
public SensorUI(String title) {
    initComponents();
    Temperature.setBorder(javax.swing.BorderFactory.createTitledBorder(title));
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">

从此商家信息中删除了大量NetBeans“生成代码” Jpanel包含一个名为“Sensor”的JLabel

public void writeSensor(String value){
    Sensor.setText(value);
}

1 个答案:

答案 0 :(得分:1)

您将变量'SensorLL'声明的范围错误:

public class MyMain {

    public static void main(String[] args) {

    // Declare it in the main-method
    SensorUI SensorLL = null;

    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            FrameDemo.createAndShowGUI();

            // Here you can simply initialize it, because it's
            // already declared
            SensorLL=new SensorUI("SensorLL");
            FrameDemo.addObject(SensorLL);
            SensorLL.writeSensor("xxx");//<<<<<This is OK
        }
    }
    );

    // Works perfectly fine, because SensorLL is declared
    // outside the run-method
    SensorLL.writeSensor(SensorLL, "xxx");//<<<This is not
}

您无法从run-method外部访问它,因为在您的代码中它只存在于run-method中。

修改 我试图改变你的SensorUI类来获得所希望的行为。我想出了这个

public class SensorUI extends javax.swing.JPanel {

private Thread updateThread = null;
private boolean updateThreadIsRunning = false;

/**
 * Creates new form SensorUI
 */
public SensorUI(String title) {

    // I can't figure out what those do (mainly because of missing source code).
    initComponents();
    Temperature.setBorder(javax.swing.BorderFactory.createTitledBorder(title));

    // Create a Thread in each SensorUI which is started once the Component is
    // created and then updates the JLabel Sensor each second
    // Now each SensorUI has its own Thread which keeps updating the content of
    // the corresponding JLabel

    this.updateThread = new Thread(new Runnable() {

        @Override
        public void run() {

                updateThreadIsRunning = true;

                while(updateThreadIsRunning){
                    // The message which is shown on the Sensor JLabel
                    writeSensor("xxx");
                    try{
                        // Here you can change the update intervall in milliseconds
                        Thread.sleep(1000);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }

        }
    });

    this.updateThread.start();

}

// Here are the leftout lines

public void writeSensor(String value){
    Sensor.setText(value);
}
}

如果我说得对,那应该完全按照自己的意愿行事。我无法测试代码,因为缺少类,所以如果发现任何错误只是说些什么,我会尝试相应地更新代码。