import java.awt.*; import javax.swing.*; public class MyButton extends JFrame { public MyButton() { JButton button = new JButton("Click Me"); button.setBackground(Color.RED); add(button); setSize(200, 100); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new MyButton(); } }
import java.awt.*; import javax.swing.*; public class MyButton extends JFrame { public MyButton() { JButton button = new JButton("Click Me"); button.setBackground(new Color(255, 0, 0)); add(button); setSize(200, 100); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new MyButton(); } }In this example, the setBackground method is used to set the background color of the button to red by specifying the RGB values (255,0,0). The package library for this example is `java.awt`.