本帖最后由 李知伦 于 2012-8-14 23:43 编辑
- import java.awt.*;
- import java.awt.event.*;
- public class TestDialog extends Frame//继承了Frame
- {
- TestDialog()
- {
- Button b=new Button("open a dialog");
- add(b,"Center");
- b.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- //传入owner参数时要用TestDialog.this来调用主方法中的对象,而不能是this或者new
- MyDialog dlg = new MyDialog(TestDialog.this, "Dialog");
- dlg.setVisible(true);
- }
- });
- setBounds(0,0,400,200);
- setVisible(true);
- addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- }
- public void setInfo()
- {
- System.out.println("TestDialog");
- }
- public static void main(String[] args)
- {
- new TestDialog();
- }
- }
- class MyDialog extends Dialog
- {
- private String strInfo = null;
- public MyDialog(Frame owner,String title) {
- super(owner,title);
- setBounds(0,0,200,150);
- Button b1=new Button("ok");
- this.add(b1,"Center");
- b1.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- //这里调用Test类对象时要用MyDialog.this.getOwner()或者getOwner()
- ((TestDialog)MyDialog.this.getOwner()).setInfo();
-
- }
- });
- }
- public void setInfo()
- {
- System.out.println("Dialog");
- }
- }
复制代码 那么如果Test不继承Frame类,而是创建一个Frame对象f来装按钮b
那么MyDialog中b1的监听器中 要调用Test的setInfo方法时,是否可行,如果可行应该怎么写
|
|