Okay so far this is what i have, i havent really gotten very far but id like to get the layout all set before i try to tie things together. The problem im having is getting the JButtons colored. In order for my game to be like the game simon with the four colors, i have the make the JButtons those colors. I have tried to use setBackRound but it didnt work. Any help will do.

public class Project extends JFrame {
   public Project() {
        super();
	JPanel works = new JPanel();
	JButton red = new JButton("RED");
	JButton green = new JButton("GREEN");
	JButton blue = new JButton("BLUE");
	JButton white = new JButton("WHITE");
	red.addActionListener(new ButtonHandler());
		
	add(new JLabel());
	setLayout(new GridLayout(3,2));
		
	works.setBackground(Color.yellow);
	add(red);
	add(green);
	add(blue);
	add(white);
	add(works);
     }
   private class ButtonHandler implements ActionListener {
	public void actionPerformed(ActionEvent e){
			
	
	}
    }
}

As yo can see i can add color to the panel but not the button

Recommended Answers

All 3 Replies

JButton red = new JButton("RED");
JButton green = new JButton("GREEN");
JButton blue = new JButton("BLUE");
JButton white = new JButton("WHITE");

red.setBackground(Color.red);
green.setBackground(Color.green);
blue.setBackground(Color.blue);
white.setBackground(Color.white);

ya thats what i thought too, i tried it but it didnt work. IS it possible it might have to do with my layout or anything maybe im doing something that doesnr support the color change? any help will do, thanks

It may be the "look and feel" you're using. The "Windows" L&F doesn't show the background color except for a tiny strip along the edge of the border, but "WindowsClassic" does show the background color.

try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.