我正在从教程中学习基本的Java GUI编程,并且此代码应该生成一个带有标题,一些文本和一些工具提示文本的窗口。它不会产生任何东西。谁能告诉我为什么它不起作用?
//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);
}
}
答案 0 :(得分:2)
您可以尝试以下方法吗?
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.
}
}