java小程序的窗口关闭问题
第一个小程序:importjava.awt.*;importjava.awt.event.*;publicclassTwoListenimplementsMouseLis...
第一个小程序:
import java.awt.*;
import java.awt.event.*;
public class TwoListen implements MouseListener,MouseMotionListener,WindowListener {
private Frame f;
private TextField tf;
public static void main(String argv[]){
TwoListen two=new TwoListen();
two.go();
}
public void go(){
f=new Frame("Two Listeners example");
f.add(new Label("click and drag the mouse"),"North");
tf=new TextField(30);
f.add(tf,"South");
f.addMouseListener( this);
f.addMouseMotionListener(this);
f.addWindowListener(this);
f.setSize(300,200);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e){
String s="Mouse dragging:X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){
String s="The mouse entered";
tf.setText(s);
}
public void mouseExited(MouseEvent e){
String s="The mouse has left the building";
tf.setText(s);
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
*************************************************************
第二个:
import java.awt.*;
import java.awt.event.*;
public class Interfaceforme extends WindowAdapter{
private Frame f;
private Button b;
public static void main(String argv[]){
Interfaceforme face=new Interfaceforme();
face.go();
}
public void go(){
f=new Frame("Two Listeners example");
f.add(new Label("click and drag the mouse"),"North");
b=new Button("hi");
f.add(b,"South");
f.setSize(300,200);
f.setVisible(true);
}
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowClosed(WindowEvent e){}
}
************************************************************
其中第一个程序运行后,点窗口的“X”可以正常关闭,而第二个却不能,一定要用任务管理器才行。请问到底是为什么??为什么第二个不能正常关闭呢?
第一个是可以关闭的!第二个不行,我想知道为什么,然后把第二个也变成可关闭的 展开
import java.awt.*;
import java.awt.event.*;
public class TwoListen implements MouseListener,MouseMotionListener,WindowListener {
private Frame f;
private TextField tf;
public static void main(String argv[]){
TwoListen two=new TwoListen();
two.go();
}
public void go(){
f=new Frame("Two Listeners example");
f.add(new Label("click and drag the mouse"),"North");
tf=new TextField(30);
f.add(tf,"South");
f.addMouseListener( this);
f.addMouseMotionListener(this);
f.addWindowListener(this);
f.setSize(300,200);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e){
String s="Mouse dragging:X="+e.getX()+"Y="+e.getY();
tf.setText(s);
}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){
String s="The mouse entered";
tf.setText(s);
}
public void mouseExited(MouseEvent e){
String s="The mouse has left the building";
tf.setText(s);
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
*************************************************************
第二个:
import java.awt.*;
import java.awt.event.*;
public class Interfaceforme extends WindowAdapter{
private Frame f;
private Button b;
public static void main(String argv[]){
Interfaceforme face=new Interfaceforme();
face.go();
}
public void go(){
f=new Frame("Two Listeners example");
f.add(new Label("click and drag the mouse"),"North");
b=new Button("hi");
f.add(b,"South");
f.setSize(300,200);
f.setVisible(true);
}
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowClosed(WindowEvent e){}
}
************************************************************
其中第一个程序运行后,点窗口的“X”可以正常关闭,而第二个却不能,一定要用任务管理器才行。请问到底是为什么??为什么第二个不能正常关闭呢?
第一个是可以关闭的!第二个不行,我想知道为什么,然后把第二个也变成可关闭的 展开
3个回答
展开全部
本来就不能啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.*;
import java.awt.event.*;
public class Interfaceforme extends WindowAdapter{
private Frame f;
private Button b;
public static void main(String args[]){
Interfaceforme face=new Interfaceforme();
face.go();
}
public void go(){
f=new Frame("Two Listeners example");
f.add(new Label("click and drag the mouse"),"North");
b=new Button("hi");
f.add(b,"South");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
}
);
f.setSize(300,200);
f.setVisible(true);
}
}
这样就能了,你的main方法中只有go()方法,但是go 方法中又没有提到关闭方法,所以你的窗口关不掉了.确切的说你的frame没有注册关闭,应该给frame添加windowslistener.
import java.awt.event.*;
public class Interfaceforme extends WindowAdapter{
private Frame f;
private Button b;
public static void main(String args[]){
Interfaceforme face=new Interfaceforme();
face.go();
}
public void go(){
f=new Frame("Two Listeners example");
f.add(new Label("click and drag the mouse"),"North");
b=new Button("hi");
f.add(b,"South");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
}
);
f.setSize(300,200);
f.setVisible(true);
}
}
这样就能了,你的main方法中只有go()方法,但是go 方法中又没有提到关闭方法,所以你的窗口关不掉了.确切的说你的frame没有注册关闭,应该给frame添加windowslistener.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询