我正在使用 NetBeans 创建此程序。
我使用 MyMain 使用 FrameDemo 创建 JForm。
然后我将 JPanel (SensorUI SensorLL=new SensorUI) 添加到 JForm。
(请注意,我对此进行了一些简化,实际上我添加了 JPanel 的四个实例。)
面板包括一个标签“传感器”。
最终我的程序将运行一个计时器,每秒一次对传感器进行采样并在传感器标签中显示采样值。
我在传感器中有一个名为 write(value) 的方法,它将在传感器中写入(值)。
我的问题是面板的创建被 NetBeans 放置在“run()”方法中(我假设)。我可以:
Sensor.write("xxx");
如果在 run() 花句内则成功,
但如果我把
Sensor.write("xxx");
在运行卷发之外,我得到了
cannot find symbol symbol: variable Sensor location: class 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);
}
最佳答案
您在错误的范围内声明了变量“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
}
您无法从运行方法外部访问它,因为在您的代码中它仅存在于运行方法内部。
编辑 我尝试更改您的 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);
}
}
如果我没猜错的话,这应该完全符合你的要求。由于缺少类,我无法测试代码,因此如果发现任何错误,请说出来,我会尝试相应地更新代码。
关于java - 在 "run()"方法之后我无法访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21816628/