2017-03-22 92 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