F Content b1 b2 lb1 lb2 lb3 A T
F Content b1 b2 lb1 lb2 lb3 A T
ActionListener; public class adnan { static JFrame f; static Container content; static JButton b1,b2; static JLabel lb1,lb2,lb3; static int a; static javax.swing.Timer t; public static void main(String[] args) { f=new JFrame("stopwatch"); f.setBounds(300/2,400/2,500/2,600/5); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); content=f.getContentPane(); content.setBackground(Color.CYAN); GridLayout gl=new GridLayout(3,2); content.setLayout(gl); a=1; lb1=new JLabel(""); content.add(lb1); b1=new JButton("Start"); content.add(b1); lb2=new JLabel(""); content.add(lb2); b2=new JButton("end"); content.add(b2); lb3=new JLabel(""); content.add(lb3); f.setVisible(true); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Calendar now=Calendar.getInstance(); int hh=now.get(Calendar.HOUR_OF_DAY); int mm=now.get(Calendar.MINUTE); int ss=now.get(Calendar.SECOND); lb1.setText(hh+":"+mm+":"+ss); } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ t=new Timer(100,new ActionListener(){ public void actionPerformed(ActionEvent e){ Calendar now=Calendar.getInstance(); int hh=now.get(Calendar.HOUR_OF_DAY); int mm=now.get(Calendar.MINUTE);
int ss=now.get(Calendar.SECOND); lb2.setText(hh+":"+mm+":"+ss); t.stop(); int a=Integer.parseInt(lb1.getText()); int b=Integer.parseInt(lb2.getText()); int c=b-a; lb3.setText(String.valueOf(c)); } }); t.start(); }}); } }
Q.2 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Calendar; public class wa { static JFrame frame; static JLabel lb1, lb2, lb3, lb4; static JTextField tx t 1,tx t 2,tx t 3; static JPanel panel; static Calendar cal; static int d, m ,y; static JButton b1; public static void main(String[] args) { frame = new JFrame("Age Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 170); panel = new JPanel(); GridLayout gl = new GridLayout(4,2); panel.setLayout(gl); lb1 = new JLabel(" Days:"); panel.add(lb1); tx t 1=new JTextField(2); panel.add(tx t 1); lb2= new JLabel("Month:"); panel.add(lb2); tx t 2=new JTextField(2); panel.add(tx t 2); lb3 = new JLabel("Year:"); panel.add(lb3); tx t 3=new JTextField(2);
panel.add(tx t 3); b1 = new JButton("age Calculate"); panel.add(b1); lb4 = new JLabel(" "); panel.add(lb4); frame.add(panel); frame.setResizable(false); frame.setVisible(true); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ cal = Calendar.getInstance(); y = cal.get(Calendar.YEAR); d = cal.get(Calendar.DATE); m = cal.get(Calendar.MONTH); int date =Integer.parseInt(tx t 1.getText()); int mon = Integer.parseInt(tx t 2.getText()); int year = Integer.parseInt(tx t 3.getText()); if (date > d){ d = d + 30; d = d - date; m--; } else { d = d - date; } if (mon > m){ m = m + 12; m = m - mon; y--; } else { m = m -mon; } y = y - year; lb4.setText(" " + d +" - " +m + " - " + y ); }}); }}