JAVA关闭窗口问题
importjava.awt.*;importjava.awt.event.*;publicclassTestscollextendsWindowAdapter{publ...
import java.awt.*;
import java.awt.event.*;
public class Testscoll extends WindowAdapter
{
public Testscoll()
{
Frame f = new Frame("javascollpane");
ScrollPane scr = new ScrollPane();
TextArea text = new TextArea("",50,50,TextArea.SCROLLBARS_NONE);
scr.add(text);
f.add(scr);
f.setSize(60,60);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
((Window)e.getComponent()).dispose();
System.out.print("quit");
System.exit(0);
}
});
}
public static void main(String []args)
{
new Testscoll();
}
}
运行后正常,但是窗口不能关闭,输出语句也没执行,哪里错了??
我是想问AWT中的事件处理,不用SWING的情况 展开
import java.awt.event.*;
public class Testscoll extends WindowAdapter
{
public Testscoll()
{
Frame f = new Frame("javascollpane");
ScrollPane scr = new ScrollPane();
TextArea text = new TextArea("",50,50,TextArea.SCROLLBARS_NONE);
scr.add(text);
f.add(scr);
f.setSize(60,60);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
((Window)e.getComponent()).dispose();
System.out.print("quit");
System.exit(0);
}
});
}
public static void main(String []args)
{
new Testscoll();
}
}
运行后正常,但是窗口不能关闭,输出语句也没执行,哪里错了??
我是想问AWT中的事件处理,不用SWING的情况 展开
4个回答
展开全部
窗口关闭事件是windowClosing,不是windowClosed。
windowClosed是父组件关闭它的时候调用。
import java.awt.*;
import java.awt.event.*;
public class Testscoll {
public Testscoll() {
Frame f = new Frame("javascollpane");
ScrollPane scr = new ScrollPane();
TextArea text = new TextArea("", 50, 50, TextArea.SCROLLBARS_NONE);
scr.add(text);
f.add(scr);
f.setSize(60, 60);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {// <--不是windowClosed,是windowClosing
((Window) e.getComponent()).dispose();
System.out.print("Closing");
System.exit(0);
}
});
}
public static void main(String[] args) {
new Testscoll();
}
}
windowClosed是父组件关闭它的时候调用。
import java.awt.*;
import java.awt.event.*;
public class Testscoll {
public Testscoll() {
Frame f = new Frame("javascollpane");
ScrollPane scr = new ScrollPane();
TextArea text = new TextArea("", 50, 50, TextArea.SCROLLBARS_NONE);
scr.add(text);
f.add(scr);
f.setSize(60, 60);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {// <--不是windowClosed,是windowClosing
((Window) e.getComponent()).dispose();
System.out.print("Closing");
System.exit(0);
}
});
}
public static void main(String[] args) {
new Testscoll();
}
}
展开全部
1. 添加import javax.swing.*;
2. Frame变成JFrame
3. 添加 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Testscoll extends WindowAdapter
{
public Testscoll()
{
JFrame f = new JFrame("javascollpane");
ScrollPane scr = new ScrollPane();
TextArea text = new TextArea("",50,50,TextArea.SCROLLBARS_NONE);
scr.add(text);
f.add(scr);
f.setSize(60,60);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
((Window)e.getComponent()).dispose();
System.out.print("quit");
System.exit(0);
}
});
}
public static void main(String []args)
{
new Testscoll();
}
}
2. Frame变成JFrame
3. 添加 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Testscoll extends WindowAdapter
{
public Testscoll()
{
JFrame f = new JFrame("javascollpane");
ScrollPane scr = new ScrollPane();
TextArea text = new TextArea("",50,50,TextArea.SCROLLBARS_NONE);
scr.add(text);
f.add(scr);
f.setSize(60,60);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
((Window)e.getComponent()).dispose();
System.out.print("quit");
System.exit(0);
}
});
}
public static void main(String []args)
{
new Testscoll();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
输入"exit",回车就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我觉得是你的监听事件有问题····具体说不出··感觉怪怪的·
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询