import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class TestPanel extends JPanel
{
private JButton button;
public TestPanel(JFrame jf)
{
button=new JButton("button");
add(button);
/////////匿名内部类
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
/////加上你的操作
}
});
}
}
public class Test
{
public static void main(String [] args)
{
JFrame jf=new JFrame("test");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300,200);
TestPanel test=new TestPanel(jf);
jf.add(test);
jf.setVisible(true);
}
}