2010-03-21 28 views
0

我可以在不參考構造函數中的對象的情況下執行此操作嗎? 換句話說,任何類從FrmTaoChild創造必須的,當添加主窗口Java:從JInternalFrame獲取Mainform對象

public class FrmTaoMain extends JFrame { 
    JToolBar tbTask = new JToolBar(); 
    public FrmTaoMain(String Caption) { 
    super(Caption); 
    ... 
    FrmTaoChild FrmChild = new FrmTaoChild(tbTask,"test"); 

    } 
} 

public class FrmTaoChild extends JInternalFrame { 
    public FrmTaoChild(JToolBar tbTask, String Caption) 
    { 
    super (Caption); 
    JButton btnTask = new JButton(Caption); 
    tbTask.add(btnTask); 
    } 
} 

回答

2

正如How to Use Internal Frames討論的工具欄上的按鈕繼承,「通常情況下,你添加的內部框架到桌面窗格」。而不是通過JToolBar作爲參數,考慮讓FrmTaoChild提供ActionFrmTaoMain可用於相應的JToolBar按鈕。有關更多信息,請參見How to Use Actions

另外,Java中的變量名通常以小寫字母開頭。

public class FrmTaoChild extends JInternalFrame { 

    private Action action; 

    public FrmTaoChild(String caption) { 
     super(caption); 
     action = new AbstractAction(caption) { ... } 
    } 

    public Action getAction() { 
     return action; 
    } 
}