import java.awt.*; import javax.swing.*; public class MyButton extends JFrame { public MyButton() { super("JButton Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200, 100); setLocationRelativeTo(null); JButton button = new JButton("Click me!"); button.setFont(new Font("Arial", Font.BOLD, 16)); // set the font of the text on the button add(button); setVisible(true); } public static void main(String[] args) { new MyButton(); } }
import java.awt.*; import javax.swing.*; public class MyButton extends JFrame { public MyButton() { super("JButton Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200, 100); setLocationRelativeTo(null); JButton button = new JButton("Click me!"); Font font = new Font("Courier", Font.ITALIC, 12); button.setFont(font); // set the font of the text on the button add(button); setVisible(true); } public static void main(String[] args) { new MyButton(); } }This example also creates a JFrame window with a JButton on it. In this example, the setFont method is used to set the font of the text on the button to Courier, italic, and size 12. The Font object is created first and then passed to the setFont method. Both examples use the java.awt package library.