gpt4 book ai didi

java - JOptionPane.showMessageDialog 后不显示背景图像

转载 作者:行者123 更新时间:2023-11-29 06:04:14 24 4
gpt4 key购买 nike

我的问题是,当我使用

创建消息对话框时
JOptionPane.showMessageDialog( ... )

在显示将图像绘制为背景的 JPanel 的应用程序上(取自:java swing: how to add an image to a jpanel),不显示背景图像,因此我必须最小化和最大化应用程序以恢复背景图像。

到目前为止,我只能通过执行以下操作来取回背景图像:

app.getApplication().getMainFrame().repaint();

但它只有在我关闭消息对话框后才有效。

有什么想法吗?

最佳答案

您发布的链接,因为他正在使用文件访问图像,在我看来这不是访问应用程序资源的好方法,因为您必须使用 URL。看看这个示例代码,并检查你哪里出了问题:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ImageTest extends JPanel
{
private BufferedImage image;

private void displayGUI()
{
JFrame frame = new JFrame("Image Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

try
{
setImage(new URL("http://gagandeepbali.uk.to/" +
"gaganisonline/images/planetbackground.jpg"));
}
catch(MalformedURLException mue)
{
mue.printStackTrace();
}


frame.setContentPane(this);
frame.pack();
frame.setVisible(true);
JOptionPane.showMessageDialog(frame,
"I am working.",
"Image Working ?",
JOptionPane.QUESTION_MESSAGE);
}

private void setImage(URL path)
{
try
{
System.out.println(path);
image = ImageIO.read(path);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}

@Override
public Dimension getPreferredSize()
{
return (new Dimension(image.getWidth(), image.getHeight()));
}

@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}

public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ImageTest().displayGUI();
}
});
}
}

关于java - JOptionPane.showMessageDialog 后不显示背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9091298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com