[英]Calling outer class' method getContentPane() from inner Action class
我想从内部操作 class 调用外部类的方法 getContentPane()。我不明白为什么我的代码不起作用。
public class MainFrame extends JFrame {
public MainFrame() {
super("My app");
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu myMenu = new JMenu("File");
menuBar.add(myMenu);
Action myAction = new AbstractAction("Do everything") {
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel();
panel.setBackground(Color.CYAN);
getContentPane().add(panel, BorderLayout.CENTER);
}
};
myMenu.add(myAction);
}
}
你必须调用validate();
getContentPane().add(...);
.
validate 方法用于使容器重新布置其子组件。 在容器显示后修改此容器的子组件(添加到容器或从容器中删除,或更改布局相关信息)时应调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.