[英]Changing a JPanel to another JPanel doesn't repaint the change
I have a main JPanel target
in MainFraim and another currentView
JPanel, which is added to the target
panel. 我在MainFraim中有一个主JPanel
target
,另一个currentView
JPanel已添加到target
面板中。 target
panel contains buttons with listeners. target
面板包含带有监听器的按钮。 These listeners are then supposed the change the content of the curretView
panel, as demonstrated below: 然后假定这些侦听器更改了
curretView
面板的内容,如下所示:
private JPanel currentPanel;
public void setView(String type) {
if (type.equals("overall")) {
this.currentPanel = getOverallView();
frame.setTitle("BookingCalendar - Overall View");
frame.validate();
} else if (type.equals("guest")) {
this.currentPanel = getGuestView();
frame.setTitle("BookingCalendar - Room View");
frame.validate();
} else if (type.equals("room")) {
currentPanel.removeAll();
this.currentPanel = getRoomView();
frame.setTitle("BookingCalendar - Guest View");
frame.validate();
}
}
Every method I call returns new JPanel each time it is called: 我每次调用的每个方法都将返回新的JPanel:
JPanel currentPanel = new JPanel(new MigLayout("","20 [grow, fill] 10 [grow, fill] 20", "20 [] 10 [] 20"))
The problem is, whenever I call these methods, the panel won't change. 问题是,每当我调用这些方法时,面板都不会改变。 It always stays the same (by default:
getOverallView()
) 它始终保持不变(默认情况下:
getOverallView()
)
I've tried with invalidate, validate, repaint on both frame, as well as the panel, but no changes occur. 我尝试了在两个框架以及面板上进行无效,验证,重绘,但是没有发生任何变化。 Could someone please elaborate more as to what I need to do in order to completely change the content of the panel
currentView
有人可以详细说明我需要做些什么,以完全更改面板
currentView
的内容。
You shouldn't be creating a new JPanel() every time. 您不应该每次都创建一个新的JPanel()。 Instead create one for each type of view, and use a CardLayout to switch between them.
而是为每种类型的视图创建一个,并使用CardLayout在它们之间切换。 CardLayout lets you have JPanels that get switched out like cards.
使用CardLayout,您可以将JPanels像卡一样切换出去。
It looks like you're just trying to move pointers around, to make the currentView
point to a different panel. 看起来您只是在尝试移动指针,以使
currentView
指向其他面板。 What you actually need to do is remove the currentView
from the target
and then add the new panel. 您实际需要做的是从
target
删除currentView
,然后添加新面板。
If this is your 2 panels... 如果这是您的2个面板...
JPanel target = new JPanel();
JPanel currentView = new JPanel();
and you add the currentView
to the target
like so... 然后像这样将
currentView
添加到target
...
target.add(currentView);
Then to change your panel, you need to remove the existing panel from the target
, and add the new one... 然后,要更改面板,您需要从
target
删除现有面板,然后添加新面板...
target.remove(currentPanel);
currentPanel = new JPanel();
target.add(currentPanel);
另外,如果您不想使用CardLayout,则可以像执行操作一样使JPanel成为新的JPanel,只需调用revalidate()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.