Can anyone help with this Java program

Discussion in 'Mac Programming' started by maxtoncollins1, Mar 3, 2012.

  1. maxtoncollins1, Mar 3, 2012
    Last edited by a moderator: Mar 3, 2012

    macrumors newbie

    Joined:
    Feb 16, 2012
    #1
    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?
     
  2. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    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
     
  3. thread starter macrumors newbie

    Joined:
    Feb 16, 2012
    #3
    Oops. I'll be sure to do that if i ever post code again. I'm new to forums in general.
    The video i was watching was this:http://www.youtube.com/watch?v=xpHs_Cm965I
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    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.
     

Share This Page