Java Swing:多个窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1655294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:43:10  来源:igfitidea点击:

Java Swing: multiple windows

javaswing

提问by sivabudh

I'm new to GUI programming, but need to create a multiple window GUI. Does anyone know of any good tutorial online or could you please show a simple code that will launch 2 windows?

我是 GUI 编程的新手,但需要创建一个多窗口 GUI。有没有人知道任何好的在线教程,或者您能否展示一个将启动 2 个窗口的简单代码?

采纳答案by Ramon

Just create two JFrame objects like this:

只需像这样创建两个 JFrame 对象:

    public static void main(String[] args)  throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new JFrame("frame1").setVisible(true);
            new JFrame("frame2").setVisible(true);
        }
    });
}

回答by Hyman

http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html

http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html

the JDesktopPane is cool if you really want an integrated desktop.. it handles objects very similar to JFrames (they are indeed called JInternalFrame) and it automatically handles minimizing, maximing, top menu bar like a normal document based application.

如果您真的想要一个集成桌面,JDesktopPane 很酷。它处理与 JFrames 非常相似的对象(它们确实被称为 JInternalFrame)并且它像一个普通的基于文档的应用程序一样自动处理最小化、最大化、顶部菜单栏。

回答by peter.murray.rust

Java has a class called "Window" http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Window.html. This may not be what you want. The normal toplevel object in Swing is a JFrame (http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html) which is a subclass of Window.

Java 有一个名为“Window”的类http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Window.html。这可能不是您想要的。Swing 中的普通顶级对象是 JFrame ( http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html),它是 Window 的子类。

回答by JRL

I suggest you use NetBeans and create a project using the "Swing Desktop Application" pre-existing template.

我建议您使用 NetBeans 并使用“Swing 桌面应用程序”预先存在的模板创建一个项目。

It will create the basic infrastructure for your app including a main window with a menu and status bar with a progress bar, about box, event handlers, etc, all pre-wired.

它将为您的应用程序创建基本的基础设施,包括一个带有菜单的主窗口和一个带有进度条的状态栏、关于框、事件处理程序等,所有这些都是预先连接的。

What's nice about it for example is that the progress bar is already configured to listen to any action task that you create, so by simply creating a new action task, you get a working progress bar that will run when the task executes, without having to code it.

例如,它的优点是进度条已经配置为侦听您创建的任何操作任务,因此只需创建一个新的操作任务,您就可以获得一个工作进度条,该进度条将在任务执行时运行,而无需编码它。

Furthermore, you get a visual drag-and drop Editor that certainly sometimes can be frustrating when it comes to resizing and layouts, but for simple layouts is very good and easy to use. You'll be able to create an interface in no time.

此外,您将获得一个可视化的拖放编辑器,在调整大小和布局时,它有时会令人沮丧,但对于简单的布局来说,它非常好且易于使用。您将能够立即创建一个界面。

For more info see here.

有关更多信息,请参见此处

回答by evilReiko

thiswebsite is the best IMO, gives you direct How-to-do-it codes, with super brief descriptions

这个网站是最好的国际海事组织,给你直接的操作方法代码,超级简短的描述

for GUI tutorials, look for "Swing" lessons.

对于 GUI 教程,请查找“Swing”课程。