import java.awt.*; import javax.swing.*; public class SimpleFrameTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ // 开一个线程 public void run() { SimpleFrame frame = new SimpleFrame(); frame.setTitle("记事本"); // 设置title frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } class SimpleFrame extends JFrame{ /* *通过调用Toolkit类静态方法getDefaultToolkit得到一个Toolkit对象 , *调用getScreenSize方法,返回屏幕大小 */ Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenHeight = screenSize.height; // 屏幕实际高 int screenWidth = screenSize.width; //屏幕实际宽 public SimpleFrame () { setSize(screenWidth/2,screenHeight/2); // 设置显示的窗口的大小 } }
结果: