请看下面的代码:
package ui;
import java.awt.Color;
public class AddDataView extends JFrame implements ActionListener {
private JPanel contentPane;
private JTextField playGameTimeTextField;
private JTextField studyTimeTextField;
private JTextField readTechnologyTimeTextField;
private JTextField officialTimeTextField;
private JTextField otherTimeTextField;
FontStyle fontStyle = new FontStyle();
Font gearFont = fontStyle.getGearFont();
Font xingshupenFont=fontStyle.getXingBiPenFont();
Font fameFont=fontStyle.getFameFont();
static StateOfOneDay stateOfOneDay;
StoreAndLoad storeAndLoad;
ShowEfficiencyDialog showEfficiencyDialog;
public AddDataView() {
setTitle("添加记录");
setIconImage(Toolkit.getDefaultToolkit().getImage("E:\\javaSE\u4EE3\u7801\\TimeManager\\asset\\icon.jpg"));
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//要后退到父窗口,这个方法的参数就得是这个。其他的参数,自己试验试验就知道有何区别了。
setBounds(400, 200, 398, 390);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel playGameTimeLabel = new JLabel("\u6E38\u620F\u65F6\u95F4\uFF1A");
playGameTimeLabel.setFont(gearFont);
playGameTimeLabel.setBounds(85, 60, 100, 28);
contentPane.add(playGameTimeLabel);
JLabel studyTimeLabel = new JLabel("\u5B66\u4E60\u65F6\u95F4\uFF1A");
studyTimeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
studyTimeLabel.setFont(gearFont);
studyTimeLabel.setBounds(78, 98, 107, 28);
contentPane.add(studyTimeLabel);
JLabel readTechnologyTimeLable = new JLabel("\u9605\u8BFB\u65F6\u95F4\uFF1A");
readTechnologyTimeLable.setHorizontalAlignment(SwingConstants.RIGHT);
readTechnologyTimeLable.setFont(gearFont);
readTechnologyTimeLable.setBounds(85, 136, 100, 28);
contentPane.add(readTechnologyTimeLable);
JLabel officialTimeLabel = new JLabel("\u516C\u52A1\u65F6\u95F4\uFF1A");
officialTimeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
officialTimeLabel.setFont(gearFont);
officialTimeLabel.setBounds(78, 172, 107, 28);
contentPane.add(officialTimeLabel);
JLabel otherTimeLabel = new JLabel("\u522B\u7684\u65F6\u95F4\uFF1A");
otherTimeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
otherTimeLabel.setFont(gearFont);
otherTimeLabel.setBounds(78, 216, 107, 28);
contentPane.add(otherTimeLabel);
playGameTimeTextField = new JTextField();
playGameTimeTextField.setHorizontalAlignment(SwingConstants.CENTER);
playGameTimeTextField.setBounds(195, 60, 82, 21);
playGameTimeTextField.setFont(fameFont);
contentPane.add(playGameTimeTextField);
playGameTimeTextField.setColumns(10);
studyTimeTextField = new JTextField();
studyTimeTextField.setHorizontalAlignment(SwingConstants.CENTER);
studyTimeTextField.setColumns(10);
studyTimeTextField.setBounds(195, 105, 82, 21);
studyTimeTextField.setFont(fameFont);
contentPane.add(studyTimeTextField);
readTechnologyTimeTextField = new JTextField();
readTechnologyTimeTextField.setHorizontalAlignment(SwingConstants.CENTER);
readTechnologyTimeTextField.setColumns(10);
readTechnologyTimeTextField.setBounds(195, 143, 82, 21);
readTechnologyTimeTextField.setFont(fameFont);
contentPane.add(readTechnologyTimeTextField);
officialTimeTextField = new JTextField();
officialTimeTextField.setHorizontalAlignment(SwingConstants.CENTER);
officialTimeTextField.setColumns(10);
officialTimeTextField.setBounds(195, 179, 82, 21);
officialTimeTextField.setFont(fameFont);
contentPane.add(officialTimeTextField);
otherTimeTextField = new JTextField();
otherTimeTextField.setHorizontalAlignment(SwingConstants.CENTER);
otherTimeTextField.setColumns(10);
otherTimeTextField.setBounds(195, 216, 82, 21);
otherTimeTextField.setFont(fameFont);
contentPane.add(otherTimeTextField);
JButton commitButton = new JButton("\u67E5\u770B&\u4FDD\u5B58");
commitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
commitButton.setBackground(Color.WHITE);
commitButton.setFont(xingshupenFont);
commitButton.setBounds(108, 282, 169, 42);
commitButton.addActionListener(this);
contentPane.add(commitButton);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
if (playGameTimeTextField.getText().equals("")
||studyTimeTextField.getText().equals("")
||readTechnologyTimeTextField.getText().equals("")
||officialTimeTextField.getText().equals("")
||otherTimeTextField.getText().equals("")) {
int result=JOptionPane.showConfirmDialog(AddDataView.this,"您的输入还没有完成,确定退出?","Exit",JOptionPane.OK_CANCEL_OPTION);
if (result==JOptionPane.OK_OPTION) {
AddDataView.this.dispose();
}
}
}
});
}
public void actionPerformed(ActionEvent e){
stateOfOneDay=new StateOfOneDay(Double.parseDouble(playGameTimeTextField.getText()),
Double.parseDouble(studyTimeTextField.getText()),
Double.parseDouble(otherTimeTextField.getText()),
Double.parseDouble(readTechnologyTimeTextField.getText()),
Double.parseDouble(officialTimeTextField.getText()));
String[] dataOfOneDay=new String[]{
stateOfOneDay.getPlayGameTime(),
stateOfOneDay.getStudyTime(),
stateOfOneDay.getOtherTime(),
stateOfOneDay.getReadTechnologyTime(),
stateOfOneDay.getOfficialTime(),
stateOfOneDay.getEfficiency(),
stateOfOneDay.getState()
};
storeAndLoad=new StoreAndLoad();
storeAndLoad.writeOneDayIntoFile(dataOfOneDay);
showEfficiencyDialog=new ShowEfficiencyDialog();
showEfficiencyDialog.show();
}
}
可以看到,其中起到关键作用的是setDefaultCloseOperation方法。另外,要注意的是,要弹出窗口来提示用户,就必须重写WindowsListener的windowClosing方法,这个方法表明用户点击了窗口右上角的关闭按钮,可以重写这个方法实现自己想要的操作,比如说弹出一个对话框提示用户是否确定要退出。选择确定的话,就退出,选择取消就留在原窗口。