I have a class that creates a JFrame. When the JFrame is created it has a start button. When the start button is clicked, it runs two threads until the stop button is clicked. The two threads are in another class file. From the class that contains the threads, how can I access the JFrame instance in order to change value that are displayed?
我有一个创建JFrame的类。创建JFrame时,它有一个开始按钮。单击开始按钮时,它将运行两个线程,直到单击停止按钮。这两个线程在另一个类文件中。从包含线程的类,如何访问JFrame实例以更改显示的值?
3 个解决方案
#1
1
In order to acheive this you have to pass the reference of JFrame using this keyword.
为了实现这一点,您必须使用此关键字传递JFrame的引用。
#2
1
To have access to a private instance within another class, I think you should use agetter. Example:
要访问另一个类中的私有实例,我认为您应该使用agetter。例:
//JFrame declaration
private JFrame frame;
//Getter
public JFrame getFrame() {
return frame;
}
#3
0
As noted by one answer, you can pass in a reference of the GUI or view into any class that needs it, for instance by passing the GUI class into the other class's constructor parameter, and using the parameter to set a field, but having said that, there are caveats:
正如一个答案所指出的,你可以将GUI或视图的引用传递给任何需要它的类,例如将GUI类传递给另一个类的构造函数参数,并使用参数来设置字段,但是说了有一些警告:
- Always be sure to make Swing state changes on the Swing event thread (the EDT). Since you're using background threading, this means that you will either
- use a SwingWorker as your background thread, and notify the GUI of changes via the publish/process method pair, or
- your SwingWorker can notify observers of change via PropertyChangeListeners and "bound" properties, or
- if using a standard Thread/Runnable, you will need to be sure to queue Swing calls onto the EDT using
SwingUtilities.invokeLater(someRunnable)
使用SwingWorker作为后台线程,并通过发布/处理方法对通知GUI更改,或
您的SwingWorker可以通过PropertyChangeListeners和“bound”属性通知观察者更改,或者
如果使用标准的Thread / Runnable,则需要确保使用SwingUtilities.invokeLater(someRunnable)将Swing调用排入EDT。
- Even better is to use a Model-View-Control type structure, but even if you do this, the model should be changed on the EDT for the same reasons above.
- As a side recommendation in general I try to avoid making classes that extend JFrame as that unnecessarily restricts my code to creating just JFrames.
始终确保在Swing事件线程(EDT)上进行Swing状态更改。由于您使用的是后台线程,这意味着您将使用SwingWorker作为后台线程,并通过发布/进程方法对通知GUI更改,或者您的SwingWorker可以通过PropertyChangeListeners通知观察者更改并“绑定”属性,或者如果使用标准的Thread / Runnable,您需要确保使用SwingUtilities.invokeLater(someRunnable)将Swing调用排队到EDT上
更好的是使用Model-View-Control类型结构,但即使这样做,也应该在EDT上更改模型,原因与上述相同。
作为一般的侧面推荐,我尽量避免使用扩展JFrame的类,因为这会不必要地限制我的代码只创建JFrame。
Note that this help is very general, but if you need more specific help, then you will want to post more specific information regarding your problem and your pertinent code.
请注意,此帮助非常通用,但如果您需要更具体的帮助,那么您需要发布有关您的问题和相关代码的更多具体信息。
#1
1
In order to acheive this you have to pass the reference of JFrame using this keyword.
为了实现这一点,您必须使用此关键字传递JFrame的引用。
#2
1
To have access to a private instance within another class, I think you should use agetter. Example:
要访问另一个类中的私有实例,我认为您应该使用agetter。例:
//JFrame declaration
private JFrame frame;
//Getter
public JFrame getFrame() {
return frame;
}
#3
0
As noted by one answer, you can pass in a reference of the GUI or view into any class that needs it, for instance by passing the GUI class into the other class's constructor parameter, and using the parameter to set a field, but having said that, there are caveats:
正如一个答案所指出的,你可以将GUI或视图的引用传递给任何需要它的类,例如将GUI类传递给另一个类的构造函数参数,并使用参数来设置字段,但是说了有一些警告:
- Always be sure to make Swing state changes on the Swing event thread (the EDT). Since you're using background threading, this means that you will either
- use a SwingWorker as your background thread, and notify the GUI of changes via the publish/process method pair, or
- your SwingWorker can notify observers of change via PropertyChangeListeners and "bound" properties, or
- if using a standard Thread/Runnable, you will need to be sure to queue Swing calls onto the EDT using
SwingUtilities.invokeLater(someRunnable)
使用SwingWorker作为后台线程,并通过发布/处理方法对通知GUI更改,或
您的SwingWorker可以通过PropertyChangeListeners和“bound”属性通知观察者更改,或者
如果使用标准的Thread / Runnable,则需要确保使用SwingUtilities.invokeLater(someRunnable)将Swing调用排入EDT。
- Even better is to use a Model-View-Control type structure, but even if you do this, the model should be changed on the EDT for the same reasons above.
- As a side recommendation in general I try to avoid making classes that extend JFrame as that unnecessarily restricts my code to creating just JFrames.
始终确保在Swing事件线程(EDT)上进行Swing状态更改。由于您使用的是后台线程,这意味着您将使用SwingWorker作为后台线程,并通过发布/进程方法对通知GUI更改,或者您的SwingWorker可以通过PropertyChangeListeners通知观察者更改并“绑定”属性,或者如果使用标准的Thread / Runnable,您需要确保使用SwingUtilities.invokeLater(someRunnable)将Swing调用排队到EDT上
更好的是使用Model-View-Control类型结构,但即使这样做,也应该在EDT上更改模型,原因与上述相同。
作为一般的侧面推荐,我尽量避免使用扩展JFrame的类,因为这会不必要地限制我的代码只创建JFrame。
Note that this help is very general, but if you need more specific help, then you will want to post more specific information regarding your problem and your pertinent code.
请注意,此帮助非常通用,但如果您需要更具体的帮助,那么您需要发布有关您的问题和相关代码的更多具体信息。