步骤:
1.设置外观风格
2.创建及设置窗口
3.添加Hello World标签
4.显示界面
public class HelloworldSwing {
private static void createAndShowGUI(){
//设置外观风格
JFrame.setDefaultLookAndFeelDecorated(true);
//创建及设置窗口
JFrame frame =new JFrame("helloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加Hello World标签
JLabel label=new JLabel("Hello World");
frame.getContentPane().add(label);
//显示窗口
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
//显示界面
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
来源于菜鸟教程:https://www.runoob.com/w3cnote/java-swing-demo-intro.html