我已经构建了一个类,它为自行车创建了一个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结束
发布于 2013-12-07 04:25:10
您将所有的JButtons都定义为local变量。它们是在您的类的构造函数中定义的,因此它们不能被您的ButtonListener内部类引用。
您需要将按钮定义为instance变量(就像您对BicyclePanel所做的那样)。
发布于 2013-12-07 04:31:26
这是关于变量的作用域。您可以将组件定义为构造对象的方法的本地变量,在本例中是ControlPanel的构造函数。要解决此问题,您可以使变量对外部对象可见。您可以使它们成为类的字段,并使用对ControlPanel的引用来初始化侦听器类,这样您就可以访问它们。但这不是一个非常好的主意,因为您让每个外部对象访问那些不需要的组件。
在Swing组件的上下文中,您只需使用setActionCommand设置按钮的actionCommand,并使用getActionCommand在侦听器中检索它。这样,您就解耦了类;它们不需要知道彼此的内部结构。缺点是,他们必须就一组通用的字符串作为操作命令达成一致。
https://stackoverflow.com/questions/20432850
复制相似问题