如何在 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