更新时间:2023-12-05 08:21:52
您尚未在main方法的mainFrame的内容窗格中添加任何组件.添加面板的唯一时间是在此ActionListener中:
You have not added any components to the mainFrame's content pane in the main method. The only time a panel gets added is in this ActionListener:
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AssignToTransientPanel((JPanel) new NewAlbum());
Container content = mainFrame.getContentPane();
content.removeAll();
content.add(transientPanel);
content.validate();
content.repaint();
}
});
仅当您单击打开"时才调用此函数,我想是偶然地将ActionListener添加到了打开的JMenuItem中,而不是newAlbum JMenuItem中.要在启动时添加内容,您需要在mainFrame.setVisible(true)行之前添加以下内容:
This is only getting called when "Open" is clicked as you have, I assume accidentally, added the ActionListener to the open JMenuItem rather than the newAlbum JMenuItem. To add content on startup you need to add something like this before the mainFrame.setVisible(true) line:
mainFrame.add(new NewAlbum());
顺便说一句,约定是Java源代码中的所有方法均以小写字母开头.对于您的方法,assignToTransientPanel会是一个更好的名称.
BTW, the convention is for all methods in Java source code to start with a lower case letter. assignToTransientPanel would be a better name for your method.