Java 如何在JPanel中刷新/重新加载图像
当数据提交到数据库时,我需要重新加载JPanel的背景图像。 我创建了从数据库填充图像的JPanel。当我更新并提交图像时,背景会自动改变。 我也尝试使用repaint()和revalidate(),但它不起作用。 它必须重新启动应用程序并再次运行,才能正常工作 这是我在JPanel中显示背景的代码Java 如何在JPanel中刷新/重新加载图像,java,swing,jpanel,background-image,repaint,Java,Swing,Jpanel,Background Image,Repaint,当数据提交到数据库时,我需要重新加载JPanel的背景图像。 我创建了从数据库填充图像的JPanel。当我更新并提交图像时,背景会自动改变。 我也尝试使用repaint()和revalidate(),但它不起作用。 它必须重新启动应用程序并再次运行,才能正常工作 这是我在JPanel中显示背景的代码 public void getLogo(Company company, PanelCompany view) { JPanel panel = new BackgroundImag
public void getLogo(Company company, PanelCompany view) {
JPanel panel = new BackgroundImage(company.getLogoBlob());
panel.revalidate();
panel.setVisible(true);
panel.setBounds(10, 10, 120, 120);
view.getPanelPhoto().add(panel);
}
这是我的助手类:
public class BackgroundImage extends JPanel{
private Image image;
public BackgroundImage (InputStream input) {
try {
image = ImageIO.read(input);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
Graphics2D gd = (Graphics2D) grphcs.create();
gd.drawImage(image, 0, 0, getWidth(), getHeight(), this);
gd.dispose();
}
}
有什么解决办法吗?感谢您之前的关注:)首先,您的助手类应该设置自己的大小 其次,您应该只使用
JPanel
的Graphics
实例
public class BackgroundImage extends JPanel{
private Image image;
public BackgroundImage (InputStream input) {
try {
image = ImageIO.read(input);
setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
Graphics2D g2d = (Graphics2D) grphcs;
g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
现在你的电话是这样的
public void getLogo(Company company, PanelCompany view) {
JPanel panel = new BackgroundImage(company.getLogoBlob());
view.getPanelPhoto().add(panel);
}
您的PanelCompany
class必须使用布局管理器。给你
>选择一个.< /p> @ GiBeltLeBrc:考虑重写<代码> GestApple大小< /代码>,如建议的那样