class myfrm extends JFrame
{
public MyPanel panel1;public myfrm()
{
panel1 = new MyPanel(this);
}
public void beep()
{}
}class MyPanel extends JPanel
{
private JFrame parent;
public MyPanel(JFrame fr)
{
parent = fr;
myActionListener ml = new myActionListener();
bt.addActionListener(ml);
}
private JButton bt;class myActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
parent.beep();
}
}}
{
public MyPanel panel1;public myfrm()
{
panel1 = new MyPanel(this);
}
public void beep()
{}
}class MyPanel extends JPanel
{
private JFrame parent;
public MyPanel(JFrame fr)
{
parent = fr;
myActionListener ml = new myActionListener();
bt.addActionListener(ml);
}
private JButton bt;class myActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
parent.beep();
}
}}
Button btn=new Button("Button");
final MyFrame frame=new MyFrame();
panel.add(btn);
frame.add(panel);
btn.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.beep();
}
});
{
public p(Container c){
c.beep();
}
}class f extends JFrame
{
public beep(){...}
}在调用的时候把f的实例作为参数传入.
当然还有p.getParent()方法可以得到父类
class MyPanel p extends JPanel
{
JFrame frame;
JButton button=new JButton("show");
public p(JFrame f){
frame=f;
add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
frame.beep();
}
});
}
}class f extends JFrame
{
public beep(){...}
}
不知道你想要的是不是这种写法?