我正在为课堂做一个项目,正在清理我的缩进等,并为我的代码做了一些事情。我无法看到森林的树木,可以使用一些帮助。我编译时遇到无法找到符号错误。这是代码。
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);
}
}
}
}
答案 0 :(得分: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;
...
您目前正在<{1}}方法中声明Light
类 。我认为你不打算这样做。
另外,我会怀疑你是否真的需要itemStateChanged
作为内部或嵌套类本身。我建议把它作为顶级课程 - 至少从一开始。除了其他任何东西之外,以这种方式导航会更容易。
我还强烈建议您始终保持在缩进之上。如果你的代码的其余部分已经很好地缩进,那么当出现问题时更容易看到。 Eclipse之类的IDE只需按一下按钮即可为您缩进所有代码 - 我建议您大量使用该功能。