【问题标题】:(Java) Explicitly/programmatically closing JPanel(Java) 显式/以编程方式关闭 JPanel
【发布时间】:2018-08-30 04:52:44
【问题描述】:

假设我有一个扩展 JPanel 的类:

class StockScreenResultsPanel extends JPanel{
    etc...
}

然后我有一个关于我刚刚从父选项卡中检索到的类的句柄:

StockScreenResultsPanel childComponent = (StockScreenResultsPanel)nasdaqTab.getComponent(); 

如果这真的是nooby,我很抱歉,但我想显式关闭 childComponent(不仅仅是隐藏它,而是从应用程序中删除它),我找不到任何示例。

有人可以告诉我如何显式/以编程方式关闭子 JPanel 吗?

我已经尝试了下面的代码并得到错误“StockScreenResultsPanel 无法转换为窗口”

childComponent.dispatchEvent(new WindowEvent( (Window)childComponent , WindowEvent.WINDOW_CLOSING));

另外,如果我尝试:

childComponent.dispose() 

它给了我一个“找不到符号”。

【问题讨论】:

  • 您是否尝试将其从JTabbedPane 中删除?由于JPanel 只是一个组件,它不像窗口那样“可关闭”
  • 您无法关闭面板。因此,您的解决方案都没有奏效(面板不是窗口,也不是一次性的,因为它只是一个JComponent。尝试removing 选项卡本身。

标签: java jpanel forceclose


【解决方案1】:

正如 cmets 在回答我上面的问题时所说,您无法关闭面板,您必须使用 remove 代替。

【讨论】:

    猜你喜欢
    • 2015-01-01
    • 2011-07-11
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2015-03-17
    • 1970-01-01
    • 2014-08-01
    相关资源
    最近更新 更多