Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication1;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Random;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- import javax.swing.Timer;
- public class JavaApplication1 {
- public static void main(String[] args) {
- MyFrame frm = new MyFrame();
- }
- }
- class MyFrame extends JFrame {
- public MyFrame(){
- Container cont = getContentPane();
- MyPanel panel = new MyPanel();
- cont.add(panel);
- setBounds(10,10,250,200);
- setVisible(true);
- }
- }
- class MyPanel extends JPanel {
- public MyPanel(){
- this.setLayout(null);
- final JTextArea ar = new JTextArea("Your text",1,1);
- ar.setBounds(10,50,100,20);
- this.add(ar);
- final Timer t = new Timer(1000,new ActionListener(){
- int i = 1;
- public void actionPerformed(ActionEvent e){
- String str = new String();
- str = String.valueOf(i);
- ar.setText(str);
- i++;
- }
- });
- JLabel label = new JLabel("Awi");
- this.add(label);
- label.setBounds(10,10,40,40);
- Font f = new Font("Arial", Font.BOLD, 10);
- JButton b = new JButton();
- b.setLabel("Timer");
- b.setBounds(10,100,100,30);
- this.add(b);
- b.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- t.start();
- }
- });
- JButton b1 = new JButton();
- b1.setLabel("Random");
- b1.setBounds(120,100,100,30);
- this.add(b1);
- b1.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- t.stop();
- Random rnd = new Random();
- int num = rnd.nextInt();
- String str = new String();
- str = String.valueOf(num);
- ar.setText(str);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement