简体   繁体   中英

Create jDialog Swing with a message of error with image

I'm new of Java and I want to do a jDialog that is opening when I push a button in the main JFrame and show a message of error in this way:

在此处输入图片说明

I can't put the image in another way in NetBeans? I create in the source package a directory with the image and a try much thing:

jDialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
jDialog1.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/img/error_button.png")))));
jDialog1.pack();
jDialog1.setLocationByPlatform(true);

jDialog1.setVisible(true);

Is there a simple way to do this?

We can use an option pane for this. It includes its own icons according to the type of message (and look and feel).

在此处输入图片说明

Exception e = new Exception("Error!");
JOptionPane.showMessageDialog(f, e, e.getMessage(), JOptionPane.WARNING_MESSAGE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM