开发者

Java Swing application Message dialog help

开发者 https://www.devze.com 2023-03-21 12:12 出处:网络
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.

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 wh开发者_开发百科at this is called and how I can create it in my GUI application.

Java Swing application Message dialog help


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);
    }
}

Java Swing application Message dialog help


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);
    ...
}

Java Swing application Message dialog help

0

精彩评论

暂无评论...
验证码 换一张
取 消