Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package javaapplication1;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Container;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.Random;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JTextArea;
  15. import javax.swing.Timer;
  16.  
  17. public class JavaApplication1 {
  18.     public static void main(String[] args) {
  19.         MyFrame frm = new MyFrame();
  20.     }
  21. }
  22.  
  23. class MyFrame extends JFrame {
  24.     public MyFrame(){
  25.         Container cont = getContentPane();
  26.         MyPanel panel = new MyPanel();
  27.         cont.add(panel);
  28.         setBounds(10,10,250,200);
  29.         setVisible(true);
  30.     }
  31. }
  32.  
  33. class MyPanel extends JPanel {
  34.     public MyPanel(){
  35.         this.setLayout(null);
  36.        
  37.         final JTextArea ar = new JTextArea("Your text",1,1);
  38.         ar.setBounds(10,50,100,20);
  39.         this.add(ar);
  40.        
  41.         final Timer t = new Timer(1000,new ActionListener(){
  42.             int i = 1;
  43.             public void actionPerformed(ActionEvent e){
  44.                 String str = new String();
  45.                 str = String.valueOf(i);
  46.                 ar.setText(str);
  47.                i++;
  48.             }
  49.         });
  50.        
  51.         JLabel label = new JLabel("Awi");
  52.         this.add(label);
  53.         label.setBounds(10,10,40,40);
  54.         Font f = new Font("Arial", Font.BOLD, 10);
  55.        
  56.        
  57.        
  58.         JButton b = new JButton();
  59.         b.setLabel("Timer");
  60.         b.setBounds(10,100,100,30);
  61.         this.add(b);
  62.         b.addActionListener(new ActionListener(){
  63.             public void actionPerformed(ActionEvent e){
  64.                 t.start();
  65.             }
  66.         });
  67.        
  68.         JButton b1 = new JButton();
  69.         b1.setLabel("Random");
  70.         b1.setBounds(120,100,100,30);
  71.         this.add(b1);
  72.         b1.addActionListener(new ActionListener(){
  73.             public void actionPerformed(ActionEvent e){
  74.                 t.stop();
  75.                 Random rnd = new Random();
  76.                 int num = rnd.nextInt();
  77.                 String str = new String();
  78.                 str = String.valueOf(num);
  79.                 ar.setText(str);
  80.             }
  81.         });  
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement