import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//让框架居中显示及Frame框架关闭按钮的功能实现
public class MyFrame extends Frame implements WindowListener {
public MyFrame(){
super("this is frame"); //设置窗口标题
setSize(400, 300); //设置窗口尺寸
//获取屏幕的高度和宽度
int w=this.getWidth(); //获取框架的宽度
int h=this.getHeight(); //获取框架的高度
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
double screenWidth=screenSize.getWidth(); //获取屏幕的宽度
double screenHeith=screenSize.getHeight(); //获取屏幕的高度
int x=(int)(screenWidth-w)/2;
int y=(int)(screenHeith-h)/2;
//设置框架显示的位置
setLocation(x, y);
// this.setBounds((int)((screenWidth)/2-this.getWidth()),(int)((screenHeith/2)-this.getHeight()), 200, 200); // 100,100分别为距离x,y轴的距离,后面的x,y分别框架的长和宽
this.setLayout(new FlowLayout()); //设置布局管理器为流布局管理器
this.add(new TextField("hello welocme to java!"));
this.addWindowListener(this); //添加窗口监听器
this.setVisible(true); //设置窗口可见 true可见,false 不可见
}
public static void main(String[] args) {
new MyFrame(); //实例化
}
public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
} //关闭窗口方法
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
java中Frame框架关闭按钮和框架居中的设置方法
最新推荐文章于 2022-11-04 10:51:01 发布