gpt4 book ai didi

java - 从内部 Action 类调用外部类的方法 getContentPane()

转载 作者:行者123 更新时间:2023-12-01 15:27:14 24 4
gpt4 key购买 nike

我想从内部 Action 类调用外部类的方法 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);
}
}

最佳答案

您必须在 getContentPane().add(...); 之后调用 validate(); 方法。

The validate method is used to cause a container to lay out its subcomponents again. 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.

关于java - 从内部 Action 类调用外部类的方法 getContentPane(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10009063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com