I'm currently working on a Stoplight, which changes lights automatically.
What I have: I currently have a code for a Stoplight that works with a button press.
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
Now to my Problems:
Seemingly my appletviewer is bugged. (Screenshots of my linked Code, buttonpress Stoplight)
These 2 Gif's should give you an idea.
- I've read through several articles in how to make a timer and methods, but i don't really know now How to implement it into my source code or change it so it works with a timer.
I understood it like this:
Make a Method which says do 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
}