如何在 Java 中动态更改 JButton 字体?
以下是动态更改 JButton 字体的一个示例
实例
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class SwingDemo extends JFrame {
JButton button = new JButton("Change");
int fontSize = 10;
public SwingDemo() {
setSize(500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(button);
// changing font size dynamically on button click
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
button.setFont(new Font("Dialog", Font.PLAIN, ++fontSize));
button.revalidate();
}
});
setVisible(true);
}
public static void main(String[] args) {
new SwingDemo();
}
}输出

单击上面的“更改”按钮以更改字体

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP