java 初始化 JFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5442745/
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-10-30 11:09:14  来源:igfitidea点击:

Initializing a JFrame

javaswinginitializationjframe

提问by omp

This is probably an easy question, but hey, i'm a beginner.

这可能是一个简单的问题,但是嘿,我是初学者。

So I've created a program that calculates some stuff, and it's running in the console at the moment. I decided to add a user interface to it, so i created a JForm using the built in features in NetBeans IDE (probably just temporary until i learn to handle Swing manually, i have a deadline so i want to finish it fast), and draw out a few controls and containers. Now that the form is ready, how do i initialise it? As far as i know i need to call the class from Main.java somehow, correct? I've tried just doing: GUI gui = new GUI(); (the class is called GUI), but that doesn't do anything. If i leave it blank it does the same thing. Or is it suppose to initialize itself?

所以我创建了一个计算一些东西的程序,它目前正在控制台中运行。我决定给它添加一个用户界面,所以我使用 NetBeans IDE 中的内置功能创建了一个 JForm(可能只是暂时的,直到我学会手动处理 Swing,我有一个截止日期所以我想快速完成它),然后绘制出一些控件和容器。现在表单已准备就绪,我该如何初始化它?据我所知,我需要以某种方式从 Main.java 调用该类,对吗?我试过只做: GUI gui = new GUI(); (该类称为 GUI),但这没有任何作用。如果我把它留空,它会做同样的事情。或者它假设初始化自己?

Thanks.

谢谢。

回答by ggf31416

GUI gui = new GUI(); 
gui.setVisible(true);

回答by Grzegorz Szpetkowski

IMHO recommended way to do this is (using event dispatch thread and simple anonymous class):

恕我直言,推荐的方法是(使用事件调度线程和简单的匿名类):

// effectively the same as SwingUtilities.invokeLater
EventQueue.invokeLater(new Runnable()
{
    public void run()
    {
        JFrame frame = new GUI(); // GUI gui = new GUI() as well
        // default value JFrame.HIDE_ON_CLOSE
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true);
    }
});

From Java API http://download.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading:

从 Java API http://download.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading

"In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread."

“一般来说,Swing 不是线程安全的。除非另有说明,所有 Swing 组件和相关类都必须在事件调度线程上访问。”

You can use SwingUtilities.invokeLater or EventQueue.invokeLater with sameresult, i.e. SwingUtilities.invokeLater method just calls EventQueue.invokeLater method.

您可以使用具有相同结果的SwingUtilities.invokeLater 或 EventQueue.invokeLater ,即 SwingUtilities.invokeLater 方法只调用 EventQueue.invokeLater 方法。

More information at Java tutorial(especially why to use that JFrame.EXIT_ON_CLOSE).

Java 教程中的更多信息(特别是为什么要使用 JFrame.EXIT_ON_CLOSE)。

回答by Johnny

Here is a simplest example to create a JFrame:

这是创建 JFrame 的最简单示例:

JFrame myFrame = new JFrame("");
frame.setTitle("Welecome to JFames!");
frame.setSize(600, 400);
frame.setVisible(true);

The frame.setTitle("text");command will change the frame title.

frame.setTitle("text");命令将更改框架标题。

The setVisible(boolean)command will make it visible because it's hidden by

setVisible(boolean)命令将使其可见,因为它被隐藏

The setSize(int, int)command will change the size of the frame.

setSize(int, int)命令将更改框架的大小。

For more JFrame examples, check out for example: https://javatutorial.net/swing-jframe-basics-create-jframe

有关更多 JFrame 示例,请查看示例:https: //javatutorial.net/swing-jframe-basics-create-jframe

回答by Johnny

initialize the class scope with, setTitle("Your Title");

使用 setTitle("Your Title"); 初始化类范围;

回答by Johnny

The above answer "initialize the class scope with, setTitle("Your Title");" says how to set the window title for a jframe.

上面的答案“用 setTitle("Your Title"); 初始化类作用域;说如何为 jframe 设置窗口标题。