Java获取屏幕的分辨率
1/**
2 * This class implements the functionality of fetching the size of the screen
3 */
4package com.leo.util;
5
6/**
7 * @author Leo Share
8 * @since 07/16/2007
9 * @version 1.0
10 */
11
12class ScreenSize{
13 private int screenWidth;
14 private int screenHeight;
15
16 public void setScreenWidth(int screenWidth){
17 this.screenWidth = screenWidth;
18 }
19 public void setScreenHeight(int screenHeight){
20 this.screenHeight = screenHeight;
21 }
22
23 public int getScreenWidth(){
24 setScreenWidth((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width);
25 return screenWidth;
26 }
27 public int getScreenHeight(){
28 setScreenHeight((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
29 return screenHeight;
30 }
31}
2 * This class implements the functionality of fetching the size of the screen
3 */
4package com.leo.util;
5
6/**
7 * @author Leo Share
8 * @since 07/16/2007
9 * @version 1.0
10 */
11
12class ScreenSize{
13 private int screenWidth;
14 private int screenHeight;
15
16 public void setScreenWidth(int screenWidth){
17 this.screenWidth = screenWidth;
18 }
19 public void setScreenHeight(int screenHeight){
20 this.screenHeight = screenHeight;
21 }
22
23 public int getScreenWidth(){
24 setScreenWidth((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width);
25 return screenWidth;
26 }
27 public int getScreenHeight(){
28 setScreenHeight((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
29 return screenHeight;
30 }
31}
在Java的GUI设计中,常常需要让Frame或Dialog显示在屏幕的中央,可以先获取屏幕的分辨率,然后用下面的代码实现:
1int screenWidth = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width);
2int screenHeight = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
3JFrame myFrame = new JFrame();
4myFrame.setSize(800, 600)//设置Frame的大小
5myFrame.setLocation((screenWidth-800)/2, (screenHeight-600)/2);//设置Frame显示在屏幕中央
2int screenHeight = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
3JFrame myFrame = new JFrame();
4myFrame.setSize(800, 600)//设置Frame的大小
5myFrame.setLocation((screenWidth-800)/2, (screenHeight-600)/2);//设置Frame显示在屏幕中央