初学JAVA,请大大指教addWindowListener getText()

来源:百度知道 编辑:UC知道 时间:2024/06/17 21:25:25
源代码如下:
import java.awt.*;
import java.awt.event.*;
public class TestButton
{
public static void main(String args[])
{
Frame f=new Frame("事件测试");
Button b=new Button("请点击本按钮");
TextField txt=new TextField();
b.addActionListener(new ButtonHandler());
txt.addActionListener(new PressEnter());
f.setLayout(new FlowLayout());
f.add(b);
f.add(txt);
f.setSize(200,200);
f.setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("key pressed!");
}
}
class PressEnter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println

顶自学.

代码的结构不好.最好不要把代码放在main里面。我做的改动在代码结构方面也是不可取的。

建议多看看别人的例子。

////////////////////////////

import java.awt.*;
import java.awt.event.*;
public class TestButton
{
public static TextField txt; ///////////////
public static void main(String args[])
{
Frame f=new Frame("Event test");
Button b=new Button("Press it");
txt=new TextField(); ///////////////
b.addActionListener(new ButtonHandler());
txt.addActionListener(new PressEnter());
f.setLayout(new FlowLayout());
f.add(b);
f.add(txt);
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() ///////////
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("key pressed!&qu