简体   繁体   English

如何更改 AWT 标签字体

[英]How to change AWT Label Font

I am trying to define the Font for an AWT Label.我正在尝试为 AWT 标签定义字体。

While I can manage to use fonts for a Java2D graphics environment, same font seems not to work with AWT Labels.虽然我可以设法将字体用于 Java2D 图形环境,但相同的字体似乎不适用于 AWT 标签。 I would like to understand if there is any limitation on font usage I am not aware about it for AWT Labels, or if I am simply not using the right syntax/procedure.我想了解是否对 AWT 标签的字体使用有任何限制我不知道它,或者我只是没有使用正确的语法/程序。

this is my code, it basically adds a Label and text to the graphics context:这是我的代码,它基本上向图形上下文添加了一个标签和文本:

package com.company.test;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;

public class TestTest extends Frame {

    Label myLabel = new Label();
    Font myFont = new Font("Roboto Condensed Light", Font.PLAIN, 12);
    Graphics2D g2d;

    public TestTest() {

        setSize(500,200);
        setLocation(10,10);
        setUndecorated(false);

        myLabel.setBackground(Color.red);
        myLabel.setFont(myFont);
        myLabel.setText("ROBOTO CONDENSED, THIS DOES NOT WORK!");
        add(myLabel, BorderLayout.SOUTH);

        setVisible(true);

    }

    public void paint(Graphics g) {

        g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(0.0f));

        g2d.setFont(myFont);
        g2d.setColor(Color.BLACK);
        g2d.drawString("ROBOTO CONDENSED THIS WORKS!",50, 50);

    }


    public static void main( String[] args ) {

        TestTest window = new TestTest();

    }

}

This is the result:这是结果:

字体使用

While font is properly defined (as it is used in the Text element in the Graphics component), it is not being applied to the AWT Label component.虽然字体已正确定义(因为它在 Graphics 组件的 Text 元素中使用),但它并未应用于 AWT Label 组件。

Any tip is welcomed.欢迎任何提示。

Note: please do not suggest to use SWING or JavaFX, I am well aware that they are the recommended way of using widgets.注意:请不要建议使用 SWING 或 JavaFX,我很清楚它们是使用小部件的推荐方式。 Question is specifically related to AWT Label widget.问题特别与 AWT 标签小部件有关。

Ok you have to create your own label;好的,您必须创建自己的标签; the idea is to get to the graphics of things.这个想法是获得事物的图形。 I have added my inline class but you can create a proper class;我已经添加了我的内联类,但您可以创建一个合适的类; then you have to pass the string or other parameters to that class:然后您必须将字符串或其他参数传递给该类:

class TestTest extends Frame {

Font myFont = new Font("Rockwell Nova", Font.PLAIN, 12);
Graphics2D g2d;

public TestTest() {

  Label myLabel = new Label() {
    public void paint(Graphics g) {
      g.setFont(myFont);
      g.drawString("ROBOTO CONDENSED, THIS DOES NOT WORK!", 0, 20);
    }
  };
    setSize(500,200);
    setLocation(10,10);
    setUndecorated(false);

    myLabel.setBackground(Color.red);

    add(myLabel, BorderLayout.SOUTH);
    setVisible(true);

}

public void paint(Graphics g) {

    g2d = (Graphics2D) g;
    g2d.setStroke(new BasicStroke(0.0f));

    g2d.setFont(myFont);
    g2d.setColor(Color.BLACK);
    g2d.drawString("ROBOTO CONDENSED THIS WORKS!",50, 50);

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM