import javax.swing.*; import java.awt.*; public class SwingDemoMain { private static void createGUI(){ //JFrame指一个窗口,构造方法的参数为窗口标题 JFrame frame=new JFrame("swing demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //内容面板(ContentPane) Container contentPane=frame.getContentPane(); contentPane.setLayout(new FlowLayout()); //向内容面板里添加控件,如JLabel、JButton contentPane.add(new JLabel("Hello world")); contentPane.add(new JButton("测试")); //设置窗口的其它参数,如窗口大小 frame.setSize(400,300); //显示窗口 frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createGUI(); } }); } }