简体   繁体   中英

How to create an hello world in Java Swing? What is wrong in my code?

I am studying Java Swing and I have some problem with the following simple code:

package com.techub.exeute;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public class Main{

    public static void main(String[] args) {

        JFrame frame = new JFrame("FrameDemo");
        frame.setMinimumSize(new Dimension(800, 400));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       

        JLabel myLabel = new JLabel("Hello World !!!", SwingConstants.CENTER);
        myLabel.setFont(new Font("Serif", Font.BOLD, 22));
        myLabel.setBackground(Color.blue);
        myLabel.setOpaque(true);
        myLabel.setPreferredSize(new Dimension(100, 80));

        frame.getContentPane().add(myLabel, BorderLayout.NORTH);

    }
}

My idea is to create a JFrame object and insert into it an Hello World JLabel object settings some property.

I do it into the main() method. The problem is that when I execute the program I don't see anything !!! Why? What is wrong in my code?

Tnx

Andrea

You are creating the frame but you are not displaying it. Call

frame.setVisible(true);

to display it.

Another thing: you should not manipulate GUI components in the main thread. Instead, create a new method for creating the frame and setting up the components, and run that method in the event dispatch thread, like in the example from the official tutorial :

import javax.swing.*;        

public class HelloWorldSwing {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Just add

frame.setVisible(true);

to your code

See the steps to Creating and Showing Java Swing Frames

//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");

//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//4. Size the frame.
frame.pack();

//5. Show it.
frame.setVisible(true);

You missed out #5

You need a

frame.setVisible(true);

call in your code.

As others mentioned you should not use the main Thread for gui operations. I suggest you should refer to the official tutorials of SWING, they are rather helpful and you'll see examples there for proper threading.

在您的方法中保持这一行

frame.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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