Customize JOptionPane Dialog

前端 未结 3 1547
南笙
南笙 2020-12-03 18:57

I am learning java swing. The code below is a catch block which handles an IOException and shows a error message.

 catch(IOException e)
    {
        System         


        
相关标签:
3条回答
  • 2020-12-03 19:15

    You can simply add your components to a JPanel and then add this JPanel to your JOptionPane, as shown in this small example :

    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();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-03 19:19

    I guess that depends on what's wrong with JOptionPaneshowMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)?

    JOptionPane.showMessageDialog(null, "Java Technolgy Dive Log", "Dive", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("images/gwhite.gif"));
    

    Dialog

    0 讨论(0)
  • 2020-12-03 19:36
    JOptionPane jop = new JOptionPane();
    JDialog dialog = jop.createDialog("File not found");
    dialog.setLayout(new BorderLayout());
    JLabel im = new JLabel("Java Technology Dive Log", new ImageIcon("images/gwhite.gif"), JLabel.CENTER);
    dialog.add(im, BorderLayout.NORTH);
    dialog.setVisible(true);
    
    0 讨论(0)
提交回复
热议问题