import java.awt.*;
import java.awt.event.*;
/*
组件与监听分离
*/
class FrameDemo
{
private Frame f;
private Button but;
FrameDemo()
{
init();
}
public void init()
{
Frame f = new Frame();
f.setBounds(200,300,500,300);
f.setLayout(new FlowLayout());
but = new Button("Button");
f.add(but);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
f.addWindowListener (new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("按钮操作");
System.exit(0);
}
});
}
public static void main(String[] args)
{
new FrameDemo();
}
}
|
|