2015-10-04 29 views
0

我正在嘗試編寫一個交通燈模擬程序,用於在Java課程中進行編程。當我運行小程序時,即使我還沒有點擊任何JButton,所有三個燈仍然點亮。當我這樣做時,小程序會像做某事一樣暫時變成空白,如果按GO鍵,汽車就會移動。我想知道是否需要重新設置圖形類中的顏色,或者使用switch語句(我不太確定如何操作),然後從其他示例中看到的顏色開始。我的代碼中是否有任何不正確的東西阻礙我獲得我想要的結果?任何幫助將不勝感激。謝謝。使用if或switch語句更改交通信號燈

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

/** 
* Class TrafficLights - At the click of a button, change the traffic lights 
* 
* @author Mickey Mouse 
* @version JDK 8 
* @course CSCI 1130-01 
* @date 10-2-15 
*/ 
public class TrafficLights extends JApplet implements ActionListener { 
    private JButton WAIT, STOP, GO; 

    private boolean clickWAIT = false; 
    private boolean clickSTOP = false; 
    private boolean clickGO = false; 

    private int carX = 200; 

    /** 
    * Called by the browser or applet viewer to inform this JApplet that it 
    * has been loaded into the system. It is always called before the first 
    * time that the start method is called. 
    */ 
    public void init() 
    { 
     setLayout(new FlowLayout()); // changes the layout from BorderLayout to FlowLayout 

     WAIT = new JButton ("WAIT"); //adds WAIT label to button 
     WAIT.setForeground(Color.yellow); //changes the label to yellow 

     //adds the WAIT JButton to the screen 
     add (WAIT); 
     WAIT.addActionListener(this); 


     GO = new JButton ("GO"); //adds GO label to button 
     GO.setForeground(Color.green); //changes the label to green 

     //adds the button to the screen 
     add (GO); 
     GO.addActionListener(this); 


     STOP = new JButton ("STOP"); //adds STOP label to button 
     STOP.setForeground(Color.red); //changes the label to red 

     //adds STOP JButton to screen 
     add (STOP); 
     STOP.addActionListener(this); 


    } 



    /** 
    * Called by the browser or applet viewer to inform this JApplet that it 
    * should start its execution. It is called after the init method and 
    * each time the JApplet is revisited in a Web page. 
    */ 
    public void start() 
    { 
     // provide any code requred to run each time 
     // web page is visited 
    } 

    /** 
    * Called by the browser or applet viewer to inform this JApplet that 
    * it should stop its execution. It is called when the Web page that 
    * contains this JApplet has been replaced by another page, and also 
    * just before the JApplet is to be destroyed. 
    */ 
    public void stop() 
    { 
     // provide any code that needs to be run when page 
     // is replaced by another page or before JApplet is destroyed 
    } 

    /** 
    * Paint method for applet. 
    * 
    * @param g the Graphics object for this applet 
    */ 
    public void paint(Graphics g) 
    { 
     super.paint(g); 

     //declares and retrieves the images from their file locations 
     Image img = getImage(getDocumentBase(), "stoplights.png"); 
     Image img2 = getImage(getDocumentBase(), "car.jpg"); 

     g.drawImage(img, 50, 100, 300, 350, 0, 0, 5000, 5000, this); //draws and resizes the stoplights image 
     g.drawImage(img2, carX, 400, 1000, 1000, 0, 0, 5000, 5000, this); //draws and resizes the car image 

     //draw and fill an oval red for the STOP stoplight when STOP is pressed 
     if (clickSTOP == true); 
     { 
     g.drawOval(63, 112, 30, 30); 
     g.setColor(Color.red); 
     g.fillOval(63, 112, 30, 30); 
     clickSTOP = false; 
     } 


     //draw and fill an oval yellow for the WAIT stoplight when WAIT is pressed 
     if (clickWAIT == true); 
     { 
     g.setColor(Color.black); 
     g.drawOval(63, 148, 30, 30); 
     g.setColor(Color.orange); 
     g.fillOval(63, 148, 30, 30); 
     clickWAIT = false; 
     } 

     //draw and fill an oval green for the GO stoplight when GO is pressed 
     if (clickGO == true); 
     { 
     g.setColor(Color.black); 
     g.drawOval(63, 184, 30, 30); 
     g.setColor(Color.green); 
     g.fillOval(63, 184, 30, 30); 
     clickGO = false; 
     } 



    } 


    public void actionPerformed(ActionEvent event) 
    { 
     /* 
     * Links the JButtons and the graphic sequences to display the lights 
     * 
     */ 
     if(event.getSource() == GO) //display green if GO is clicked 
     { 
      clickGO = true; 
      carX -=15; 
      repaint(); 
      } 
      if (event.getSource() == WAIT) //display yellow if WAIT is clicked 
      { 
       clickWAIT = true; 
       repaint(); 
       } 
       if (event.getSource() == STOP) //display red if STOP is clicked 
       { 
        clickSTOP = true; 
        repaint(); 
        } 

    } 


    /** 
+0

當您更改狀態(例如從等待到停止)時,您需要重置其他狀態。使用單個狀態變量會更簡單,例如可能利用'enum's例如 – MadProgrammer

回答

2

當我運行小程序,所有三個指示燈仍然點亮,即使我沒有任何點擊這些Jbutton的又

這是因爲你的每一個後有;if語句,這基本上是短路你的邏輯,所以它後面的代碼,總是執行,不管變量的狀態...

if (clickSTOP == true); 

所以,相反,你應該使用更多的東西一樣

if (clickSTOP == true) 
{ 
    g.setColor(Color.red); 
    g.fillOval(63, 112, 30, 30); 
    clickSTOP = false; 
} 

您也從內您paint方法修改程序的狀態,這是一般不提倡,因爲paint應該只是做到這一點,漆當前州。

更好的地方改變國家在ActionListener

相反則有三個狀態變量,它通常被描述同樣的事情,你應該有一個單一的狀態變量,所以國家就永遠只能是gowaitstop,從來沒有一個三者的組合。

同樣,你可以使用一個ButtonGroupJToggleButton S或JRadioButton S,這將允許按鈕攜帶一定數量的關於狀態的信息,因爲只有唯一的按鈕可以在一個時間來選擇。

看一看How to Use the ButtonGroup ComponentHow to Use Buttons, Check Boxes, and Radio Buttons更多細節

當我這樣做時,小應用程序打開空白的瞬間像它做一些

這可能是事實,你是引起從paint方法中加載圖像,但作爲一般規則,您應避免覆蓋此類頂級容器的paint

+0

感謝您花時間寫出如此徹底的答案。我會盡量做出適當的調整,看看它是如何發展的。再次感謝您的回覆。 – chase366