01.import java.awt.*; 02.import javax.swing.*; 03. 04.public class TestJFrame { 05. public static void main(String[] args) { 06. MyFrame mf = new MyFrame("我的第一个窗口"); 07. } 08.} 09. 10.class MyFrame extends JFrame { 11. Container p; 12. public MyFrame() { 13. } 14. public MyFrame(String title) { 15. super(title); //设置窗体标题 16. p = getContentPane(); 17. setSize(500, 500); //设置窗体大小 18. centerOnScreen(); //窗体在屏幕中间显示 19. setIconImage(new ImageIcon("mm.jpg").getImage()); //设置窗体图标 20. p.setBackground(new Color(165, 79, 90)); //设置窗体背景色 21. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //设置关闭按钮操作 22. setVisible(true); //设置窗体可见 23. } 24. 25. public void centerOnScreen() { 26. Dimension displaySize = getToolkit().getScreenSize(); 27. Dimension winSize = getSize(); 28. int x = (displaySize.width - winSize.width) / 2; 29. int y = (displaySize.height - winSize.height) / 2; 30. if(x < 0) { 31. x = 0; 32. } 33. if(y < 0) { 34. y = 0; 35. } 36. setLocation(x, y); 37. } 38.}
文章转载自CSDN :http://blog.csdn.net/tsyj810883979/article/details/6494431