简体   繁体   English

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

[英]How to display text on JFrame or JPanel?

I want to display some text on a window, should I use JFrame or JFanel ?我想在窗口上显示一些文本,我应该使用JFrame还是JFanel What is the difference between those two and how can I add text to a window?这两者之间有什么区别,如何向窗口添加文本?

I am using eclipse, please help me.我正在使用eclipse,请帮助我。

A JFrame is a top-level window with a title and a border. JFrame是具有标题和边框的顶级窗口。 A JPanel provides general-purpose containers for lightweight components, such as textfields, buttons and more. 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM