JFrame添加WindowAdapter监听,当试图关闭窗口时触发的问题
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.io.FileInputSt...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
public class jishiben
{
static JFrame frm = new JFrame();
static JDialog jdia = new JDialog(frm);
static JButton but = new JButton("OK");
public static void main(String[] args)
{
frm.setVisible(true);
frm.setBounds(100, 100, 200, 200);
frm.addWindowListener(new hello());
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jdia.setBounds(50, 50, 50, 50);
jdia.add(but);
// jdia.setVisible(true);
}
static class hello extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
jdia.setVisible(true);
}
public void windowLostFocus(WindowEvent e)
{
frm.setTitle("lost");
}
}
}
里面的两个函数,一个是失去焦点时设置标题,一个是点叉叉时弹出确认对话框,为什么实现不了 展开
import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
public class jishiben
{
static JFrame frm = new JFrame();
static JDialog jdia = new JDialog(frm);
static JButton but = new JButton("OK");
public static void main(String[] args)
{
frm.setVisible(true);
frm.setBounds(100, 100, 200, 200);
frm.addWindowListener(new hello());
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jdia.setBounds(50, 50, 50, 50);
jdia.add(but);
// jdia.setVisible(true);
}
static class hello extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
jdia.setVisible(true);
}
public void windowLostFocus(WindowEvent e)
{
frm.setTitle("lost");
}
}
}
里面的两个函数,一个是失去焦点时设置标题,一个是点叉叉时弹出确认对话框,为什么实现不了 展开
1个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class jishiben {
static JFrame frm = new JFrame();
static JDialog jdia = new JDialog(frm);
static JButton but = new JButton("OK");
public static void main(String[] args) {
frm.setVisible(true);
frm.setBounds(100, 100, 200, 200);
hello hello = new hello();
frm.addWindowListener(hello);
frm.addWindowFocusListener(hello);//添加WindowFocusListener
frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//改为点X不做任何操作
jdia.setBounds(50, 50, 100, 100);
jdia.add(but);
but.addActionListener(new ButtonAction());//添加点OK,关闭程序
}
private static final class ButtonAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
}
static class hello extends WindowAdapter {
public void windowClosing(WindowEvent e) {
jdia.setVisible(true);
}
public void windowLostFocus(WindowEvent e) {
frm.setTitle("lost");
}
}
}
//你没有仔细研究,Listener和Adapter的关系,并不是一一对应关系,如WindowAdapter对应WindowFocusListener, WindowListener, WindowStateListener, EventListener。
WindowListener并不会监听windowLostFocus事件。
//第二个问题:
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
这句话等于说一点X,就关闭程序,所以才显示不出确认对话框。
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class jishiben {
static JFrame frm = new JFrame();
static JDialog jdia = new JDialog(frm);
static JButton but = new JButton("OK");
public static void main(String[] args) {
frm.setVisible(true);
frm.setBounds(100, 100, 200, 200);
hello hello = new hello();
frm.addWindowListener(hello);
frm.addWindowFocusListener(hello);//添加WindowFocusListener
frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//改为点X不做任何操作
jdia.setBounds(50, 50, 100, 100);
jdia.add(but);
but.addActionListener(new ButtonAction());//添加点OK,关闭程序
}
private static final class ButtonAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
}
static class hello extends WindowAdapter {
public void windowClosing(WindowEvent e) {
jdia.setVisible(true);
}
public void windowLostFocus(WindowEvent e) {
frm.setTitle("lost");
}
}
}
//你没有仔细研究,Listener和Adapter的关系,并不是一一对应关系,如WindowAdapter对应WindowFocusListener, WindowListener, WindowStateListener, EventListener。
WindowListener并不会监听windowLostFocus事件。
//第二个问题:
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
这句话等于说一点X,就关闭程序,所以才显示不出确认对话框。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询