gpt4 book ai didi

java - 编译时找不到符号错误。

转载 作者:行者123 更新时间:2023-12-02 07:10:00 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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;
...

您当前正在 itemStateChanged 方法中声明 Light 类。我不认为你有意这样做。

此外,我会质疑您是否真的需要 Light 本身成为内部类或嵌套类。我建议将其设为顶级类(class) - 至少在一开始是这样。除了其他之外,这样导航会更容易。

我还强烈建议您始终注意缩进。如果代码的其余部分已经很好地缩进,那么当出现问题时就更容易发现。诸如 Eclipse 之类的 IDE 可以通过按一下按钮来缩进所有代码 - 我建议您大量使用该功能。

关于java - 编译时找不到符号错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15645198/

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