简体   繁体   English

从NetBeans中的JFrame删除JPanel

[英]Removing JPanel from a JFrame in NetBeans

I have several JPanels that contain buttons, labels, etc. that I want to switch between from a main JFrame. 我有几个包含按钮,标签等的JPanel,我想从主JFrame之间进行切换。 Currently I am trying to use the this.add(JPanelname); 目前,我正在尝试使用this.add(JPanelname); method and this.remove(JPanelname); 方法和this.remove(JPanelname); with the validate(); validate(); and repaint(); repaint(); methods 方法

The problem is it will add the panel to the JFrame but it will not remove it. 问题在于它将面板添加到JFrame但不会将其删除。 I am not sure how exactly to go about this. 我不确定该怎么做。

Maybe you should be using a Card Layout . 也许您应该使用卡片布局

Or maybe you should be using modal JDialogs. 也许您应该使用模式JDialogs。 So whenever you click on the "widjet" a new window is displayed. 因此,无论何时单击“ widjet”,都会显示一个新窗口。 Then when you close the dialog you are back on your main frame. 然后,当您关闭对话框时,您将回到主框架。

If you are constantly switching between JPanels, then a JTabbedPane may be the right thing to use. 如果您经常在JPanels之间切换,那么使用JTabbedPane可能是正确的选择。 It should not be necessary to call "validate" or "repaint" when you add or remove a JPanel. 添加或删除JPanel时,不必调用“ validate”或“ repaint”。 Do you have a layout manager installed? 您是否安装了布局管理器? Do you make sure to call add/remove only within the UI event thread? 您确定只在UI事件线程中调用添加/删除吗? Also, typically one does not call "validate()" but rather "invalidate()" to invalidate the container for updates. 同样,通常不调用“ validate()”而是调用“ invalidate()”以使容器无效以进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM