user16610334
user16610334

Reputation:

How to change a colour of a JButton on Intellij idea

I wrote some code for a simple calculator. Let me explain; the calculator performs only four operations: addition, subtraction, multiplication and division. I wanted to design a program so that when I press a key on the calculator well it changes color from white to the color chosen by the programmer (i.e. me); but the code that I wrote to change key color was not accepted by the IDE and it generated heaps of errors. Here is the code I had chosen to change a color of JButton.

if ((Screen.getText(buttonTwoText)).equals(true)) {

  buttonTwo.getcolorModel("red");

  Thread.sleep(millis:2000);

  buttonTwo.getColorModel("white");
}

//Screen here is my JTextField name , ButtonTwo is a one of my JButtons name 

Upvotes: 1

Views: 548

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51533

A JComponent (JButton) has two methods for changing the background and foreground colors.

button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);

You can use any Color instances that you want.

Upvotes: 1

Related Questions