【问题标题】:Making JMenuItem show one JPanel and hide another使 JMenuItem 显示一个 JPanel 并隐藏另一个
【发布时间】:2014-12-29 14:58:34
【问题描述】:

我希望能够通过选择某个 JMenuItem 在我的框架中的两个可能的 JPanel 之间进行切换。到目前为止我尝试了什么:

我的 JMenuBar 类中的动作监听器:

@Override
public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(fullList))
            gui.switchToFullList();
        else if (e.getSource().equals(history))
            gui.switchToHistory();
}

在 GUI 类中:

void switchToFullList() {
    remove(history);
    add(fullList);
}

void switchToHistory() {
    remove(fullList);
    add(history);
}

historyfullList 是 JPanel。

这似乎并没有以任何方式修改我的框架。

【问题讨论】:

标签: java swing actionlistener jmenuitem


【解决方案1】:

如果您想显示一个面板并隐藏另一个面板,它们都应该是您的 Frame 的子项,那么您可以通过 frame.JpanelName 访问这些面板。

删除历史并添加完整列表的示例:

frame.remove(frame.history);
frame.getContentPane().add(frame.fullList);
frame.validate();
frame.repaint();

【讨论】:

  • frame.add(...)frame.getContentPane().add(...)有什么区别?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2012-02-25
相关资源
最近更新 更多