如何删除旧的 JPanel 并添加新的 JPanel?
我想从窗口(JFrame)中删除旧的 JPanel 并添加一个新的。我该怎么做呢?
我尝试了以下操作:
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); \\ <------------
}
);
}
我的代码更新了“旧”JPanel,然后它使整个 JPanel 不可见,但它不起作用。编译器抱怨用 <------------
指示的行。它写道:
。
添加:
我已经成功地完成了我需要的操作,并通过以下方式完成了它:
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);
}
};
它有效,但在我看来它不像是一个安全的解决方案,原因如下:
- 我更改并添加了元素来自另一个线程的 JFrame。
- 在我已经“打包”JFrame 并使其可见之后,我将一个新的 JPanel 添加到 JFrame 中。
我应该这样做吗?
I would like to remove an old JPanel from the Window (JFrame) and add a new one. How should I do it?
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
.
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.
Should I be doing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
setVisible(false),即使在正确的位置,实际上也不会从容器中删除面板。如果要替换面板,请执行以下操作:
请注意,frame.getContentPane().add(Component) 与frame.add(Component) 相同 - 组件实际上包含在内容窗格中。
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:
Note that frame.getContentPane().add(Component) is the same as frame.add(Component) - the components are actually contained within the content pane.
不要忘记或忽视使用布局的方法,即 CardLayout 作为框架布局,以允许这种类型的行为(例如,对于“向导”来说这是一个很好的策略)。这样做的一个优点是它不会导致任何奇怪的闪光或绘制效果,因为这就是此布局的目的 - 允许面板被交换,假设它们具有专有的“不动产”或可以共享相同的区域(即“向导”般的行为。)
Don't forget or overlook the approach of using the Layout, namely the CardLayout as 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.)
您可以使用
You can use
该行实际上是方法运行的结果。
你可能想要这样的东西:
This line is actualy out the method run.
You probably want something like this: