import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class gui extends JFrame{
private static final long serialVersionUID = 1L;
private JLabel item1;
public void createGUI(){
setTitle("Window Title");
setLayout(new FlowLayout());
item1 = new JLabel("This is a sentence.");
item1.setToolTipText("This is a tip...");
add(item1);
}
import javax.swing.JFrame;
public static void main(String[] args) throws Exception{
gui guiObj = new gui();
guiObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiObj.setSize(300,300);
guiObj.setVisible(true);
}
窗口的标题未应用,标签未显示。我不确定为什么...有人看到我可能忽略了的东西吗?
最佳答案
您需要致电createGUI()
。那是一切初始化的地方
gui guiObj = new gui();
guiObj.createGUI();
另外,请遵循Java命名约定。类名应以大写字母开头,即
gui
→Gui