user5342000
user5342000

Reputation:

How to display text on JFrame or JPanel?

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

I am using eclipse, please help me.

Upvotes: 1

Views: 25306

Answers (1)

Bruno Caceiro
Bruno Caceiro

Reputation: 7189

A JFrame is a top-level window with a title and a border. A JPanel provides general-purpose containers for lightweight components, such as textfields, buttons and more.

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

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);

Upvotes: 1

Related Questions