[java]代码库
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
class MyFrame extends JFrame {
JButton myButton;
JTextArea myTextArea;
int count;
public MyFrame(String title) {
super(title);
myButton = new JButton("click me");
myTextArea = new JTextArea();
add(myButton);
add(myTextArea);
ButtonListener bList = new ButtonListener();
myButton.addActionListener(bList);
}
class ButtonListener implements ActionListener //这里定义了一个内部类
{
public void actionPerformed(ActionEvent e) {
count++;
myTextArea.setText("button clicked" + count + "times");
}
}
// end of innerclass ButtonListener
public static void main(String args[]) {
MyFrame f = new MyFrame("Inner Class Frame");
f.setSize(300, 300);
f.setVisible(true);
f.show();
}
}
//源代码片段来自云代码http://yuncode.net