import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MyFrame extends JFrame{
private static final long serialVersionUID = 1L;
private static MyFrame frame;
public static void main(String[] args) {
//使用EventQueue.invokeLater(new Runnable(){})保证窗体创建更安全
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame=new MyFrame();
frame.setVisible(true);//窗体是否显示 1、 true 显示 2、false 不显示
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyFrame() {
this.setTitle("新窗口");//设置窗口名称
this.setBounds(30,0,600,200);//设置窗口初始化的位置(a,b),和大小(width,height)
/*
* 设置窗体大小,可通过setBounds实现
* this.setSize(200, 200);
* 设置窗体位置,可通过setBounds实现
* this.setLocation(100,100);
* 窗体全屏显示
* this.setExtendedState( JFrame.MAXIMIZED_BOTH );
* 窗体居中显示
* this.setLocationRelativeTo(null);
*/
this.setResizable(true);//设置是否可以调节窗口大小,默认可以 1、 true 可以 2、false 不可以
/*
* JFrame.DO_NOTHING_ON_CLOSE 删不了
* JFrame.HIDE_ON_CLOSE 隐藏起来其实是不删
* JFrame.DISPOSE_ON_CLOSE 删了这一个
* JFrame.EXIT_ON_CLOSE 全部删了
*/
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭按钮
//设置窗体关闭响应事件
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //加上这句,否则点No也会退出
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e)
{
int option=JOptionPane.showConfirmDialog(
MyFrame.this, "确定退出系统? ", "提示 ",JOptionPane.YES_NO_CANCEL_OPTION);
if(option==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
}
}
Swing JFrame使用实例
最新推荐文章于 2024-05-29 08:30:27 发布