1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* Change Button Background Color Example This java example shows how to change button background color using AWT Button class. */ import java.applet.Applet; import java.awt.Button; import java.awt.Color; /* <applet code="ChangeButtonBackgroundExample" width=200 height=200> </applet> */ public class ChangeButtonBackgroundExample extends Applet{ public void init(){ //create buttons Button button1 = new Button("Button 1"); Button button2 = new Button("Button 2"); /* * To change background color of a button use * setBackground(Color c) method. */ button1.setBackground(Color.red); button2.setBackground(Color.green); //add buttons add(button1); add(button2); } } |
Example Output
Thank you for this article
is there a way to change the background when it is clicked only ?