PDA

View Full Version : Can anyone help with this Java program




maxtoncollins1
Mar 3, 2012, 05:27 PM
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:


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?



balamw
Mar 3, 2012, 05:29 PM
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

maxtoncollins1
Mar 3, 2012, 05:32 PM
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

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

chown33
Mar 3, 2012, 06:45 PM
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);
}


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.