有人能给我解释一下这个 javax swing 方法吗?

发布于 2024-08-19 12:48:28 字数 677 浏览 9 评论 0原文

我无法理解这个简单的代码:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
   public void run() {
      createAndShowGUI();
   }
});

任何人都可以解释一下它是如何工作的(简单来说,因为我是新手)?这个简短的代码是这个 更大的代码

更具体地说,我有以下问题:

  1. “public void run”是否定义了新方法?如果是这样,为什么它是在另一个方法中定义的(请参阅“更大的代码”以供参考)?
  2. 如果“public void run”定义了一个新方法,那么定义一个只包含一行代码的方法(createAndShowGUI)的理由是什么?
  3. “invokeLater”有什么作用?这对我来说实际上是最复杂的问题。

我想再次强调,我是一个新手,使用“特殊”和“技术”词语会产生更多问题。

如果您决定帮助我,请提前致谢!

I have trouble understanding this simple code:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
   public void run() {
      createAndShowGUI();
   }
});

Can anybody please explain me how it works (in simple terms because I am a newbie)? This short code is a part of this larger code.

To be more specific, I have the following questions:

  1. Does "public void run" define a new method? If it is the case, why it is defined within another method (see "larger code" for reference)?
  2. If "public void run" defines a new methods, what is a reason to define a method containing only one line of code (createAndShowGUI)?
  3. What does "invokeLater" do? This is actually the most complicated question for me.

I would like to emphasize one more time that I am a newbie and usage of "special" and "technical" words will produce even more questions.

Thank you in advance, if you decide to help me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

深陷 2024-08-26 12:48:28

我能给出的最简短的答案是:

Runnable 是 Java 中的一个接口,表示定义 run 方法的类型。任何实现此接口的类都必须提供 run 的实现。 Runnable 表示将由系统其他部分执行的任务。 Thread 是众所周知的Runnable

当您的代码类似于 new InterfaceName() { //implementation } 时,称为匿名类。匿名类的目的是创建一个实现接口类型的一次性类。这个类没有名称,因此我们永远无法再次引用它。它仅用于此处。

在不了解 Swing 的情况下,看起来 SwingUtilities.invokeLater() 接受一个 Runnable 并且......好吧,我猜它稍后会调用它(大概是多久之后) JavaDocs 中描述)。但是,您将 run 在这里定义为简单的另一个方法调用的原因是,SwingUtilities 中的某些代码将调用 run 方法这个可运行;事实上,它对任何 Runnable 可能了解的只是它定义了一个带有签名 public void run() 的方法,因为这是 中定义的唯一方法。可运行的接口。

嗯,我想这毕竟还不算太短。

The shortest answer I can give is:

Runnable is an interface in Java representing a type that defines a run method. Any class that implements this interface must provide an implementation for run. Runnables represent tasks that are to be executed by other parts of your system. Thread is a well-known Runnable.

When you have code that looks like new InterfaceName() { //implementation }, that is called an anonymous class. The point of an anonymous class is to make a one-off class implementing the interface type. This class has no name, and as such we can never refer to it again. It is only used here.

Without knowing much about Swing, it looks like SwingUtilities.invokeLater() accepts a Runnable and...well, I would guess it invokes it later (how much later is probably described in the JavaDocs). But, the reason you would define run here as simply another method invocation is that some code inside SwingUtilities is going to be calling the run method on this Runnable; indeed, all it can possibly know about any Runnable is that it defines a method with signature public void run(), because that is the only method defined in the Runnable interface.

Well, I guess that wasn't too short after all.

骄兵必败 2024-08-26 12:48:28

这是一个匿名内部类

您定义了三件事:

  • 一个新类
  • 该类中名为 run 的方法
  • 该类的新实例

在此示例中,某些操作 (createAndShowGUI()) 需要在以下位置运行Swing 线程。您不能在方法调用中间“跳”到另一个线程,因此您可以将一个对象(您创建的新实例)放入队列中。当 Swing 线程准备就绪时,它将从队列中删除该对象并调用 run 方法,该方法又调用 createAndShowGUI。现在一切都发生在正确的线程中。

It's an anonymous inner class.

You are defining three things:

  • A new class
  • A method called run within that class
  • A new instance of that class

In this example some operation (createAndShowGUI()) needs to run in the Swing thread. You cannot "jump" into another thread in the middle of a method call, so instead you are placing an object (the new instance you created) in a queue. When the Swing thread is ready it will remove that object from the queue and call your run method, which in turn calls createAndShowGUI. Now everything happens in the correct thread.

清秋悲枫 2024-08-26 12:48:28

虽然这就是 Swing 通常创建 GUI 的方式,但它们所做的只是告诉您“就这样做,我们稍后会解释原因”。我想他们是对的,因为细节有点复杂并且对初学者没有帮助。但无论如何:

invokeLater 启动一个线程,稍后将运行 Runnable 类。正如 finnw 所提到的,可运行类是通过 new Runnable { [..] } 临时创建的,并且必须根据 指定一个 public void run() 方法可运行的接口。在这种情况下,它所做的只是执行 createAndShowGUI 方法。

While this is how Swing typically creates a GUI, all they do is tell you "just do it like this, we will explain why later". And i guess they are right, because the details are somewhat complicated and non-helpful for a beginner. But anyway:

invokeLater launches a thread that will run a Runnable class later. The runnable class is created ad hoc as mentioned by finnw via new Runnable { [..] } and must specify a public void run() method according to the Runnable interface. In this case, all it does is execute the createAndShowGUI method.

不念旧人 2024-08-26 12:48:28

此代码使函数 (createAndShowGUI()) 在另一个线程中被调用。
如果您不知道线程是什么,最好阅读一下它,但无论如何,请将其视为与其他代码同时执行的代码。其他代码将是此 Invoke() 调用之后的行。
这在图形应用程序中很常见,代码运行以更新图形,而其他代码响应用户操作。
也就是说,我没有费心去阅读有关 InvokeLater() 的文档,也没有写过太多 Java 代码。正如“LumpN”所说 - “这就是 Swing 通常创建 GUI 的方式”。

更奇怪的是(对于新手来说),invokeLater() 参数是以匿名方式定义的——“在代码中”。这实际上是一件好事,因为这个类没什么新鲜的简单的。所有其他答案都解释了匿名的事情。

所以:
1.是的。
2.你问为什么并要求不要获得太多技术细节 - 但正如“yulkes”所说 - 因为在Java中你不能将方法作为参数传递,所以你必须构建这个类,并且像这样构建它很好。
3.根据我在第一段中总结的内容,您可以简要阅读文档(只需在Google上搜索函数名称),也可以了解“Google代码搜索”。

This code makes a function (createAndShowGUI()) be called in another thread.
In case you don't know what a thread is, better read about it, but anyhow, think about it as code that is performed at the same time that other code is performed. The other code will be the lines that follow this Invoke() call.
This is very common in graphic apps, code runs to update the graphic while other code respond to user actions.
That said I haven't bothered to read the documentation about InvokeLater(), and haven't wrote too much Java in my life. As 'LumpN' said - 'this is how Swing typically creates a GUI'.

The weirder (for a newbie) thing is that the invokeLater() parameter is defined in an annonymous way - 'within the code'. It is actually a nice thing since this class is so nothing-new-simple. All other answers has explained the annonymous thing.

so:
1.yes.
2. You asked why and asked not to get too much technical details - but as said by 'yulkes' - because in Java you can't pass methods as parameters, so you must build this class, and its nice to build it like this.
3. With what I summed on my first paragraph, you can briefly read documentation (just search function name on Google), also get the to know 'Google code search'.

怂人 2024-08-26 12:48:28

当您调用 invokeLater 时,您将向其传递一个 Runnable 类型实例。 Runnable 是一个仅声明一个方法“public void run()”的接口。
简短代码的第一行中发生的情况称为“匿名内部类” - 您正在创建一个仅使用一次的 Runnable 实例,您甚至没有命名它。

InvokeLater 会收到你的 Runnable,里面会做类似的事情,

public static void invokeLater(Runnable r) {
    // Wait for the correct time to run this
    r.run()
}

这意味着 invokeLater 会决定何时调用 run 方法,从而运行 createAndShowGUI。

因为在 Java 中您还不能将方法作为参数传递来运行,所以您被迫创建这个一次性的 Runnable 实例。

When you call invokeLater you're passing it a Runnable type instance. Runnable is an interface that declares only one method "public void run()".
What happens in line 1 of your short code, is called an "Annonymous Inner Class" - You're creating an instance of Runnable which is used only one time, you're not even naming it.

InvokeLater will receive your Runnable, and inside will do something like

public static void invokeLater(Runnable r) {
    // Wait for the correct time to run this
    r.run()
}

This means invokeLater will decide when to call the run method, thus running createAndShowGUI.

Because in Java you can't pass methods as parameters to run (yet), you are forced to create this one-use Runnable instance.

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