如何将对象传递给implements并传递出本地对象?

发布于 2024-10-13 17:39:33 字数 1077 浏览 1 评论 0原文

如何将对象传递给工具并将本地对象传递给外部对象?我认为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紙鸢 2024-10-20 17:39:33

当您尝试在未指定(或暗示)封闭类的实例的上下文中创建非静态内部类的实例时,就会出现此问题。

由此,我推断您已将其中一个类声明为非静态内部类;例如,这样的事情:

public class Outer {
    ...
    public class Inner {
        public Inner() {
           ...
        }
        ...
    }
    ...
}

如果您现在尝试使用 new Inner() 在其他代码中创建 Inner 实例,您将收到与您类似的编译错误看到。

您可以执行以下两种操作之一来“修复”该问题:

  • 如果将 public class Inner { 更改为 public static class Inner {,您可以使用 >new Inner() 正如您当前所做的那样。但这意味着 Inner 的代码无法访问封闭类的(最终)实例变量;即Outer

  • 如果您不想将 Inner 更改为静态类,则需要按如下方式实例化它:

    外部外部 = ...
    ...
    内部内部 = 外部.new Inner(); // 合格的创建
    

FOLLOWUP

使用静态类调用 swing 有什么缺点吗?

只有我在上面确定的那个。

那么,所有实例化都发生在外部构造函数内吗?正确的?

不可以。上面“限定创建”示例中的代码可以出现在 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:

public class Outer {
    ...
    public class Inner {
        public Inner() {
           ...
        }
        ...
    }
    ...
}

If you now try to create an instance of Inner in some other code using new 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 { to public static class Inner {, you can use new Inner() as you are currently doing. But this will mean that the code of Inner 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:

    Outer outer = ...
    ...
    Inner inner = outer.new Inner();  // qualified creation
    

FOLLOWUP

any down side using static class to call swing?

Only the one that I identified above.

SO , all the instantiate happen inside Outer constructor? right?

No. The code in the "qualified creation" example above can appear anywhere that the Inner class is accessible. And since we declared it as public ...

If you instantiate Inner inside a constructor (or instance method) for Outer, you can just use new Inner(). The enclosing Outer instance is the same as this.

疯到世界奔溃 2024-10-20 17:39:33

尝试

final Sensors sens = new Sensors();

一下。

Try

final Sensors sens = new Sensors();

instead.

假情假意假温柔 2024-10-20 17:39:33

很简单,将引用声明为final,匿名类代码将看到它。

Easy, declare the reference final and it will be seen by the anon class code.

罪#恶を代价 2024-10-20 17:39:33

我同意 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文