Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project1;
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import static java.lang.System.load;
- class mainPanel extends JPanel{
- private MyTimerPanel p1;
- private MyTimerPanel p2;
- private MyTimerPanel p3;
- public mainPanel(){
- p1 = new MyTimerPanel();
- p2 = new MyTimerPanel();
- p3 = new MyTimerPanel();
- add (p1);
- add (p2);
- add (p3);
- }
- }
- public class MyTimerPanel extends JPanel{
- private GeoCountDownTimer geoCountDownTimer;
- private Timer javaTimer;
- private TimerListener timer;
- private JLabel label;
- private JLabel output;
- private JButton incDay;
- private JButton decDay;
- private JButton inc1;
- private JButton dec1;
- private JButton compare;
- private JButton load;
- private JButton save;
- private JButton Days2Go;
- private JButton equal;
- private JButton daysInFuture;
- private JButton start;
- private JButton stop;
- private JButton enter;
- private JTextField value;
- public MyTimerPanel(){
- geoCountDownTimer = new GeoCountDownTimer();
- timer = new TimerListener();
- load = new JButton("Load Timer:");
- save = new JButton("Save Timer:");
- Days2Go = new JButton("Days to Go");
- equal = new JButton("Equal?");
- compare = new JButton("Compare to:");
- start = new JButton("Start");
- stop = new JButton("Stop");
- enter = new JButton("Enter");
- incDay = new JButton("Days to Increase");
- decDay = new JButton("Days to Decrease");
- inc1 = new JButton("+1 Day");
- dec1 = new JButton("-1 Day");
- daysInFuture = new JButton ("Days in Future");
- output = new JLabel("Please enter a date in the text field and press enter.");
- label = new JLabel("xx/xx/xxxx");
- value = new JTextField(" ");
- add (daysInFuture);
- add (output);
- add (load);
- add (save);
- add (Days2Go);
- add (equal);
- add (compare);
- add (inc1);
- add (dec1);
- add (incDay);
- add (decDay);
- add (start);
- add (stop);
- add (enter);
- add (new JLabel("Timer: "));
- add (label);
- add (new JLabel("Input Timer: "));
- add (value);
- start.addActionListener(new ButListener());
- stop.addActionListener(new ButListener());
- enter.addActionListener(new ButListener());
- incDay.addActionListener(new ButListener());
- decDay.addActionListener(new ButListener());
- load.addActionListener(new ButListener());
- save.addActionListener(new ButListener());
- Days2Go.addActionListener(new ButListener());
- equal.addActionListener(new ButListener());
- compare.addActionListener(new ButListener());
- inc1.addActionListener(new ButListener());
- dec1.addActionListener(new ButListener());
- daysInFuture.addActionListener(new ButListener());
- // this calls timer object 10 times per second
- javaTimer = new Timer(1000, timer); // 1000 * 60 * 60 * 24
- }
- private class TimerListener implements ActionListener{
- public void actionPerformed(ActionEvent e){
- geoCountDownTimer.inc();
- label.setText(geoCountDownTimer.toDateString());
- if (geoCountDownTimer.equals(new GeoCountDownTimer("1/1/2038"))){
- System.out.println ("Time is up!");
- javaTimer.stop();
- }
- }
- }
- private class ButListener implements ActionListener{
- public void actionPerformed(ActionEvent e)
- {
- if (start == e.getSource())
- javaTimer.start();
- else if (inc1 == e.getSource()) {
- geoCountDownTimer.inc();
- label.setText(geoCountDownTimer.toDateString());
- }
- else if (dec1 == e.getSource()) {
- geoCountDownTimer.dec();
- label.setText(geoCountDownTimer.toDateString());
- }
- else if (stop == e.getSource()) {
- javaTimer.stop();
- label.setText(geoCountDownTimer.toDateString());
- }
- else if (enter == e.getSource()) {
- geoCountDownTimer = new GeoCountDownTimer(value.getText().trim());
- label.setText(geoCountDownTimer.toDateString());
- }
- else if (decDay == e.getSource()) {
- geoCountDownTimer.dec(Integer.parseInt(value.getText().trim()));
- label.setText(geoCountDownTimer.toDateString());
- }
- else if (incDay == e.getSource()) {
- geoCountDownTimer.inc(Integer.parseInt(value.getText().trim()));
- label.setText(geoCountDownTimer.toDateString());
- }
- else if (load == e.getSource()) {
- load(value.getText().trim());
- label.setText(geoCountDownTimer.toDateString());
- }
- else if (save == e.getSource()) {
- geoCountDownTimer.save(value.getText().trim());
- label.setText(geoCountDownTimer.toDateString());
- }
- else if (compare == e.getSource()) {
- GeoCountDownTimer s = new GeoCountDownTimer(label.getText());
- GeoCountDownTimer s1 = new GeoCountDownTimer(value.getText().trim());
- if (s.compareTo(s1) == 1)
- output.setText("Your date is less than the Current Timer!");
- else if (s.compareTo(s1)==0)
- output.setText(("Your date is equal to the Current Timer!"));
- else if (s.compareTo(s1)==-1)
- output.setText("Your date is greater than the Current Timer!");
- else output.setText("Your date is invalid, please enter a date in the format 'xx/xx/xxxx'");
- }
- else if(Days2Go == e.getSource()) {
- int temp = 0;
- temp = geoCountDownTimer.daysToGo(value.getText().trim());
- output.setText(temp + " days remaining!");
- }
- else if(daysInFuture == e.getSource()) {
- GeoCountDownTimer temp = new GeoCountDownTimer(label.getText().trim());
- temp.daysInTheFuture(Integer.parseInt(value.getText().trim()));
- output.setText(temp.toDateString() + " is the date " + label.getText().trim() + " days in the future!");
- }
- else if (equal == e.getSource()){
- GeoCountDownTimer s = new GeoCountDownTimer(label.getText());
- GeoCountDownTimer s1 = new GeoCountDownTimer(value.getText().trim());
- if (s.equals(s1))
- output.setText(("Your date is equal to the Current Timer!"));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement