I’m stuck adding a timestamp at 1-sec intervals to a GUI.
I’m stuck adding a timestamp at 1-sec intervals to a GUI. Below is my GUI and attempted timestamp class:
import javax.swing.*;
import javax.swing.JPanel;
public class Main {
//Constructs the main JFrame that allows traffic choices
public static void main(String[] args){
JFrame frame = new JFrame (“Traffic Analyzer”);
frame.setVisible(true);
frame.setSize (500,450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel (“Choose an option to execute: “);
JPanel panel = new JPanel();
frame.add(panel);
panel.add(label);
//Creates a button for each option
JButton button0 = new JButton (“Current Time Stamp”);
JButton button1 = new JButton (“Traffic Light Display At 3 Intersections”);
JButton button2 = new JButton (“Positions & Speed of 3 Cars Passing 3 Intersections”);
panel.add(button0);
panel.add(button1);
panel.add(button2);
//Allows for a reaction once button is pushed
button0.addActionListener(new Action());
button1.addActionListener(new Action1());
button2.addActionListener(new Action2());
}
}
import java.sql.Timestamp;
import java.util.Date;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Action extends JFrame implements ActionListener {
public void actionPerformed (ActionEvent e){
JFrame frame2 = new JFrame(“TimeStamp”);
frame2.setSize(390,300);
frame2.setLocation(100,150);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel labeltimestamp = new JLabel(“Current TimeStamp: “+ new Timestamp(date.getTime()));
labeltimestamp.setBounds(50,50,200,40);
frame2.add(labeltimestamp);
frame2.setLayout(null);
frame2.setVisible(true);
int i = 1;
while(true){
Date date = new Date();
i++;
}
}
}
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"
