有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java AppletViewer出现错误并试图加入计时器

我现在正在做一个红绿灯,它可以自动改变灯光

我拥有的:我目前有一个刹车灯的代码,可以通过按下按钮来工作

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

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 23.01.2017
  * @author 
  */

public class ampel extends JApplet {
  // Anfang Attribute
  private JLabel jLabel1 = new JLabel();
  private JLabel jLabel2 = new JLabel();
  private JLabel jLabel3 = new JLabel();
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  private JButton jButton3 = new JButton();
  private JButton jButton4 = new JButton();
  private JLabel jLabel4 = new JLabel();
  private JLabel jLabel5 = new JLabel();
  private JLabel jLabel6 = new JLabel();
  // Ende Attribute

  public void init() {
    Container cp = getContentPane();
    cp.setLayout(null);
    cp.setBounds(0, 0, 314, 300);
    // Anfang Komponenten
    jButton1.setVisible(true);
    jButton2.setVisible(false);
    jButton3.setVisible(false);
    jButton4.setVisible(false);
    jLabel1.setBounds(16, 24, 75, 41);
    jLabel1.setText("");
    jLabel1.setOpaque(true);
    cp.add(jLabel1);
    jLabel2.setBounds(16, 88, 75, 33);
    jLabel2.setText("");
    jLabel2.setOpaque(true);
    cp.add(jLabel2);
    jLabel3.setBounds(16, 144, 75, 33);
    jLabel3.setText("");
    jLabel3.setOpaque(true);
    cp.add(jLabel3);
    jButton1.setBounds(112, 96, 73, 25);
    jButton1.setText("jButton1");
    jButton1.setMargin(new Insets(2, 2, 2, 2));
    jButton1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton1_ActionPerformed(evt);
      }
    });
    cp.add(jButton1);
    jButton2.setBounds(112, 96, 73, 25);
    jButton2.setText("jButton2");
    jButton2.setMargin(new Insets(2, 2, 2, 2));
    jButton2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton2_ActionPerformed(evt);
      }
    });
    cp.add(jButton2);
    jButton3.setBounds(112, 96, 73, 25);
    jButton3.setText("jButton3");
    jButton3.setMargin(new Insets(2, 2, 2, 2));
    jButton3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton3_ActionPerformed(evt);
      }
    });
    cp.add(jButton3);
    jButton4.setBounds(112, 96, 73, 25);
    jButton4.setText("jButton4");
    jButton4.setMargin(new Insets(2, 2, 2, 2));
    jButton4.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton4_ActionPerformed(evt);
      }
    });
    cp.add(jButton4);
    cp.setBackground(new Color(0xC0C0C0));
    jLabel4.setBounds(224, 16, 75, 49);
    jLabel4.setText("");
    jLabel4.setBackground(Color.WHITE);
    jLabel4.setOpaque(true);
    cp.add(jLabel4);
    jLabel5.setBounds(224, 80, 75, 49);
    jLabel5.setText("");
    jLabel5.setBackground(Color.WHITE);
    jLabel5.setOpaque(true);
    cp.add(jLabel5);
    jLabel6.setBounds(224, 144, 75, 33);
    jLabel6.setText("");
    jLabel6.setBackground(Color.WHITE);
    jLabel6.setOpaque(true);
    cp.add(jLabel6);
    // Ende Komponenten

  } // end of init

  // Anfang Methoden
  public void jButton1_ActionPerformed(ActionEvent evt) {
    jButton2.setVisible(true);
    jButton1.setVisible(false);

    jLabel1.setBackground(new Color(255,0,0));
    jLabel2.setBackground(new Color(255,255,255));
    jLabel3.setBackground(new Color(255,255,255));

    jLabel4.setBackground(new Color(255,255,255));
    jLabel5.setBackground(new Color(255,255,255));
    jLabel6.setBackground(new Color(0,255,0));
  } // end of jButton1_ActionPerformed

  public void jButton2_ActionPerformed(ActionEvent evt) {
    jButton3.setVisible(true);
    jButton2.setVisible(false);
    jLabel1.setBackground(new Color(255,0,0));
    jLabel2.setBackground(new Color(255,255,0));
    jLabel3.setBackground(new Color(255,255,255));

    jLabel4.setBackground(new Color(255,255,255));
    jLabel5.setBackground(new Color(255,255,0));
    jLabel6.setBackground(new Color(255,255,255));
  } // end of jButton2_ActionPerformed

  public void jButton3_ActionPerformed(ActionEvent evt) {
    jButton4.setVisible(true);
    jButton3.setVisible(false);
    jLabel1.setBackground(new Color(255,255,255));
    jLabel2.setBackground(new Color(255,255,255));
    jLabel3.setBackground(new Color(0,255,0));

    jLabel4.setBackground(new Color(255,0,0));
    jLabel5.setBackground(new Color(255,255,255));
    jLabel6.setBackground(new Color(255,255,255));
  } // end of jButton3_ActionPerformed

  public void jButton4_ActionPerformed(ActionEvent evt) {
    jButton1.setVisible(true);
    jButton4.setVisible(false);
    jLabel1.setBackground(new Color(255,255,255));
    jLabel2.setBackground(new Color(255,255,0));
    jLabel3.setBackground(new Color(255,255,255));

    jLabel4.setBackground(new Color(255,0,0));
    jLabel5.setBackground(new Color(255,255,0));
    jLabel6.setBackground(new Color(255,255,255));
  } // end of jButton4_ActionPerformed

  // Ende Methoden

} // end of class ampel

现在谈谈我的问题:

  1. 似乎我的appletviewer被窃听了。(我的链接代码截图,按Stoplight)

enter image description here

enter image description here

这两个Gif应该会给你一个想法

  1. 我已经阅读了几篇关于如何制作计时器和方法的文章,但我现在真的不知道如何将其实现到源代码中,或者如何将其更改为与计时器一起工作

我是这样理解的:

做一个方法,上面写着做x,y,z

public void timer1_ActionPerformed(ActionEvent evt) {

    timer.setInitialDelay

    // tell him to do x

    Thread.sleep(5000); // or something similare so it will wait 5 seconds

    // Tell him to do y
    // wait

    // Tell him to do z
    // ...repeat
}

共 (0) 个答案