java - 用可用字体列表填充组合框,Java

标签 java swing list fonts jcombobox

如何使用 GraphicsEnvironment.getAllFonts() 方法用所有可用字体的列表填充组合框?

<小时/>

我用过

JComboBox font = new 
    JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts());

但这没有用。

最佳答案

关于,

I used JComboBox font = new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts());
That hasn't worked.

它确实有效。但是您必须设置列表单元格渲染器才能显示字体名称。例如,

GraphicsEnvironment graphEnviron = 
       GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] allFonts = graphEnviron.getAllFonts();

JComboBox<Font> fontBox = new JComboBox<>(allFonts);
fontBox.setRenderer(new DefaultListCellRenderer() {
   @Override
   public Component getListCellRendererComponent(JList<?> list,
         Object value, int index, boolean isSelected, boolean cellHasFocus) {
      if (value != null) {
         Font font = (Font) value;
         value = font.getName();
      }
      return super.getListCellRendererComponent(list, value, index,
            isSelected, cellHasFocus);
   }
});
JOptionPane.showMessageDialog(null, new JScrollPane(fontBox));

这在 combo box tutorial 中有很好的描述。 .

关于java - 用可用字体列表填充组合框,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22119327/

相关文章:

java - 将 Jackson ObjectMapper 与 Jersey 一起使用

java - 有没有办法在手机上查找java内存状态?

Web 中的 Java Swing 应用程序

c# - 递归过滤不可变列表

java - 我应该使用 Iterator 还是 for 循环来迭代?

javascript - 是否可以使用代码来转换格式为 'list' : "translation"? 的列表

java - setheight() 不改变变量

java - 如何使用j2html添加标签

Java Swing : SpringLayout within BorderLayout

java - 这种 MVC 方法是编写数独游戏的好方法吗?