2013-03-26 201 views
0

我做A類項目,並清理我的壓痕等,並做了一些我的代碼。我看不到樹林,可以使用一些幫助。我編譯時遇到無法找到符號錯誤。這是代碼。編譯時找不到符號錯誤。

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

什麼符號是不是發現了什麼? – Floris 2013-03-26 18:57:55

+0

爲什麼'TrafficLights.Light'?只需使用'Light'。 – ecbrodie 2013-03-26 18:58:58

+0

你的光芒是不是一個靜態class..just利用光 – Pragnani 2013-03-26 18:59:06

回答

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

您目前正在申報中的itemStateChanged方法Light類。我不認爲你打算這麼做。

此外,我會質疑你是否真的需要Light本身是內部或嵌套類。我建議讓它成爲頂級課程 - 至少要開始。除此之外,以這種方式導航會更容易。

我也強烈建議在任何時候都試圖保持壓痕的頂部。如果代碼的其餘部分已經很好地縮進,則更容易看出什麼時候出錯。 IDE等IDE可以通過按鈕縮進所有代碼 - 我建議您大量使用該功能。

+0

我不確定我關注?我是否需要通過在主體之後移動燈光來讓燈光變成外部燈光? – 2013-03-26 23:28:03

+0

@KevinSchultz:你可能把它變成一個外部類,把它全部移到不同的文件中。但至少你需要將它移出你的'itemStateChanged'方法。 – 2013-03-27 06:43:25

相關問題