[英]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.我正在从教程中学习基本的 Java GUI 编程,这段代码应该生成一个带有标题、一些文本和一些工具提示文本的窗口。 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.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.