-1

How do I? Can I?

public class Jbutton {
    public static void main (String[] args){
    JButton button = new JButton("I'm colorful!");
    button.

What do I do here ^ to set the button color?

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Caders117
  • 319
  • 3
  • 18

3 Answers3

1

You have 2 options either use setBackground or setIcon E.g.

  1. button.setBackground(myColor);

  2. button.setIcon(new javax.swing.ImageIcon(pathToIcon));

sol4me
  • 15,233
  • 5
  • 34
  • 34
1

Using your example:

public class Jbutton {
    public static void main (String[] args){
        //You're going to need some context for this like a JFrame
        JButton button = new JButton("I'm colorful!");
        button.setBackground(Color.RED);
    }
}

Though I think for a more enlightening section of code, you might explore This Question to see how some parts of swing and AWT work.

Community
  • 1
  • 1
Matt
  • 5,404
  • 3
  • 27
  • 39
0

I believe its like so

{
JButton btn = new       
JButton(String.valueOf(i));
btn.setBackground(Color.BLACK);
btn.setForeground(Color.GRAY);
p3.add(btn);
}

Using my phone so trouble editing it to post code, that's what I used on a previous project though if that helps