简体   繁体   中英

JFrame window doesn't show up

I'm learning basic Java GUI programming from a tutorial, and this code is supposed to generate a window with a title, some text, and some tooltip text. It doesn't generate anything. Can anyone tell me why it isn't working?

//In a class called apples:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class apples extends JFrame {

    private JLabel item1;

    public apples() {
        super("Title");
        setLayout(new FlowLayout());

        item1 = new JLabel("This is some displayed text");
        item1.setToolTipText("This is some tooltip text.");
        add(item1);
    }

}


//In the main class class1:
import javax.swing.JFrame;

class class1 {
    public static void main(String args[]) {

        apples green = new apples();
        green.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Can you try like below?

class Class1 {
    public static void main(String args[]) {
        apples green = new apples();
        green.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        green.setSize(1000, 500); // set window's size.
        green.pack(); // packs the components closely together.
        green.setVisible(true); // makes the window visible.
    }
}

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