Java 如何删除旧的 JPanel 并添加新的 JPanel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2487658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I remove an old JPanel and add a new one?
提问by Roman
I would like to remove an old JPanel from the Window (JFrame) and add a new one. How should I do it?
我想从窗口 (JFrame) 中删除旧的 JPanel 并添加一个新的。我该怎么做?
I tried the following:
我尝试了以下方法:
public static void showGUI() {
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(partnerSelectionPanel);
frame.setSize(600,400);
frame.setVisible(true);
}
private static void updateGUI(final int i, final JLabel label, final JPanel partnerSelectionPanel) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
label.setText(i + " seconds left.");
}
partnerSelectionPanel.setVisible(false); \ <------------
}
);
}
My code updates the "old" JPanel and then it makes the whole JPanel invisible, but it does not work. The compiler complains about the line indicated with <------------
. It writes: <identifier> expected, illegal start of type
.
我的代码更新了“旧”JPanel,然后它使整个 JPanel 不可见,但它不起作用。编译器抱怨用 指示的行<------------
。它写道:<identifier> expected, illegal start of type
。
ADDED:
添加:
I have managed to do what I needed and I did it in the following way:
我已经设法做我需要做的事情,我是通过以下方式做到的:
public static void showGUI() {
frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(partnerSelectionPanel);
//frame.add(selectionFinishedPanel);
frame.setSize(600,400);
frame.setVisible(true);
}
public static Thread counter = new Thread() {
public void run() {
for (int i=4; i>0; i=i-1) {
updateGUI(i,label);
try {Thread.sleep(1000);} catch(InterruptedException e) {};
}
partnerSelectionPanel.setVisible(false);
frame.add(selectionFinishedPanel);
}
};
It works but it does not look to me like a safe solution for the following reasons:
它有效,但由于以下原因,它在我看来并不像一个安全的解决方案:
- I change and add elements to the JFrame from another thread.
- I add a new JPanel to a JFrame, after I have already "packed" the JFrame and made it visible.
- 我从另一个线程更改元素并将其添加到 JFrame。
- 在我已经“打包”了 JFrame 并使其可见之后,我向 JFrame 添加了一个新的 JPanel。
Should I be doing that?
我应该这样做吗?
采纳答案by Russ Hayward
setVisible(false), even in the correct place, will not actually remove the panel from the container. If you want to replace the panel do this:
setVisible(false),即使在正确的位置,实际上也不会从容器中移除面板。如果要更换面板,请执行以下操作:
frame.getContentPane().remove(partnerSelectionPanel);
frame.add(new JPanel());
frame.getContentPane().invalidate();
frame.getContentPane().validate();
Note that frame.getContentPane().add(Component) is the same as frame.add(Component) - the components are actually contained within the content pane.
请注意, frame.getContentPane().add(Component) 与 frame.add(Component) 相同 - 组件实际上包含在内容窗格中。
回答by Roman
partnerSelectionPanel.setVisible(false); \ <------------
This line is actualy out the method run.
这一行实际上是方法运行。
You probably want something like this:
你可能想要这样的东西:
public void run() {
label.setText(i + " seconds left.");
try {
Thread.sleep (i * 1000);
} catch (InterruptedException e) {
handleException (e);
}
partnerSelectionPanel.setVisible(false);
}
回答by Wintermut3
Don't forget or overlook the approach of using the Layout, namely the CardLayoutas the Frames Layout, to allow this type of behavior (This is a good strategy for a "Wizard" for example). One advantage to this is it doesn't cause any weird flash or draw effects as that is what this Layout is meant to do--Allow a panels to be swapped out, assuming they have exclusive "real estate" or can share the same areas (i.e. "Wizard" like behavior.)
不要忘记或忽略使用布局的方法,即CardLayout作为框架布局,以允许这种类型的行为(例如,这是一个“向导”的好策略)。这样做的一个优点是它不会导致任何奇怪的闪光或绘制效果,因为这正是此布局的目的——允许面板被换出,假设它们具有专属的“房地产”或可以共享相同的区域(即“巫师”之类的行为。)
回答by sayem siam
You can use
您可以使用
Frame.setContentPane(jPanel);