Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.awt.Panel;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import java.text.SimpleDateFormat;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- public class Z09 extends JFrame{
- public static JPanel createpanels() {
- JTextArea ta1 = new JTextArea(30,30);
- JPanel panel1 = new JPanel(new BorderLayout());
- panel1.add(new JScrollPane(ta1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
- BorderLayout.CENTER);
- JButton susp = new JButton("SUSP");
- panel1.add(susp,BorderLayout.SOUTH);
- MyThread th = new MyThread(ta1);
- th.start();
- susp.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if(susp.getText() == "GO") {
- th.resumeThread();
- susp.setText("SUSP");
- } else if (susp.getText() == "SUSP") {
- th.suspendThread();
- susp.setText("GO");
- }
- }
- });
- return panel1;
- }
- public static void main(String[] args) {
- JFrame f = new JFrame("StopResume");
- f.setLayout(new GridLayout(1,2));
- f.add(createpanels());
- f.add(createpanels());
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- int a = JOptionPane.showConfirmDialog(f, "Are you sure you wanna exit?",
- "Approval", JOptionPane.OK_CANCEL_OPTION);
- if(a == JOptionPane.OK_OPTION)
- System.exit(0);
- }
- });
- f.pack();
- f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- f.setVisible(true);
- f.setLocationRelativeTo(null);
- }
- }
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import javax.swing.JTextArea;
- public class MyThread extends Thread {
- JTextArea ta;
- boolean suspended = false;
- public MyThread(JTextArea ta) {
- this.ta = ta;
- suspended = false;
- }
- public void run() {
- while(isAlive()) {
- synchronized (this) {
- while(suspended) {
- try {
- wait();
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- while(!suspended) {
- Calendar cal = Calendar.getInstance();
- ta.append(cal.getTime().toString() + "\n");
- try {
- Thread.sleep((long) (Math.random() * 1500 + 500));
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
- public void suspendThread() {
- suspended = true;
- }
- public void resumeThread() {
- suspended = false;
- synchronized(this) {
- notify();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement