Java 如何在 JFrame 或 JPanel 上显示文本?

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

How to display text on JFrame or JPanel?

javajframe

提问by

I want to display some text on a window, should I use JFrameor JFanel? What is the difference between those two and how can I add text to a window?

我想在窗口上显示一些文本,我应该使用JFrame还是JFanel?这两者之间有什么区别,如何向窗口添加文本?

I am using eclipse, please help me.

我正在使用eclipse,请帮助我。

回答by Bruno Caceiro

A JFrameis a top-level window with a title and a border. A JPanelprovides general-purpose containers for lightweight components, such as textfields, buttons and more.

一个JFrame的是带有标题和边框的顶层窗口。甲的JPanel提供通用的容器轻量级组件,诸如文本框,按钮等等。

You will need to use both JFrame and JPanel, you create the JFrame and then add a Panel, wich will contain your textfield:

您将需要同时使用 JFrame 和 JPanel,您创建 JFrame,然后添加一个面板,其中将包含您的文本字段:

JFrame frame = new JFrame("Title for your window");

JPanel panel = new JPanel();
JLabel label = new JLabel("Your text here");

panel.add(label);

frame.add(panel);
frame.setVisible(true);