Swing中JFrame如何获得屏幕尺寸

如果要获得屏幕得高和宽,需要利用Java中的Toolkit工具包,方法很简单,具体代码如下所示:

import java.awt.Dimension;
import java.awt.Toolkit;import javax.swing.JFrame;public class Demo1 {public static void main(String[] args) {JFrame jf = new JFrame("获得本机屏幕尺寸大小");jf.setLayout(null);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*---------------------------------------------------------------*/Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();int screenWidth = (int) screensize.getWidth();//获得屏幕得宽int screenHeight = (int) screensize.getHeight();//获得屏幕得高/*---------------------------------------------------------------*/jf.setSize(screenWidth/2, screenHeight/2);jf.setVisible(true);}
}