简体   繁体   English

如何在Java中正确向JButton添加图标?

[英]How to correctly add an icon to JButton in Java?

I am trying to add this icon to a button in my minesweeper application. 我正在尝试将此图标添加到我的minesweeper应用程序中的按钮上。 However, instead of having the icon, I get an empty square. 但是,我没有显示图标,而是得到了一个空的正方形。 Here's the code I'm using: (The code was used just to test if I can actually can add the icon to a button) 这是我正在使用的代码:(该代码仅用于测试我是否真的可以将图标添加到按钮上)

SwingUtilities.invokeLater(() -> {
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JButton button = new JButton("\uD83D\uDEA9");
                Font font = new Font("Code2000", Font.PLAIN, 12);
                button.setFont(font);
                frame.add(button);
                frame.pack();
                frame.setVisible(true);           
            });

Thanks in advance. 提前致谢。

Try adding an ImageIcon to the Button 尝试将ImageIcon添加到按钮

JButton button = new JButton();
ImageIcon image = new ImageIcon(Directory);
button.setIcon(image);

A very simple solution! 一个非常简单的解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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