Hi i am trying to make taxi meter which shows the current price.Rightnow I have to click startmeter afterevery 1 minute than it's update the new price but I want it to update automatically once the price change after 1 minute instead of me pressing startmeter everytime.thanks for your help.

import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.event.ActionListener;
import javax.swing.Timer; 
import java.awt.event.*;

public class taximeter extends JFrame{

    JButton start;
    JButton end;
    JLabel total;
    JPanel panel;
    Container pane;
    long star=-1;
    long sto=-1;





    public taximeter(){

     super("Taxi Meter");


      JButton start=new JButton("Start meter");
      JButton stop=new JButton("Stop meter");
        JLabel total=new JLabel("$0.00");

     star=System.currentTimeMillis();




        panel=new JPanel();

        panel.setLayout(null);
        panel.setVisible(true);
        panel.add(start);
        panel.add(stop);
      panel.add(total);




        start.setBounds(40, 50, 120, 40);


        stop.setBounds(40, 125, 120, 40);

        total.setBounds(75, 5, 120, 40);



        Container pane=getContentPane();

        pane.add(panel);
        pane.setVisible(true);



        GregorianCalendar jc=new GregorianCalendar();
        int lj=jc.getTime().getHours();
        int tj=jc.get(Calendar.HOUR);
        int ne=jc.get(Calendar.MINUTE);
        int jx=jc.get(Calendar.SECOND);






























        stop.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e){

                if(e.getSource()==stop){


                    total.setText("$ 5.00");


                }

            }
        }





        );

           start.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){

                        int ld=jc.get(Calendar.MINUTE);
                        int mi=ne-ld;
                        int kd=mi*2;
                        int jm=kd+5;
                        int bc=jc.get(Calendar.HOUR);
                        int dc=(ld-ne)*5;







                        if(e.getSource()==start){



                    sto = System.currentTimeMillis() - star;
                    long lp = sto/(60*1000)% 60;
                    long ts=(lp*3)+3;












                    String lq=Long.toString(ts);



                        total.setText(lq);

                        panel.setVisible(true);


                   }     


                        }







                });















    }






    public static void main(String[] args) {

  taximeter mw=new taximeter();
  mw.setVisible(true);
  mw.setSize(200, 200);












    }



    }

Recommended Answers

All 2 Replies

Use a javax.swing.Timer to call a method that updates the info every 60,000 milliseconds. See the API doc for details

also, for readability purposes, remove all those blank lines in your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.