gpt4 book ai didi

java - 按钮监听器和 Action 监听器

转载 作者:太空宇宙 更新时间:2023-11-04 15:20:37 34 4
gpt4 key购买 nike

我构建了一个类,为自行车创建一个 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 无法解析为变量和一系列错误。我不确定这是否是错误编写的代码。

这是完整的类(class)(请记住,整个类(class)并未完全完成。

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

}//控制面板结束

最佳答案

您将所有 JButton 定义为本地变量。它们是在类的构造函数中定义的,因此您的 ButtonListener 内部类无法引用它们。

您需要将按钮定义为实例变量(就像您对 BicyclePanel 所做的那样)。

关于java - 按钮监听器和 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20432850/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com