Can I do this without reference to the object in the constructor? In other words, any class inherited from FrmTaoChild when creating must to add the button on the toolbar of the main window开发者_如何学运维
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);
}
}
As discussed in How to Use Internal Frames, "Usually, you add internal frames to a desktop pane." Instead of passing JToolBar
as a parameter, consider having FrmTaoChild
supply an Action
that FrmTaoMain
can use for the corresponding JToolBar
button. See How to Use Actions for more.
As an aside, variable names in Java usually begin with lower case.
public class FrmTaoChild extends JInternalFrame {
private Action action;
public FrmTaoChild(String caption) {
super(caption);
action = new AbstractAction(caption) { ... }
}
public Action getAction() {
return action;
}
}
精彩评论