1

I have built a class that creates a java Applet for a bike that moves based on the button clicked by the user. The applet displays properly but I am having problems with my button listeners.

I am having problems with the following part of my code.

private class ButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        Object action = event.getSource();

        if (action == redBikeStart)

            bicycle1.resume();

        else if (event.getSource() == redBikeStop)                   
            bicycle1.suspend();                      
        else if (event.getSource() == redBikeReverse)               
            bicycle1.reverse();                       
        else if (event.getSource() == blueBikeStart)                   
            bicycle2.resume();                  
        else if (event.getSource() == blueBikeStop) 
            bicycle2.suspend();                        
        else if (event.getSource() == blueBikeReverse)                           
            bicycle2.reverse();
    }
} 

It keeps telling me that the for example the blueBikeStop cannot be resolved into a variable and a series of errors. I am not sure if this is incorrectly written code.

here is the complete class (keep in mind that the whole class is not entirely completed.

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

public class ControlPanel extends JPanel
{
//1 for the red bicycle control, 2 for the blue bicycle control
private BicyclePanel bicycle1, bicycle2;

private JPanel leftPanel, rightPanel;
private int width, height;

//The constructor creates 6 buttons, 2 sliders, and 2 bicycle panels
//and organize them using layouts.
public ControlPanel(int width, int height)
{
    this.width = width;
    this.height = height;

    //create 2 bicycle panels and arrange them using GridLayout
    bicycle1 = new BicyclePanel(Color.red, Color.cyan, width/2);
    bicycle2 = new BicyclePanel(Color.blue, Color.yellow, width/2);

    rightPanel = new JPanel();
    rightPanel.setLayout(new GridLayout(2,1));
    rightPanel.add(bicycle1);
    rightPanel.add(bicycle2);

    JPanel topLPanel = new JPanel();           
    topLPanel.setLayout(new GridLayout(3,1));           
    JPanel topRPanel = new JPanel();           
    topRPanel.setLayout(new BorderLayout());           
    JPanel bottomLPanel = new JPanel();          
    bottomLPanel.setLayout(new GridLayout(3,1));           
    JPanel bottomRPanel = new JPanel();        
    bottomRPanel.setLayout(new BorderLayout());

    JButton redBikeStart = new JButton("Start Red"); 
    redBikeStart.addActionListener(new ButtonListener()); 
    JButton redBikeStop = new JButton("Stop Red"); 
    JButton redBikeReverse = new JButton("Reverse Red"); 
    JButton blueBikeStart = new JButton("Start Blue"); 
    JButton blueBikeStop = new JButton("Stop Blue");
    JButton blueBikeReverse = new JButton("Reverse Blue"); 


    redBikeStop.addActionListener(new ButtonListener());          
    redBikeReverse.addActionListener(new ButtonListener());       
    blueBikeStart.addActionListener(new ButtonListener());          
    blueBikeStop.addActionListener(new ButtonListener());           
    blueBikeReverse.addActionListener(new ButtonListener());

    JLabel redBikeLabel = new JLabel("Red Delay");       
    JSlider redBikeDelay = new JSlider(JSlider.VERTICAL);        
    redBikeDelay.setMaximum(50);          
    redBikeDelay.setPaintLabels(true);          
    redBikeDelay.setPaintTicks(true);       
    redBikeDelay.setMajorTickSpacing(10);          
    redBikeDelay.setMinorTickSpacing(1);    
    redBikeDelay.setValue(20);     
    redBikeDelay.addChangeListener(new SliderListener());   

    JLabel blueBikeLabel = new JLabel("Blue Delay");          
    JSlider blueBikeDelay = new JSlider(JSlider.VERTICAL);         
    blueBikeDelay.setMaximum(50);         
    blueBikeDelay.setPaintLabels(true);           
    blueBikeDelay.setPaintTicks(true);         
    blueBikeDelay.setMajorTickSpacing(10);    
    blueBikeDelay.setMinorTickSpacing(1);           
    blueBikeDelay.setValue(20);           
    blueBikeDelay.addChangeListener(new SliderListener());


    leftPanel = new JPanel();
    leftPanel.setLayout(new GridLayout(2,1));          
    JPanel leftTopPanel = new JPanel();           
    leftTopPanel.setLayout(new GridLayout(1,2));           
    topLPanel.add(redBikeStart);      
    topLPanel.add(redBikeStop);           
    topLPanel.add(redBikeReverse);    
    topRPanel.add(redBikeLabel, BorderLayout.NORTH);    
    topRPanel.add(redBikeDelay, BorderLayout.WEST);     
    leftTopPanel.add(topLPanel);          
    leftTopPanel.add(topRPanel);         
    JPanel leftBottomPanel = new JPanel();         
    leftBottomPanel.setLayout(new GridLayout(1,2));         
    bottomLPanel.add(blueBikeStart);    
    bottomLPanel.add(blueBikeStop);          
    bottomLPanel.add(blueBikeReverse);         
    bottomRPanel.add(blueBikeLabel, BorderLayout.NORTH);         
    bottomRPanel.add(blueBikeDelay, BorderLayout.WEST);  
    leftBottomPanel.add(bottomLPanel);       
    leftBottomPanel.add(bottomRPanel);         
    leftPanel.add(leftTopPanel);           
    leftPanel.add(leftBottomPanel);


    //organize the left panel and right panel using SplitPane
    setLayout(new BorderLayout());
    leftPanel.setPreferredSize(new Dimension(width, 120));
    JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
    add(sp);

    setPreferredSize(new Dimension(width,height));
}


private class ButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        Object action = event.getSource();

        if (action == redBikeStart)

            bicycle1.resume();

        else if (event.getSource() == redBikeStop)                   
            bicycle1.suspend();                      
        else if (event.getSource() == redBikeReverse)               
            bicycle1.reverse();                       
        else if (event.getSource() == blueBikeStart)                   
            bicycle2.resume();                  
        else if (event.getSource() == blueBikeStop) 
            bicycle2.suspend();                        
        else if (event.getSource() == blueBikeReverse)                           
            bicycle2.reverse();
    }
} //end of ButtonListener

private class SliderListener implements ChangeListener
{
    public void stateChanged(ChangeEvent event)
    {
        /***to be completed***/
    }

} //end of SliderListener

} //end of ControlPanel

2
  • Please do not remove the question's content. The question and the answers are useless then. If you think the question needs to be deleted then you can flag it. Dec 6, 2013 at 21:11
  • Please leave the question in a state that shows the problem. Don't remove nearly all of the code. Dec 6, 2013 at 21:29

2 Answers 2

0

You defined all you JButtons as local variables. They are defined within the constructor of your class, so they can't be reference by your ButtonListener inner class.

You need to define the buttons as instance variables (like you did with your BicyclePanel).

0

This is about the scope of variables. You define your components as variables local to the method that constructs the objects, in this case the constructor of ControlPanel. To resolve this you could either make the variables visible to outside objects. You could make them fields of the class and initialize your listener classes with a reference to ControlPanel so you could access them. But this is not a very good idea as you give every outside object access to those components which is not desired.

In the context of Swing components you could just set the actionCommand of the buttons with setActionCommand and retrieve this in the listener with getActionCommand. This way you have decoupled the classes; they don't need to know about the internal structure of each other. On the downside they have to agree on a common set of strings as action commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.