0

我正在为班级做一个项目,正在清理我的缩进等,并对我的代码做了一些事情。我只见树木不见森林,需要一些帮助。编译时出现找不到符号错误。这是代码。

public class TrafficLights extends JFrame implements ItemListener
{
    private JRadioButton jrbRed;
    private JRadioButton jrbYellow;
    private JRadioButton jrbGreen;
    private ButtonGroup btg = new ButtonGroup();

    private TrafficLights.Light light = new TrafficLights.Light();
                  //     ^ error 1 is here               ^ error 2 is here       

public static void main(String[] args)
    {
    TrafficLights frame = new TrafficLights();
    frame.setDefaultCloseOperation(3);
    frame.setSize(200, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }

public TrafficLights()
    {
    setTitle("Traffic Light Signal");

    JPanel p1 = new JPanel();
    p1.setLayout(new FlowLayout(1));
    p1.add(this.light);

    JPanel p2 = new JPanel();
    p2.setLayout(new FlowLayout());
    p2.add(this.jrbRed = new JRadioButton("Red"));
    p2.add(this.jrbYellow = new JRadioButton("Yellow"));
    p2.add(this.jrbGreen = new JRadioButton("Green"));

    this.jrbRed.setMnemonic('R');
    this.jrbYellow.setMnemonic('Y');
    this.jrbGreen.setMnemonic('G');

    this.btg.add(this.jrbRed);
    this.btg.add(this.jrbYellow);
    this.btg.add(this.jrbGreen);

    setLayout(new BorderLayout());
    add(p1, "Center");
    add(p2, "South");

    this.jrbRed.addItemListener(this);
    this.jrbYellow.addItemListener(this);
    this.jrbGreen.addItemListener(this);

    this.jrbGreen.setSelected(true);
    this.light.turnOnGreen();
    }

public void itemStateChanged(ItemEvent e)
    {
        if (this.jrbRed.isSelected())
            {
            this.light.turnOnRed();
            }

        if (this.jrbYellow.isSelected())
            {
            this.light.turnOnYellow();
            }

        if (this.jrbGreen.isSelected())
            {
            this.light.turnOnGreen();
            }

class Light extends JPanel
    {
    private boolean red;
    private boolean yellow;
    private boolean green;

public Light()
{ }

public void turnOnRed()
{
    this.red = true;
    this.yellow = false;
    this.green = false;
    repaint();
}

public void turnOnYellow()
{
    this.red = false;
    this.yellow = true;
    this.green = false;
    repaint();
}

public void turnOnGreen()
{
    this.red = false;
    this.yellow = false;
    this.green = true;
    repaint();
}

protected void paintComponent(Graphics g)
{
    super.paintComponent(g);

    if (this.red)
        {
            g.setColor(Color.red);
            g.fillOval(10, 10, 20, 20);
            g.setColor(Color.black);
            g.drawOval(10, 35, 20, 20);
            g.drawOval(10, 60, 20, 20);
            g.drawRect(5, 5, 30, 80);
        }

    else if (this.yellow)
        {
            g.setColor(Color.yellow);
            g.fillOval(10, 35, 20, 20);
            g.setColor(Color.black);
            g.drawRect(5, 5, 30, 80);
            g.drawOval(10, 10, 20, 20);
            g.drawOval(10, 60, 20, 20);
        }

    else if (this.green)
        {
            g.setColor(Color.green);
            g.fillOval(10, 60, 20, 20);
            g.setColor(Color.black);
            g.drawRect(5, 5, 30, 80);
            g.drawOval(10, 10, 20, 20);
            g.drawOval(10, 35, 20, 20);
        }

else
    {
        g.setColor(Color.black);
        g.drawRect(5, 5, 30, 80);
        g.drawOval(10, 10, 20, 20);
        g.drawOval(10, 35, 20, 20);
        g.drawOval(10, 60, 20, 20);
    }
}

public Dimension getPreferredSize()
    {
        return new Dimension(100, 100);
    }
}
}
}
4

1 回答 1

2

这就是问题所在 - 我缩进了代码以使其更清晰......

public void itemStateChanged(ItemEvent e)
{
    if (this.jrbRed.isSelected())
    {
        this.light.turnOnRed();
    }

    if (this.jrbYellow.isSelected())
    {
        this.light.turnOnYellow();
    }

    if (this.jrbGreen.isSelected())
    {
        this.light.turnOnGreen();
    }

    class Light extends JPanel
    {
        private boolean red;
        private boolean yellow;
        private boolean green;
       ...

您当前正在方法Light声明该类。我不认为你打算这样做。itemStateChanged

此外,我会质疑您是否真的需要Light成为内部类或嵌套类本身。我建议将其作为顶级课程——至少从一开始就是这样。除了其他任何事情之外,以这种方式导航会更容易。

我还强烈建议您始终保持在您的缩进之上。如果您的代码的其余部分已经很好地缩进,那么当出现问题时更容易看到。诸如 Eclipse 之类的 IDE 只需按一下按钮即可为您缩进所有代码 - 我建议您大量使用该功能。

于 2013-03-26T18:59:17.550 回答