Bikin Random Gui

public class RandomCounter_Test
{
    public static void main(String [] args)
    {
        //* Menjalankan fungsi thread dari kelas RandomCounterGUI dan menampilkannya
        RandomCounterGUI test=new RandomCounterGUI();
        test.setVisible(true);
    }
}

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

public class RandomCounterGUI extends JFrame
{
    //* Dekarasi atribut
    private JTextField textfield;
    private JButton startStopButton;
    private JPanel mainPanel;
    boolean start=true;
    RandomCounter counter;
   
    public RandomCounterGUI()
    {
        //* Mengatur semua tentang frame
        this.setLocation(100,100);
        this.setSize(200,100);
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Random Counter");
       
        //* Mengatur panel beserta komponen di dalamnya
        mainPanel=new JPanel();
        mainPanel.setLayout(new FlowLayout());
        startStopButton=new JButton("Start");
        textfield=new JTextField(10);
        textfield.setEditable(false);
       
        mainPanel.add(startStopButton);
        mainPanel.add(textfield);
       
        this.setContentPane(mainPanel);
       
        //* penanganan pada tombol startStopButton
        startStopButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                randomCounterAction();
            }
        });
    }
   
    //* method untuk memulai counter/menghitung secara acak
    public void randomCounterAction()
    {
        if(start)
        {
            counter=new RandomCounter(textfield, startStopButton);
            counter.startRandom();
            start=false;
        }
        else if(start==false)
        {
            counter.startRandom();
            start=true;
        }
    }
}

import javax.swing.*;

public class RandomCounter extends Thread
{
    //* Dekarasi atribut
    JTextField outField;
    JButton startStop;
    private int value;
    Thread thread;
    boolean start=false;
   
    //* Constructor
    public RandomCounter()
    {
       
    }
   
    public RandomCounter(JTextField outField, JButton startStop)
    {
        this.outField=outField;
        this.startStop=startStop;
    }
   
    //* Method menjalankan Thread
    public void startRandom()
    {
        thread=new Thread(this);
       
        if(start==false)
        {
            thread.start();
            start=true;
            startStop.setText("Stop");
        }
        else
        {
            start=false;           
            startStop.setText("Start"); 
        }   
    }
   
    //* Eksekusi ketika thread dijalankan
    @Override
    public void run()
    {
        while(start)
        {
            value=(int) (Math.random()*200);
            outField.setText(String.valueOf(value));
            try
            {
                thread.sleep(300);
            }
            catch(InterruptedException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

Komentar

Postingan populer dari blog ini

Bikin Amplop Cantik

Menyesap Rindu Dalam Coding (Sastra Edan #7)

Kost Anyar Nyah...