如何将对象传递给implements并传递出本地对象?
如何将对象传递给工具并将本地对象传递给外部对象?我认为 SwingUtilities.invokeLater 对于 Swing 对象来说是必要的,对吧?
Sensors sens = new Sensors();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI application = new GUI(sens);
application.getJFrame().setVisible(true);
}
});
SMS sms = new SMS(application);
这是我尝试解决问题的方法,但是我得到了一个No enclosure instance of type GUI isaccessible。必须使用 GUI 类型的封闭实例来限定分配(例如 xnew A(),其中 x 是 GUI 的实例)。问题。
// in main
Sensors sens = new Sensors();
GUI application = null;
SwingUtilities.invokeLater(new GUIthread(sens , application));
SMS sms = new SMS(application);
//a class inside GUI.java , but not inside GUI class
class GUIthread implements Runnable{
Sensors s;
GUI g;
public GUIthread(Sensors s , GUI g){
this.s = s;
this.g = g;
}
@Override
public void run() {
// TODO Auto-generated method stub
g = new GUI(s);
g.getJFrame().setVisible(true);
}
}
How do I pass a object to an implement and pass the local object to object that is outside? I think the SwingUtilities.invokeLater is nessasary for a Swing object , right?
Sensors sens = new Sensors();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI application = new GUI(sens);
application.getJFrame().setVisible(true);
}
});
SMS sms = new SMS(application);
this is me try to solve the problem , but i get a No enclosing instance of type GUI is accessible. Must qualify the allocation with an enclosing instance of type GUI (e.g. x.new A() where x is an instance of GUI). problem.
// in main
Sensors sens = new Sensors();
GUI application = null;
SwingUtilities.invokeLater(new GUIthread(sens , application));
SMS sms = new SMS(application);
//a class inside GUI.java , but not inside GUI class
class GUIthread implements Runnable{
Sensors s;
GUI g;
public GUIthread(Sensors s , GUI g){
this.s = s;
this.g = g;
}
@Override
public void run() {
// TODO Auto-generated method stub
g = new GUI(s);
g.getJFrame().setVisible(true);
}
}
the sourcecode
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您尝试在未指定(或暗示)封闭类的实例的上下文中创建非静态内部类的实例时,就会出现此问题。
由此,我推断您已将其中一个类声明为非静态内部类;例如,这样的事情:
如果您现在尝试使用
new Inner()
在其他代码中创建Inner
实例,您将收到与您类似的编译错误看到。您可以执行以下两种操作之一来“修复”该问题:
如果将
public class Inner {
更改为public static class Inner {
,您可以使用>new Inner()
正如您当前所做的那样。但这意味着Inner
的代码无法访问封闭类的(最终)实例变量;即Outer
。如果您不想将
Inner
更改为静态类,则需要按如下方式实例化它:FOLLOWUP
只有我在上面确定的那个。
不可以。上面“限定创建”示例中的代码可以出现在
Inner
类可访问的任何位置。由于我们将其声明为public
...如果您在
Outer
的构造函数(或实例方法)内实例化Inner
,则可以使用new Inner()
。封闭的Outer
实例与this
相同。This problem arises when you try to create an instance of a non-static inner class in a context that does not specify (or imply) an instance of the enclosing class.
From this, I deduce that you have declared one of your classes as a non-static inner class; e.g. something like this:
If you now try to create an instance of
Inner
in some other code usingnew Inner()
, you will get a compilation error like the one you are seeing.You can do one of two things to "fix" the problem:
If you change
public class Inner {
topublic static class Inner {
, you can usenew Inner()
as you are currently doing. But this will mean that the code ofInner
cannot access the (final) instance variables of the enclosing class; i.e.Outer
.If you don't want to change
Inner
to a static class, you will need to instantiate it as follows:FOLLOWUP
Only the one that I identified above.
No. The code in the "qualified creation" example above can appear anywhere that the
Inner
class is accessible. And since we declared it aspublic
...If you instantiate
Inner
inside a constructor (or instance method) forOuter
, you can just usenew Inner()
. The enclosingOuter
instance is the same asthis
.尝试
一下。
Try
instead.
很简单,将引用声明为final,匿名类代码将看到它。
Easy, declare the reference final and it will be seen by the anon class code.
我同意 Zach 的观点,并怀疑 GUIthread 是一个内部类。如果是这样,您最好将其设为独立类或静态内部类,但在没有更多信息和实际错误消息的情况下很难知道这是否是真正的解决方案。
I agree with Zach and suspect that GUIthread is an inner class. If so, you may do well to make it a stand-alone class or a static inner class, but it's difficult to know if this is the true solution without more information and without the actual error message.