1、窗口关闭时有弹出对话框提示;
2、使用系统的外观;
3、窗体屏幕居中,可缩放;
package com.yhj;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane = null;
private int width = 800;
private int height = 600;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
// setResizable(false);
// setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/com/sun/java/swing/plaf/windows/icons/HardDrive.gif")));
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int exi = JOptionPane.showConfirmDialog(null, "确定要退出吗?",
"提示", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (exi == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
});
setTitle("\u4F01\u4E1A\u8FDB\u9500\u5B58\u7BA1\u7406\u7CFB\u7EDF");
setFont(new Font("微软雅黑", Font.PLAIN, 14));
Dimension size = getToolkit().getScreenSize();
setLocation((size.width - width) / 2, (size.height - height) / 2);
setSize(width, height);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}