java - 使用 If 或 Switch 语句更改交通信号灯

标签 java swing if-statement japplet

我一直在尝试为 Java 编程入门类(class)编写交通灯模拟程序。当我运行该小程序时,即使我尚未单击任何 JButton,所有三个灯仍然亮起。当我这样做时,小程序会暂时变成空白,就像它正在做某事一样,如果我按“GO”,汽车就会移动。我想知道是否需要重置图形类中的颜色,或者使用 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();
                   }

    } 


    /**

最佳答案

When I run the applet, all three lights are still lit up even though I haven't clicked any of the JButtons yet

这是因为在每个 if 语句之后都有一个 ;,这基本上使您的逻辑短路,因此无论它后面的代码如何,总是会执行变量的状态...

if (clickSTOP == true);

因此,您应该使用更像的东西

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

您还可以在 paint 方法中修改程序的状态,通常不鼓励这样做,因为 paint 应该这样做,绘制当前状态。

更改状态的更好位置是在 ActionListener 中。

您应该拥有一个状态变量,而不是拥有三个通常描述相同事物的状态变量,因此状态只能是 gowait停止,绝不是三者的组合。

同样,您可以使用 ButtonGroupJToggleButtonJRadioButton,这将允许按钮携带一定数量的有关状态的信息,因为一次只能选择一个按钮。

看看How to Use the ButtonGroup ComponentHow to Use Buttons, Check Boxes, and Radio Buttons了解更多详情

When I do, the applet does turn blank momentarily like it's doing something

这可能是由于您从 paint 方法中加载图像引起的,但作为一般规则,您应该避免覆盖顶级容器的 paint像这样。

关于java - 使用 If 或 Switch 语句更改交通信号灯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32939530/

相关文章:

if-statement - Racket编程——我的 map

java - 在 java 中绘制图形 - NetBeans IDE

java - 监控jms消息的工具

java - JComboBox 中的问题

Python if 语句检查字符串中的英镑符号?

if-statement - Cobol - 检查 IF 子句中的余数

java - 什么是单框架应用程序,什么是 FrameView?

java - 在 Android 的 GridView 中放置图像(从 URL 加载)和编辑文本框

java - 如何动态改变JLabel的内容

java - 滚动 Pane 不显示内容