本文实例讲述了Java编程获取当前屏幕分辨率的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/** * This class implements the functionality of fetching the size of the screen */ package com.leo.util; /** * @author Leo Share * @since 07/16/2007 * @version 1.0 */ class ScreenSize{ private int screenWidth; private int screenHeight; public void setScreenWidth( int screenWidth){ this .screenWidth = screenWidth; } public void setScreenHeight( int screenHeight){ this .screenHeight = screenHeight; } public int getScreenWidth(){ setScreenWidth(( int )java.awt.Toolkit.getDefaultToolkit().getScreenSize().width); return screenWidth; } public int getScreenHeight(){ setScreenHeight(( int )java.awt.Toolkit.getDefaultToolkit().getScreenSize().height); return screenHeight; } } |
在Java的GUI设计中,常常需要让Frame或Dialog显示在屏幕的中央,可以先获取屏幕的分辨率,然后用下面的代码实现:
1
2
3
4
5
|
int screenWidth = (( int )java.awt.Toolkit.getDefaultToolkit().getScreenSize().width); int screenHeight = (( int )java.awt.Toolkit.getDefaultToolkit().getScreenSize().height); JFrame myFrame = new JFrame(); myFrame.setSize( 800 , 600 ) //设置Frame的大小 myFrame.setLocation((screenWidth- 800 )/ 2 , (screenHeight- 600 )/ 2 ); //设置Frame显示在屏幕中央 |
希望本文所述对大家java程序设计有所帮助。
原文链接:http://www.cnblogs.com/xxpal/articles/825037.html