[英]Calling outer class' method getContentPane() from inner Action class
I want to call outer class' method getContentPane() from inner Action class. I don't understand why my code doesn't work.我想从内部操作 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);
}
}
You must call validate();
你必须调用
validate();
method after getContentPane().add(...);
getContentPane().add(...);
. .
The validate method is used to cause a container to lay out its subcomponents again.
validate 方法用于使容器重新布置其子组件。 It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
在容器显示后修改此容器的子组件(添加到容器或从容器中删除,或更改布局相关信息)时应调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.