Below is the code which creates 9 buttons in gridlayout form on a specific pannel3. What i want is to make the background of each button black with grey text over it. Can anyone help please?
for(int i=1;i<=9;i++)
{
p3.add(new JButton(""+i));
}
JButton red = new JButton("RED"); JFrame class is used to create a new JFrame container with the given text as the frame's title. JButton class is used to create button with the given text as content of the button. Color c = new Color(255, 255, 255);
Use the setBackground method to set the background and setForeground to change the colour of your text.
By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.
Check out JButton documentation. Take special attention to setBackground
and setForeground
methods inherited from JComponent
.
Something like:
for(int i=1;i<=9;i++)
{
JButton btn = new JButton(String.valueOf(i));
btn.setBackground(Color.BLACK);
btn.setForeground(Color.GRAY);
p3.add(btn);
}
Simple:
btn.setBackground(Color.red);
To use RGB values:
btn[i].setBackground(Color.RGBtoHSB(int, int, int, float[]));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With