gpt4 book ai didi

java - JOptionPane.showMessageDialog() 显示但没有任何消息?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:15:52 25 4
gpt4 key购买 nike

在下面的代码中,我在 try/catch block 中调用了 JOptionPane.showMessageDialog。但是当错误被捕获时,我的 JOptionPane 是可见的但没有任何消息!有人知道为什么以及如何解决这个问题吗?

问候

MyBoardJPannel.java

package experimentations.gui;

import java.awt.Graphics;
import java.awt.Image;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MyBoardPannel extends JPanel {

@Override
public void paint(Graphics grahics) {
if (imageToShow == null)
imageToShow = loadImage("sampleImage");
}

/**
* In fact, there are not any image in project => will go to catch clause.
* @param imageName
*/
private void loadImage(String imageName) {
InputStream imageStream = getClass().getResourceAsStream("/"+imageName+".png");
try {
imageToShow = ImageIO.read(imageStream);
}
catch (Exception e) {
String errorMessage = "Failed to load image "+imageName;
System.err.println(errorMessage);
JOptionPane.showMessageDialog(this, errorMessage,
"Image loading error", JOptionPane.ERROR_MESSAGE);
imageToShow = null;
System.exit(1);
}
}

private Image imageToShow;



}

JOptionPaneErrorShowing.java

package experimentations.gui;

import javax.swing.JFrame;

public class JOptionPaneErrorShowing extends JFrame {

public JOptionPaneErrorShowing(){
setTitle("JOptionPane experimentation");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

add(new MyBoardPannel());
}

/**
* @param args
*/
public static void main(String[] args) {
new JOptionPaneErrorShowing().setVisible(true);
}

}

最佳答案

这可能是 Swing 并发问题。但更重要的是,永远不要从 paint 或 paintComponent 方法中加载图像。在构造函数或其他地方阅读它,但 paint/paintComponent 需要精简且速度极快。

要解决您的问题,请考虑读取 SwingWorker 对象中的图像。但是,如果您从 SwingWorker 的 doInBackground 方法中调用 JOptionPane,请务必使用 SwingUtilities.invokeLater(Runnable) 在 Swing 事件线程 EDT 上调用它。

此外,除非您正在处理绘制边框和子项,否则您几乎不会想在 JPanel 的绘制方法中进行绘制。而是在 paintComponent 方法中进行绘制,并且不要忘记在该 paintComponent 覆盖中调用 super.paintComponent(g) 方法。您需要阅读 Swing 图形教程,因为所有内容都在此处拼写。

例如:

import java.awt.Graphics;
import java.awt.Image;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class MyBoardPannel extends JPanel {
protected static final String SAMPLE_IMAGE = "sampleImage";
Image imageToShow = null;

public MyBoardPannel() {
SwingWorker<Image, Void> mySW = new SwingWorker<Image, Void>() {

@Override
protected Image doInBackground() throws Exception {
return loadImage(SAMPLE_IMAGE);
}

@Override
protected void done() {
try {
imageToShow = get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
};

mySW.execute();
}

@Override
public void paintComponent(Graphics grahics) {
super.paintComponent(grahics);
if (imageToShow != null) {
grahics.drawImage(imageToShow, 0, 0, null);
}
}

private Image loadImage(String imageName) {
InputStream imageStream = getClass().getResourceAsStream(
"/" + imageName + ".png");
try {
return ImageIO.read(imageStream);
} catch (Exception e) {
final String errorMessage = "Failed to load image " + imageName;
System.err.println(errorMessage);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(MyBoardPannel.this, errorMessage,
"Image loading error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
});
}

return null;
}

}

关于java - JOptionPane.showMessageDialog() 显示但没有任何消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7312102/

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