import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
//得到屏幕大小的例子
public class ScreenSizeDemo
{
JFrame jf = new JFrame("ScreenSizeDemo");
JTextField jtf = new JTextField(20);
JButton jb = new JButton("测试");
//得到屏幕的大小
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int)screensize.getWidth();//得到宽
int height = (int)screensize.getHeight();//得到高
public ScreenSizeDemo()
{
jf.setLayout(new FlowLayout(FlowLayout.LEFT));
jf.add(jtf);
jtf.setEditable(false);
jf.add(jb);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtf.setText("您的屏幕大小是:" + width + "*" + height);
}
});
jf.setSize(320,80);
jf.setLocation(width/3,height/3);
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new ScreenSizeDemo();
}
}