You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java AWT程序中Label字体家族无法变更的问题求助

问题:Java AWT Label字体家族无法生效

我在大学实验中使用java.awt实现图形界面,尝试通过界面修改Label的Font时,发现字体大小和字体风格可正常变更,但字体家族无法在界面上生效。虽然Label的Font对象已被赋值,但界面显示无变化。也许是低级错误,但我实在找不到原因。

代码示例

Main.java

public class Main {

    public static void main(String[] args) {

        MainWindow mainWindow = new MainWindow();
        MainWindow.set_main_window();

    }
}

MainWindow.java

import java.awt.*;
import java.util.Scanner;

public class MainWindow {

    private static Frame main_window;
    private static int height;
    private static int width;
    private static Label label;

    MainWindow(){
        main_window = new Frame();
        Scanner sc = new Scanner(System.in);
        System.out.print("Width: ");
        width = sc.nextInt();
        System.out.print("Height: ");
        height = sc.nextInt();
        main_window.setSize(width, height);
    }

    public static int getHeight(){
        return height;
    }

    public static int getWidth(){
        return width;
    }

    public static void setFont(Font font){
        label.setFont(font);
        main_window.repaint();
        //System.out.println("!!! "+font.getFontName());
        System.out.println(label.getFont().getFontName());
    }

    public static void set_main_window(){
        Scanner sc = new Scanner(System.in);
        ChooseFontPanel Panel2 = new ChooseFontPanel();
        TextPanel Panel = new TextPanel();
        //System.out.print("Type text: ");
        //String labelText = sc.next();
        label = new Label("123");
        label.setBounds(main_window.getWidth()/2,
                main_window.getHeight()/6*5,
                50,
                25);
        label.setFont(ChooseFontPanel.getFont());

        List[] ListArray = ChooseFontPanel.getFontPanel();
        for (List item: ListArray)
            main_window.add(item);

        TextField t = TextPanel.getTextInputField();

        main_window.add(t);
        main_window.add(label);
        main_window.setLayout(null);
        main_window.setVisible(true);
    }
}

ChooseFontPanel.java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ChooseFontPanel{
    private static String fontName = "Agency FB";
    private static int fontSize = 12;
    private static int fontAngle = 0;
    private static Font chosenFont = new Font(fontName,fontAngle,fontSize);
    private static List fontList;
    private static List fontTypeList;
    private static List fontSizeList;
    public static List[] getFontPanel(){
        return new List[] {fontList,fontTypeList,fontSizeList};
    }

    public static List getFontList(){
        return fontList;
    }
    public static List getFontTypeList(){
        return fontTypeList;
    }
    public static List getFontSizeList(){
        return fontSizeList;
    }
    public static Font getFont(){
        return chosenFont;
    }
    ChooseFontPanel() {

        fontList = new List(267,false);
        String fontArray[] =
                GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for(int i=0;i<fontArray.length;i++) fontList.add(fontArray[i]);


        fontList.setBounds(10,
                MainWindow.getHeight()/3,
                MainWindow.getWidth()/3,
                MainWindow.getHeight()/3
        );
        fontList.select(0);
        fontList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.print(fontName+"->");
                fontName = fontList.getSelectedItem();
                chosenFont = new Font(fontName,fontAngle,fontSize);
                MainWindow.setFont(chosenFont);

                //System.out.println(fontName);
                //System.out.println(chosenfont.getFontName());
            }
        });

        fontTypeList = new List(3,false);
        fontTypeList.add("Plain");
        fontTypeList.add("Bold");
        fontTypeList.add("Italic");
        fontTypeList.setBounds(MainWindow.getWidth()/3+10,
                MainWindow.getHeight()/3,
                MainWindow.getWidth()/3,
                MainWindow.getHeight()/3
        );
        fontTypeList.select(0);
        fontTypeList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.print(fontAngle+"->");
                fontAngle = fontTypeList.getSelectedIndex();
                chosenFont = new Font(fontName,fontAngle,fontSize);
                MainWindow.setFont(chosenFont);
                //System.out.println(fontAngle);

            }
        });

        fontSizeList = new List(40,false);
        for(int i=2;i<=80;i+=2){
            fontSizeList.add(String.valueOf(i));
        }
        fontSizeList.setBounds(MainWindow.getWidth()/3*2+10,
                MainWindow.getHeight()/3,
                MainWindow.getWidth()/3-15,
                MainWindow.getHeight()/3
        );
        fontSizeList.select(5);

        fontSizeList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println(fontSizeList.getSelectedItem());
                //System.out.print(fontSize+"->");
                fontSize = Integer.parseInt(fontSizeList.getSelectedItem());
                chosenFont = new Font(fontName,fontAngle,fontSize);
                MainWindow.setFont(chosenFont);
                //System.out.println(fontSize);
            }
        });
    }

}

P.S. 我知道这段代码写得不好,抱歉哈哈


内容的提问来源于stack exchange,提问作者Егор

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:39:55