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

How display Label with emojis using Swing JAVA

我尝试使用 Java 显示表情符号。我找到了 https://github.com/vdurmont/emoji-java 这个图书馆,它非常有用,但只能在终端中使用。

我尝试使用 swing 显示 Label

如何用摇摆显示带表情符号的标签JAVA?

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

What other font can I use?

请参阅 获取已安装字体的列表,这些字体将显示 String 的所有字符。这应该在 运行 时间完成,除非您为应用程序提供合适的 Font

备注:

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

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