Mac Can anyone help with this Java program

maxtoncollins1

macrumors newbie
Original poster
Feb 16, 2012
13
0
I am trying to make a java program like the guy in a video i watched. He made a menu with a button group to change the background color of the frame. the code i have is like this:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class color extends JFrame implements ActionListener {
	private JMenuBar bar;
	private JMenu menu;
	private JRadioButtonMenuItem red;
	private JRadioButtonMenuItem blue;
	private JRadioButtonMenuItem green;
	
	public color() {
		bar = new JMenuBar();
		menu = new JMenu("Colors");
		red = new JRadioButtonMenuItem("Red");
		blue = new JRadioButtonMenuItem("Blue");
		green = new JRadioButtonMenuItem("Green");
		setLayout(new FlowLayout());
		setSize(300, 250);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Colors");
		bar.add(menu);
		menu.add(red);
		menu.add(blue);
		menu.add(green);
		ButtonGroup group = new ButtonGroup();
		group.add(red);
		group.add(blue);
		group.add(green);
		red.addActionListener(this);
		blue.addActionListener(this);
		green.addActionListener(this);
		add(bar);
	}
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == red) {
			setBackground(Color.RED);
		}
		if (e.getSource() == blue) {
			setBackground(Color.BLUE);
		}
		if (e.getSource() == green); {
			setBackground(Color.GREEN);
}
	}
}
But whenever I run it the background is always green. Can anyone correct this to make it work right?
 
Last edited by a moderator:

balamw

Moderator
Staff member
Aug 16, 2005
19,075
963
New England
I am trying to make a java program like the guy in a video i watched. He made a menu with a button group to change the background color of the frame. the code i have is like this:
It would be helpful if you posted a link to the video in question.

Also, please use CODE tags when you post code around here.

Thanks,

B
 

chown33

Moderator
Staff member
Aug 9, 2009
8,427
4,431
Pale blue comma
Code:
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == red) {
			setBackground(Color.RED);
		}
		if (e.getSource() == blue) {
			setBackground(Color.BLUE);
		}
		if (e.getSource() == green)[B][COLOR="Red"];[/COLOR][/B] {
			setBackground(Color.GREEN);
}
Misplaced semicolon hilited in red. Work it out from there. If I told you exactly how to fix it, you won't get the debugging experience.

You should be able to walk through the code, taking the red-hilited semicolon into account, and figure out what the code is actually doing. Once you figure out what it's doing, and knowing what the visible symptom is, you should be able to fix the problem.