В настоящее время я работаю над стоп-сигналом, который автоматически меняет свет.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
Теперь мои проблемы:
Казалось бы мой Appletviewer прослушивается. (скриншоты моего связанного кодекса, ButtonPress Stoplight)
Эти 2 Gif должны дать вам представление.
- Я прочитал несколько статей о том, как создать таймер и методы, но я действительно не знаю сейчас. Как реализовать его в своем исходном коде или изменить его так, чтобы он работает с таймером.
Я понял это так:
сделать метод, который говорит, что делать х, у, г ...
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
}
где ваш код? –
Я сделал это выделенным жирным шрифтом в моем редактировании. – Shaddy
Я бы «догадался», что ваша проблема начинается с использования апплета и запускается в «Thread.sleep (5000);», что, вероятно, блокирует EDT и предотвращает обновление пользовательского интерфейса. Апплеты устарели (возможно, не в JDK, а в жизни), почти все браузеры либо активно блокируют их, либо отказываются от поддержки для них, тем лучше их вообще избегать. При «угадывании» вы используете Swing 'Timer', который находится в правильном направлении, но' Timer' будет ждать вас, вам не нужно ничего делать самостоятельно. Подумайте о «Таймер» как псевдо-петле, каждый тик - итерация цикла. – MadProgrammer