java swing显示字体的问题

xxslljrj  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(397)

你好,

所以我的问题是:我不明白为什么这个小程序:

package tests;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

final class FontTest implements Runnable {
    public static void main(String[]arguments_) {
        SwingUtilities.invokeLater(new FontTest());
    }
    private String newLine = System.getProperty("line.separator");
    @Override
    public void run() {
        JFrame frame_ = new JFrame("FontTest");
        JPanel panel_ = new JPanel(new GridLayout(1, 2));
        JTextArea area_1 = new JTextArea(), area_2 = new JTextArea();
        Font[]all_fonts = GraphicsEnvironment
                       .getLocalGraphicsEnvironment().getAllFonts();
        // picking 2 fonts with different predefined specificities
        // Arial Bold Italic and Bahnschrift Bold Condensed
        Font font_1 = all_fonts[6], font_2 = all_fonts[13];
        Map<? extends Attribute, ?> map_1 = font_1.getAttributes()
                                  , map_2 = font_2.getAttributes();
        setTextArea(area_1, new Font(map_1));
        setTextArea(area_2, new Font(map_2));
        panel_.setPreferredSize(new Dimension(1080, 240));
        panel_.add(area_1);
        panel_.add(area_2);
        frame_.add(panel_);
        frame_.pack();
        frame_.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame_.setLocationRelativeTo(null);
        frame_.setVisible(true);
    }
    private void setTextArea(JTextArea area_, Font font_) {
        area_.setFont(font_.deriveFont(14.f));
        area_.setBorder(BorderFactory.createTitledBorder(area_.getFont().getName()));
        area_.append(newLine);
        for(Object o : area_.getFont().getAttributes().entrySet()) {
            area_.append(o.toString());
            area_.append(newLine);
        }
        area_.append(newLine);
        area_.append(area_.getFont().toString());
        for(Attribute a : font_.getAvailableAttributes()) {
            System.out.println(a);
        }
    }
}

在执行时显示:jframe通过名称、属性和构造函数显示两种字体,两者都不相关
为什么? TextAttributenull 对于宽度、姿势和重量(以及除大小和族以外的所有其他项),因为字体的名称表示一些值?
为什么是建设者( font_.toString() )描述一个简单的样式,因为它们应该是粗体+斜体还是粗体?
为什么第一个字体(arial)实际上是粗体和斜体,因为它的属性和构造器说它不是?
为什么第二种字体(bahnschrift)既不粗体也不精简,因为它的属性和构造函数与第一种字体相同?
拜托,谢谢你帮我理解。。。
编辑:

for(Attribute a : font_.getAvailableAttributes()) {
    System.out.println(a);
}

对于第一种字体(arial粗体-斜体):

java.awt.font.TextAttribute(family)
java.awt.font.TextAttribute(weight)
java.awt.font.TextAttribute(width)
java.awt.font.TextAttribute(posture)
java.awt.font.TextAttribute(size)
java.awt.font.TextAttribute(transform)
java.awt.font.TextAttribute(superscript)
java.awt.font.TextAttribute(char_replacement)
java.awt.font.TextAttribute(foreground)
java.awt.font.TextAttribute(background)
java.awt.font.TextAttribute(underline)
java.awt.font.TextAttribute(strikethrough)
java.awt.font.TextAttribute(run_direction)
java.awt.font.TextAttribute(bidi_embedding)
java.awt.font.TextAttribute(justification)
java.awt.font.TextAttribute(input method highlight)
java.awt.font.TextAttribute(input method underline)
java.awt.font.TextAttribute(swap_colors)
java.awt.font.TextAttribute(numeric_shaping)
java.awt.font.TextAttribute(kerning)
java.awt.font.TextAttribute(ligatures)
java.awt.font.TextAttribute(tracking)

对于第二个(bahnschrift粗体压缩):

java.awt.font.TextAttribute(family)
java.awt.font.TextAttribute(weight)
java.awt.font.TextAttribute(width)
java.awt.font.TextAttribute(posture)
java.awt.font.TextAttribute(size)
java.awt.font.TextAttribute(transform)
java.awt.font.TextAttribute(superscript)
java.awt.font.TextAttribute(char_replacement)
java.awt.font.TextAttribute(foreground)
java.awt.font.TextAttribute(background)
java.awt.font.TextAttribute(underline)
java.awt.font.TextAttribute(strikethrough)
java.awt.font.TextAttribute(run_direction)
java.awt.font.TextAttribute(bidi_embedding)
java.awt.font.TextAttribute(justification)
java.awt.font.TextAttribute(input method highlight)
java.awt.font.TextAttribute(input method underline)
java.awt.font.TextAttribute(swap_colors)
java.awt.font.TextAttribute(numeric_shaping)
java.awt.font.TextAttribute(kerning)
java.awt.font.TextAttribute(ligatures)
java.awt.font.TextAttribute(tracking)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题