按钮监听器和动作监听器

时间:2013-12-06 20:19:27

标签: java button user-interface listener actionlistener

我已经构建了一个类,它为自行车创建一个Java Applet,它根据用户点击的按钮移动。小程序显示正常,但我的按钮监听器出现问题。

我的代码的以下部分出现问题。

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();
    }
} 

它一直告诉我,例如blueBikeStop无法解析为变量和一系列错误。我不确定这是否是错误编写的代码。

这是完整的课程(请记住,整个课程并未完全完成。

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

} // ControlPanel的结尾

2 个答案:

答案 0 :(得分:0)

您将所有JButton定义为local个变量。它们是在类的构造函数中定义的,因此它们不能被ButtonListener内部类引用。

您需要将按钮定义为instance变量(就像您使用BicyclePanel一样)。

答案 1 :(得分:0)

这是关于变量的范围。您将组件定义为构造对象的方法的本地变量,在本例中为ControlPanel的构造函数。要解决此问题,您可以使变量对外部对象可见。您可以将它们作为类的字段并使用对ControlPanel的引用初始化您的侦听器类,以便您可以访问它们。但这不是一个好主意,因为你给每个外部对象访问那些不需要的组件。

在Swing组件的上下文中,您可以使用setActionCommand设置按钮的actionCommand,并使用getActionCommand在侦听器中检索它。这样你就可以解耦类了;他们不需要了解彼此的内部结构。在缺点方面,他们必须就一组共同的字符串达成一致作为动作命令。