2017-03-22 51 views
1

我试图将交通灯由红色变为黄色并在绿色中重复,并通过按下一个按钮来启动此过程。这是我的代码:更改交通灯状态java切换按钮

public class TrafficLight extends JFrame implements ActionListener { 
    JButton b1, b2, b3; 

    Signal green = new Signal(Color.green); 
    Signal yellow = new Signal(Color.yellow); 
    Signal red = new Signal(Color.red); 

public TrafficLight(){ 
    super("Traffic Light"); 
    getContentPane().setLayout(new GridLayout(2, 1)); 
    b1 = new JButton("Change State"); 
    b1.addActionListener(this); 


    green.turnOn(false); 
    yellow.turnOn(false); 
    red.turnOn(true); 

    JPanel p1 = new JPanel(new GridLayout(3,1)); 
    p1.add(red); 
    p1.add(yellow); 
    p1.add(green); 
    JPanel p2 = new JPanel(new FlowLayout()); 
    p2.add(b1); 


    getContentPane().add(p1); 
    getContentPane().add(p2); 
    pack(); 
    } 

我知道必须有其他的if/else语句,但我不知道这是我应该去最好的方向

public void actionPerformed(ActionEvent e){   
    if (e.getSource() == b1){ 
     green.turnOn(false);    
     yellow.turnOn(false); 
     red.turnOn(true); 

    } 
} 

回答

1

如果你有Java8,可以循环任何集合是这样的:

List<Color> colors = Arrays.asList(GREEN, YELLOW, RED); 
Iterator<Color> loop = Stream.generate(() -> colors).flatMap(List::stream).iterator(); 

你完成后,它(和地方保存这个迭代器,因为这是你的应用程序状态):

actionPerformed(ActionEvent e) { 
    Color clr = loop.next(); 
    if (clr == GREEN) { 
    green.turnOn(true); 
    yellow.turnOn(false); 
    red.turnOn(false); 
    } 
    //<continue there for other colors> 
} 

我个人也建议进一步抽象,以便你的颜色代表一个应用程序状态,所以你的颜色是一个类与

interface Color { 
    void activate(); 
} 
然后至210

事件侦听器将只调用

loop.next().activate(); 

和所有必要的工作将是内部的方法来完成,而不是在监听器里。

继续使用绝对路径还可以让您添加,例如,在前一个灯熄灭之前闪烁。

+0

完美我不知道你能够循环颜色,我应该能够使它现在工作。谢谢! – Liam

0

如果我是你,我会做这样的事情:

public void actionPerformed(ActionEvernt e) { 
    // Turn off everything so you don't repeat yourself 
    green.turnOn(false); 
    yellow.turnOn(false); 
    red.turnOn(false); 

    // This is the work around with if/else statements. 
    // Can be done with a switch statement too. 
    // No sure what you mean by 'b1', I will assume b1 is red, b2 is green, and b3 is yellow. 
    if(e.getSource() == b1) { 
     red.turnOn(true); 
    } else if (e.getSource() == b2) { 
     green.turnOn(true); 
    } else if (e.getSource() == b3) { 
     yellow.turnOn(true); 
    } 
} 

如果您有任何问题随时问。

+0

这是我最初的,但我想要做的只是按b1切换光的颜色。 b1代表按钮。 – Liam

0

这里是另一种方式来做到这一点,创建一个Signals类将管理未来光开启(并且翻到最后点燃一关):

import java.awt.Color; 
import java.util.ArrayList; 
import java.util.List; 

public class Signals { 

    private List<Signal> signalsList = new ArrayList<>(); 
    private int selectedIndex = 0; 

    public Signals(final int selectedIndex, final Signal... signals) { 

     for (Signal signal : signals) { 

      signalsList.add(signal); 

     } 

     this.selectedIndex = selectedIndex; 

     signalsList.get(selectedIndex).turnOn(true); 
    } 

    public void switchLights() { 

     signalsList.get(selectedIndex).turnOn(false); 

     selectedIndex = (selectedIndex + 1) > (signalsList.size() - 1) ? 0 : selectedIndex + 1; 

     signalsList.get(selectedIndex).turnOn(true); 

    } 

} 

在代码中,创建一个实例该类

Signals signals = new Signals(2,green,yellow,red); 

而使用它的方式:

public void actionPerformed(ActionEvent e){   
    if (e.getSource() == b1){ 

     signals.switchLights(); 

    } 
} 
+0

当我尝试执行此代码时,指示灯从红色变为绿色,但在按住切换按钮时停在那里 – Liam

0

尝试的AWT定时器。这将允许您在每个之间设置恒定的变化率。只需在交通信号灯的每个状态下执行一次if(或者如果您使用enum,则为开关)。

LightTimer = new Timer(1000, null); 
LightTimer.addActionListener(new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent event) { 
    if(b1.getState() == true) { 
     if (light.getState() == red) 
     light.setState(yellow); 
     else if(light.getState() == yellow) 
     light.setState(green); 
     else light.setState(red); 
    } 
    } 
}); 
+0

如果有人可以请格式化代码,我无法在手机上进行格式化 – Rocket6488