简体   繁体   中英

Java Swing application Message dialog help

I am working on a Java Swing application. I need to create a dialog like that shown in the figure. I do not know the name for this; I can not explain, so I am attaching a picture. Please tell what this is called and how I can create it in my GUI application.

图片是这样的

There's more than one way to skin a cat .


public final class JDialogDemo {
    private static BufferedImage bi;

    public static void main(String[] args){
        try {
            loadImage();

            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void loadImage() throws IOException{
        bi = ImageIO.read(JDialogDemo.class.getResource("../resource/close-icon.png"));
    }

    private static void createAndShowGUI(){
        final JDialog dialog = new JDialog();
        dialog.setUndecorated(true);

        final JPanel panel = new JPanel(){
            @Override
            public Dimension getPreferredSize(){
                return new Dimension(400, 40);
            }
        };
        panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        panel.setBackground(new Color(238, 221, 130));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

        final JLabel closeLabel = new JLabel();
        closeLabel.setIcon(new ImageIcon(bi));
        closeLabel.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                dialog.dispose();
            }
        });

        panel.add(new JLabel("There are deleted items that used to be in this folder."));
        panel.add(Box.createHorizontalGlue());
        panel.add(closeLabel);
        dialog.add(panel);
        dialog.pack();
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
    }
}

在此处输入图像描述


This is simply a demonstration. Feel free to tailor this however you like.

Another handy source for icons is the UIManager . In this case, the JInternalFrame Look & Feel closeIcon has some appeal, but others are also available.

Modifying @Moonbeam's answer produces the result below.

private static final Icon icon = UIManager.getIcon("InternalFrame.closeIcon");
...
private static void createAndShowGUI() {
    ...
    closeLabel.setIcon(icon);
    ...
}

JDialogDemo

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