想要实现点击关闭按钮接着弹出窗口,下面是代码,点击关闭按钮后窗口关闭了,求解
import java.awt.*;
import java.awt.event.*;import javax.swing.*;class Chen extends JFrame implements ActionListener{
JButton button1;
JButton button;
public Chen(){
Sum();
}
public void Sum(){//调动窗口方法
init();
this.setVisible(true);
button.addActionListener(this);
button1.addActionListener(this);
this.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing ( WindowEvent e ){ //实现点击关闭按钮弹出窗口
CloseWindow();
Sum();//重新调动窗口
}
});
}
private void init(){ //创建窗口的方法
this.setLayout(new FlowLayout());
this.setTitle("测试窗口");
this.setBounds(100, 110, 200, 250);
setBackground(Color.pink);
JLabel biaoqian=new JLabel(" 你喜欢这本书吗? ");
add(biaoqian);
button=new JButton("喜欢");
add(button);
button.setBackground(Color.pink);
button1=new JButton("不喜欢");
add(button1);
button1.setBackground(Color.black);
}
@Override
public void actionPerformed(ActionEvent e){//按钮响应方法
if(e.getActionCommand().equals("喜欢"))
JOptionPane.showMessageDialog(null, "感谢您的认可");
if(e.getActionCommand().equals("不喜欢"))
JOptionPane.showMessageDialog(null, "我们会努力进步的","感谢您的参与",JOptionPane.WARNING_MESSAGE);
}
public void CloseWindow(){ //关闭窗口的方法
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class exemple1 {
public static void main(String[] args){
Chen c=new Chen();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货