Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you to create a JFrame with simple text?

I just want to make a JFrame that will say "Hello world", nothing big, no interaction needed. How do I do this?

I can create the JFrame, however I do not know how to put a JPanel with simple text in it.

Here is what I got so far

JFrame frame = new JFrame("Relief Valve");
frame.setResizable(false);
frame.setLocation(500,300);
JPanel p1 = new JPanel();
frame.setVisible(true);
like image 393
Mehmet K Avatar asked Feb 15 '23 12:02

Mehmet K


2 Answers

Instead of creating the JPanel, try:

JLabel label = new JLabel("this is my text");
frame.add(label);
frame.pack();
like image 160
Rob I Avatar answered Feb 20 '23 10:02

Rob I


JFrame window = new JFrame("Hello World App"); 
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
window.add(new JLabel("Hello World"), BorderLayout.CENTER);
window.pack();
window.setVisible(true);
window.setLocationRelativeTo(null);

I'm currently on a mobile device but I'll be happy to document that when I get on a computer, feel free to ask any questions though.

like image 42
Josh M Avatar answered Feb 20 '23 10:02

Josh M