我做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);
}
}
}
}
什麼符號是不是發現了什麼? – Floris 2013-03-26 18:57:55
爲什麼'TrafficLights.Light'?只需使用'Light'。 – ecbrodie 2013-03-26 18:58:58
你的光芒是不是一個靜態class..just利用光 – Pragnani 2013-03-26 18:59:06