如何实现java中点击按钮,退出窗体事件。
我的代码如下
import java.util.Scanner;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JFrame;
import java.awt.event.WindowListener;
import java.awt.Button;
public class close extends Frame implements ActionListener
{
Panel myPanel1;
Button bt;
close()
{
super("");
super.setTitle("退出系统");
setBounds(180,180,180,180);
setLayout(null);
bt=new Button("退出");
myPanel1=new Panel();
myPanel1.setBounds(50,50,50,50);
myPanel1.add(bt);
add(myPanel1);
setVisible(true);
}
public void windowClosing(ActionEvent arg0){
bt.setVisible(false);
System.exit(0);
}
public static void main(String[] args)
{
new close();
}
}
会提示 close 不是抽象的并且未覆盖java.awt.envet.ActionListener中的抽象方法请问这是为什么
还有如何实现点击右上角的X退出窗体功能0.0
啊,对了,这段代码使用TXT编写的
------解决方案--------------------
- Java code
import java.awt.Button;import java.awt.Frame;import java.awt.Panel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class close extends Frame implements ActionListener{ Panel myPanel1; Button bt; close(){ super(""); super.setTitle("退出系统"); setBounds(180,180,180,180); setLayout(null); bt=new Button("退出"); myPanel1=new Panel(); myPanel1.setBounds(50,50,50,50); myPanel1.add(bt); add(myPanel1); bt.addActionListener(this); setVisible(true); } public void windowClosing(ActionEvent arg0){ bt.setVisible(false); System.exit(0); } public static void main(String[] args){ new close(); } public void actionPerformed(ActionEvent e){ windowClosing(e); }}
------解决方案--------------------
你的代码问题很对,给个代码参考下吧
- Java code
package csdn;import java.awt.Button;import java.awt.Frame;import java.awt.Panel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Close extends Frame { Panel myPanel1; Button bt; Close() { super(""); super.setTitle("退出系统"); setBounds(180, 180, 180, 180); setLayout(null); bt = new Button("退出"); myPanel1 = new Panel(); myPanel1.setBounds(50, 50, 50, 50); myPanel1.add(bt); add(myPanel1); setVisible(true); //让按钮能退出 bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); // 让窗体关闭按有效 addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { new Close(); }}