Java教程4:接口和基本的Swing应用程序

作者 : 慕源网 本文共2584个字,预计阅读时间需要7分钟 发布时间: 2022-04-14 共386人阅读

下面的代码演示了一个基本的Swing应用程序。Swing是使用Java创建桌面应用程序的标准GUI(图形用户界面)API。

注意,除了一堆不熟悉的类之外,我们还使用接口的概念来创建基本的Swing应用程序。

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;

// Note, we will implement the Runnable interface

public class Application implements Runnable {


    public static void main(String[] args) {
        
        // Create an Application object
        Application app = new Application();
        
        // Run the Swing thread. 
        // invokeLater requires a class that implements the Runnable interface
        javax.swing.SwingUtilities.invokeLater(app);
    }

    public void run() {
        // Create a new window with the title "Hello World"
        JFrame frame = new JFrame("Hello World");
        
        // Tell the application to exit if the user closes the frame
        // by clicking on the X in the corner.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Create a label to add to the frame.
        JLabel label = new JLabel("Hi there");
        
        // Add the label to the frame's content pane
        // Note, this is an example of method
        
        // Get the frame's content pane.
        Container content = frame.getContentPane();
        
        content.add(label);

        // Set the frame size.
        frame.setSize(400, 100);
        
        // Make the frame visible.
        frame.setVisible(true);
        

}

接口在关键字implements中显示其自身。当我们创建Swing应用程序时,我们必须在一个特殊的线程中运行该应用程序。这个线程是什么或做什么超出了本教程的范围。需要注意的主要一点是,我们可以通过将要在此线程中运行的代码传递给javax.swing.SwingUtilities.invokeLater()来创建线程。

这不是在Java中创建线程的常规方法——它只适用于Swing。

我们希望告诉invokeLater()运行创建GUI的代码。但是我们如何告诉它运行什么代码呢?

答案是,我们向它传递一个包含名为run()的方法的对象。我们把我们想要运行的代码放在run方法中。

但是,invokeLater()如何确定它只会被传递给包含run()方法的对象呢?是什么阻止我们传递任何旧垃圾?这就是接口的用武之地。接口是一种文本契约,它指定类必须(至少)包含哪些方法。在本例中,我们在应用程序类的名称之后编写了ImplementsRunnable。

Runnable是一个接口。它指定该类必须定义一个run()方法——否则程序将无法编译。同时,invokeLater()指定传递给它的对象必须实现Runnable,因此必须包含run()方法。

这里没有什么花哨的东西——没有深奥的面向对象方法,只有一个简单的文本契约,当您使用implements关键字时,它会自动执行。

让我们来看一个定义和使用接口的最简单的例子。

接口

首先,让我们定义一个接口。我们将创建一个接口,说明一个类必须包含一个名为“greet()”的方法,返回类型为void,不接受任何参数。

public interface Greeter {
    public void greet();
}

这就是它的全部。此接口强制“implements”它的任何类都具有Greet方法。

让我们创建一个类,并假设它实现了Greeter接口。

public class Person implements Greeter {

}

这个类不会编译。为什么不?因为我们已经说过它实现了Greeter,但它不包含强制的greet()方法。我们来补充一下。

public class Person implements Greeter {


    public void greet() {
        System.out.println("Hello there");
    }

}

好多了。现在类编译。我们还添加了在控制台中写入“hello there”的指令。我们可以在greet()方法中放入我们喜欢的任何代码,或者根本不放入代码。

最后,我们将创建一个带有方法的对象,该方法接受任何实现Greeter接口的对象。

public class RunGreeter {
    public void speak(Greeter greeter) {
        greeter.greet();
    }
}

RunGreeter类的Speak方法可以接受任何实现Greeter接口的对象作为参数。此外,因为实现此接口的所有对象都必须包含greet()方法,所以RunGreeter可以很好的调用该方法。

让我们把这些都放在我们的主要应用程序方法中。

public class Application {

    public static void main(String[] args) {

        Person person = new Person();
        
        RunGreeter runner = new RunGreeter();
        
        runner.speak(person);

    }

}
Hello there

这是一个完整的应用程序,我们可以运行它来生成上面所示的输出。

接口一开始可能看起来有点奇怪,但实际上它们极大地简化了Java代码,消除了许多其他面向对象语言中令人困惑的“多重继承”的需要。它们在Java中被广泛使用。接口的一个优点是它只指定了类可以包含的最少方法。因此,一个类可以实现许多接口,并以许多不同的方式使用。


慕源网 » Java教程4:接口和基本的Swing应用程序

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录