Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.2 kB
7
Indexable
Never
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.util.Date;

import static java.awt.BorderLayout.*;

public class Main {

    public static JTextArea text1;
    public static JTextArea text2;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                ()->{

                    JFrame jFrame = new JFrame();
                    JPanel guziki = new JPanel();

                    guziki.setLayout(new BorderLayout());
                    jFrame.setLayout(new BorderLayout(2,2));

                    text1 = new JTextArea("");

                    text2 = new JTextArea("");

                    JScrollPane lewyTekst= new JScrollPane(text1);
                    lewyTekst.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                    lewyTekst.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                    lewyTekst.setPreferredSize(new Dimension(400,400));

                    JScrollPane prawyTekst= new JScrollPane(text2);
                    prawyTekst.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                    prawyTekst.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                    prawyTekst.setPreferredSize(new Dimension(400,400));


                    LinePrinter thr1 = new LinePrinter(true);
                    thr1.start();

                    LinePrinter thr2 = new LinePrinter(false);
                    thr2.start();

                    JButton but1 = new JButton("SUSP");
                    but1.setPreferredSize(new Dimension(200,30));
                    guziki.add(but1,LINE_START);


                    but1.addActionListener(e -> {
                        if(but1.getText()=="SUSP") {
                            thr2.isSuspended=true;
                            but1.setText("GO");
                        }
                        else {
                            thr2.isSuspended=false;
                            but1.setText("SUSP");
                        }

                    });

                    JButton but2 = new JButton("SUSP");
                    but2.setPreferredSize(new Dimension(200,30));
                    guziki.add(but2,LINE_END);

                    but2.addActionListener(e -> {
                        if(but2.getText()=="SUSP") {
                            thr1.isSuspended=true;
                            but2.setText("GO");
                        }
                        else {
                            thr1.isSuspended=false;
                            but2.setText("SUSP");
                        }

                    });

                    jFrame.add(lewyTekst,LINE_START);
                    jFrame.add(prawyTekst,LINE_END);
                    jFrame.add(guziki,SOUTH);


                    jFrame.setTitle("Program");
                    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    jFrame.setVisible(true);

                    jFrame.pack();


                }
        );
    }


}


class LinePrinter extends Thread {

    int time;
    Boolean czyDrugi;

    public final static Object obj = new Object();
    volatile boolean isSuspended = false;
    volatile boolean isStopped = false;

    public LinePrinter(Boolean czyDrugi) {
        time= (int)((Math.random()*900) + 100);
        this.czyDrugi=czyDrugi;

    }

    @Override
    public synchronized void run() {
        while (isStopped == false) {
            if (!isSuspended) {
                synchronized (obj) {
                    Date date=java.util.Calendar.getInstance().getTime();
                    if (czyDrugi)  Main.text2.append(date+"\n");
                    else Main.text1.append(date+"\n");
                }
                try {
                    Thread.sleep(time);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
        System.out.println("Thread "+" exits");

    }

    public void stopThread(){
        isStopped = true;
    }

}