主要方法invokeLater可以创建与Java主方法中通常不同的方法吗? [英] Can the main method invokeLater be created differently than it normally is in a Java main method?

查看:115
本文介绍了主要方法invokeLater可以创建与Java主方法中通常不同的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以对invokeLater进行编码,以便您可以创建Runnable的实例,然后@Override方法run(),然后将该Runnable变量发送到方法中。

Can the invokeLater be coded so that you can create an instance of Runnable and then @Override the method run() and then send that Runnable variable into the method.

我想知道你是否可以改变代码的生成方式,并且仍能得到相同的结果,因为这就是我开始弄清楚程序在编程语言中是如何工作的方式。

I am wanting to see if you can change how the code is generated and still get the same results because that is how I start figuring out how things really work in a programming language.

代码:

import javax.swing.*;
public class DDHSimpleProgram {

    JFrame f = new JFrame("A simple Swing Program");
    JLabel l = new JLabel("Swing powers the modern GUI!");

    public DDHSimpleProgram() {
        f.setLocation(300, 300);
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(l);
        f.pack();
        f.setVisible(true);
    }

    public void run() {
        new DDHSimpleProgram();
    }

    public static void main(String[] args) {
        Runnable r = null; 
        SwingUtilities.invokeLater(r);
    }
}

此代码有效:

下面的代码适用于实现Runnable接口的程序,然后在零参数构造函数中创建GUI。 invokeLater将对象的新实例创建为匿名类。除了向控制台写入消息之外,Run方法实际上不会执行任何操作。我仍然认为这是面向对象设计结构中一个有趣的部分,对我来说有时候仍然很奇怪,因为你可以实际拥有你希望它做的任何方法。

The code below works with the program implementing the Runnable interface and then creates the GUI in the zero argument constructor. The invokeLater creates a new instance of the object as an anonymous class. The Run method actually does not do anything other than write a message to the console. I still think that this is an interesting part of the object oriented design structure that seems odd to me sometimes still because you can actual have the method to anything you want it to do.

import javax.swing.*;
public class DDHSimpleProgram implements Runnable {
    JFrame f = new JFrame("A simple Swing Program");
    JLabel l = new JLabel("Swing powers the modern GUI!");

    public DDHSimpleProgram() {
        f.setLocation(300, 300);
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(l);
        f.pack();
        f.setVisible(true);
    }

    @Override
    public void run() {
        System.out.println("Run Method");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DDHSimpleProgram());
    }
}


推荐答案

第一个例子不起作用, r null 。它不会来的原因是因为你的类没有实现 Runnable ,所以 run 永远不会被调用 invokeLater

The first example won't work, r is null. The reason it won't come is because your class does't implement Runnable, so run can never be called by invokeLater

第二个示例在EDT之外构建UI类但调用运行 EDT通过 invokeLater

The second example constructs the UI class outside of the EDT but calls run either the EDT via invokeLater

最好是构建一个实现Runnable 。从它的main方法,我将构造一个 Main 的实例,并将其作为对 invokeLater 的引用传递。在运行方法中,我会去构建UI,但我这样挑剔

Preferrably, I would construct a Main class that implements Runnable. From its main method, I would construct an instance of Main and pass it as a reference to invokeLater. In the run method, I would go about constructing the UI, but I'm fussy this way


可以对invokeLater进行编码,以便您可以创建
Runnable的实例,然后@Override方法run(),然后将
Runnable变量发送到方法中。

Can the invokeLater be coded so that you can create an instance of Runnable and then @Override the method run() and then send that Runnable variable into the method.

答案是肯定的。实现 Runnable 的任何对象实例都可以传递给 invokeLater

The answer to that is, yes. Any instance of an object that implements Runnable can be passed to invokeLater

更新了示例...

基于主界面看起来像......

Based on the assumption of the main interface looking like...

public class DDHSimpleProgram {

    JFrame f = new JFrame("A simple Swing Program");
    JLabel l = new JLabel("Swing powers the modern GUI!");

    public DDHSimpleProgram() {
        f.setLocation(300, 300);
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(l);
        f.pack();
        f.setVisible(true);
    }

}

我可能会使用 Main 类类似于......

I might use a Main class something like...

public class Main implements Runnable {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Main());
    }

    @Override
    public void run() {
        DDHSimpleProgram prog = new DDHSimpleProgram();
    }

}

这篇关于主要方法invokeLater可以创建与Java主方法中通常不同的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆