TY_BCA_2015_ADV_JAVA_15MARKS_SLIP12



SLIP12:  Write a java program to display the number’s between 1 to 100 continuously in a TextField by clicking on button. (use Runnable Interface)

import java.awt.event.*;
import javax.swing.*;

class Message implements Runnable
{
            JTextField t;
            public void run()
            {
                        for(int i =1; i<=100;i++)
                        {
                                    t.setText(""+i);
                                    try
                                    {
                                                Thread.sleep(50);
                                    }
                                    catch(Exception  e)
                                    {
                                                e.printStackTrace();
                                    }
                        }
            }
}
class Slip12_1 implements ActionListener
{
            JFrame f;
            JPanel p;
            JTextField t;
            JButton b;
            Thread t1;

            Slip12_1()
            {
                        f = new JFrame();
                        p = new JPanel();

                        t = new JTextField(60);
                        b = new JButton("Start");

                        t1 = new Thread(this);

                        b.addActionListener(this);

                        p.add(t);
                        p.add(b);

                        f.add(p);
                        f.setSize(400, 400);
                        f.setVisible(true);
            }


            public void actionPerformed(ActionEvent e)
            {
                        t1.start();
            }   
}

1 comment: