我正在学习java swing。下面的代码是一个catch块,它处理一个IOException并显示一条错误消息。
catch(IOException e)
{
System.out.println("IOException");
JOptionPane.showMessageDialog(null,"File not found",null,
JOptionPane.ERROR_MESSAGE);
}我正在考虑在catch块中声明和自定义我自己的JOptionPane,如下所示:
JOptionPane jop=new JOptionPane();
jop.setLayout(new BorderLayout());
JLabel im=new JLabel("Java Technology Dive Log",
new ImageIcon("images/gwhite.gif"),JLabel.CENTER);
jop.add(im,BorderLayout.NORTH);
jop.setVisible(true);但问题是,我不知道如何让它像showMessageDialogue方法那样出现在屏幕上。请帮帮忙。提前谢谢。
发布于 2012-09-02 18:42:12
您可以简单地将组件添加到JPanel中,然后将此JPanel添加到JOptionPane中,如下面的小示例所示:
import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.imageio.ImageIO;
public class JOptionPaneExample {
private void displayGUI() {
JOptionPane.showConfirmDialog(null,
getPanel(),
"JOptionPane Example : ",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
}
private JPanel getPanel() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Java Technology Dive Log");
ImageIcon image = null;
try {
image = new ImageIcon(ImageIO.read(
new URL("http://i.imgur.com/6mbHZRU.png")));
} catch(MalformedURLException mue) {
mue.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}
label.setIcon(image);
panel.add(label);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JOptionPaneExample().displayGUI();
}
});
}
}https://stackoverflow.com/questions/12234850
复制相似问题