这个JAVA程序怎么让他堆满整个屏幕啊
发布网友
发布时间:2022-05-07 01:54
我来回答
共1个回答
热心网友
时间:2023-10-11 15:33
获取屏幕的长度和高度,然后在location 方法里进行设置边界是它们的一半就可以了,下面的getMidDimesion方法就是这样。
例子:
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class testFrame extends Frame
{
public testFrame(int width,int height)
{
this.setSize( width,height);
this.setLocation(testFrame.getMidDimesion( new Dimension(width,height)));
this.setBackground( Color.BLACK );
this.setVisible( true);
this.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
获取屏幕的长度和高度,然后在location 方法里进行设置边界是它们的一半就可以了,下面的getMidDimesion方法就是这样。
例子:
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class testFrame extends Frame
{
public testFrame(int width,int height)
{
this.setSize( width,height);
this.setLocation(testFrame.getMidDimesion( new Dimension(width,height)));
this.setBackground( Color.BLACK );
this.setVisible( true);
this.addWindowListener(new WindowListener()
{
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
});
}
public static Point getMidDimesion(Dimension d)
{
Point p = new Point();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
p.setLocation((dim.width - d.width)/2,(dim.height - d.height)/2);
return p;
}
public static void main(String[] args)
{
new testFrame(300,200);
}
}