I tried to use setFont function of an AWT object, such as Label and Button. However, it only changed the style and size. The font family was still unchanged.
There is no solution on the Internet. All of the existing contents are about Swing JButton, not AWT Button.
Is there a way to make setFont function of AWT objects work?
I'm using the newest version of Eclipse on Windows 11. JDK version is 21.
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
public class Main {
public static void main(String args[]) {
Frame frame = new Frame("ChangeButtonFont");
Font font = new Font("Comic Sans MS", Font.ITALIC, 32);
Button button1 = new Button("Comic Sans");
button1.setFont(font);
button1.setForeground(Color.blue);
Panel p = new Panel();
p.add(button1);
frame.add(button1, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
I use this demo to test, the problem still exists. If I replace "Button" with Swing object "JButton", the function will work with font family successfully changed. Button doesn't work JButton works