JPanel removeAll不会摆脱以前的组件 [英] JPanel removeAll doesn't get rid of previous components

查看:370
本文介绍了JPanel removeAll不会摆脱以前的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个swing应用程序,其中可以在JPanel中显示图像.如果应用程序无法生成图像,我想从JPanel中删除前一个图像,并用JTextField和消息替换它.我可以添加文本字段,但它是在先前内容的顶部绘制的,该内容本身是JPanel的子类.这就是我所拥有的:

I have a swing application in which I display images in a JPanel. If the app is unable to produce the image I want to remove the previous one from the JPanel and replace it with a JTextField and message. I can add the text field , but it's drawn on top of the previous contents, which is itself a subclass of JPanel. Here's what I have:

private void displayMessage(String message) {
  JTextField tf = new JTextField(message);
  cdPanel.removeAll();
  cdPanel.add(tf, BorderLayout.NORTH);//tried lots of variations, inc. no layout    
  cdPanel.validate();
}

如何使cdPanel完全重绘?

How can I get cdPanel to completely redraw itself?

推荐答案

您可以简单地尝试致电:

You can simply try calling :

cdPanel.revalidate();
cdPanel.repaint();   // This is required in some cases

代替

cdPanel.validate();

这篇关于JPanel removeAll不会摆脱以前的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆