如何使用 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/