如何使用Swing JAVA显示带表情符号的标签

3

我尝试使用Java显示表情符号。我发现了https://github.com/vdurmont/emoji-java这个库,它非常有用,但只能在终端中工作。

我尝试使用swing显示标签。

我该如何在Java的swing中显示带有表情符号的标签?

package Graphic;

import com.vdurmont.emoji.EmojiParser;
import javax.swing.*;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

public class Graphic{

    public static void Graphic() {
        JFrame frame = new JFrame();
        frame.setSize(400, 400); // Tamaño de la ventana principal
        frame.setTitle("Orders work");
        String withlove  = "With :heart: Nicoll";
        String result = EmojiParser.parseToUnicode(withlove);
        System.out.println(result);

        JLabel conamor    = new JLabel(result);

        conamor.setText(result);
        frame.add(conamor);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

enter image description here


https://github.com/vdurmont/emoji-java/issues/131 - undefined
2
不知道EmojiParser是做什么的,但如果我将代码改为String result = "With \u2764 Nicoll";,在运行Windows 10和Java 15时会得到一个心形符号。请参见图片。--- 可能是使用了不支持表情符号的字体。 - undefined
我使用的是Ubuntu,不工作。 - undefined
我还可以使用哪种字体?谢谢! - undefined
任何支持Unicode字符的字体都应该可以使用。我不知道Ubuntu有哪些可用的字体,但你可以尝试使用JVM支持的通用无衬线字体,例如yourLabel.setFont(new Font("SansSerif", Font.PLAIN, fontSize)); - undefined
1个回答

2
“我可以使用哪些其他字体?”请参见this answer以获取已安装字体的列表,这将显示String中所有字符。这应该在运行时完成,除非您已经在应用程序中提供了合适的Font

注意:
  1. 代码将需要使用与表情符号相对应的Unicode字符。
  2. 它将是单色的,与文本颜色相同。就像在此处看到的那样。

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.*;

public class IHeartNickel {

    private JComponent ui = null;
    Vector<String> fonts = new Vector<>();
    // heavy black heart in Unicode
    String heart = new String(Character.toChars(10084));
    String msg = "I " + heart + " Nickel (%1s)";

    IHeartNickel() {
        initUI();
    }

    public final void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));
        String[] allFonts = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for (String f : allFonts) {
            Font font = new Font(f, Font.PLAIN, 1);
            if (font.canDisplayUpTo(msg) < 0) {
                fonts.add(f);
            }
        }
        JList list = new JList(fonts);
        list.setVisibleRowCount(10);
        list.setCellRenderer(new HeartListCellRenderer());
        ui.add(new JScrollPane(list));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            IHeartNickel o = new IHeartNickel();
            
            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);
            
            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());
            
            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }

    class HeartListCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(
                JList<? extends Object> list, 
                Object value, 
                int index, 
                boolean isSelected, 
                boolean cellHasFocus) {
            Component c = super.getListCellRendererComponent(
                    list, value, index, isSelected, cellHasFocus);
            JLabel l = (JLabel)c;
            Font font = new Font(value.toString(), Font.PLAIN, 20);
            l.setText(String.format(msg, font.getFontName()));
            l.setFont(font);
            
            return l;
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接