Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.GridLayout;
  4. import java.awt.Panel;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.awt.event.WindowListener;
  10. import java.text.SimpleDateFormat;
  11.  
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPanel;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTextArea;
  19.  
  20. public class Z09 extends JFrame{
  21.  
  22. public static JPanel createpanels() {
  23. JTextArea ta1 = new JTextArea(30,30);
  24. JPanel panel1 = new JPanel(new BorderLayout());
  25. panel1.add(new JScrollPane(ta1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
  26. BorderLayout.CENTER);
  27. JButton susp = new JButton("SUSP");
  28. panel1.add(susp,BorderLayout.SOUTH);
  29.  
  30. MyThread th = new MyThread(ta1);
  31. th.start();
  32.  
  33. susp.addActionListener(new ActionListener() {
  34.  
  35. @Override
  36. public void actionPerformed(ActionEvent e) {
  37. if(susp.getText() == "GO") {
  38. th.resumeThread();
  39. susp.setText("SUSP");
  40. } else if (susp.getText() == "SUSP") {
  41. th.suspendThread();
  42. susp.setText("GO");
  43. }
  44. }
  45. });
  46.  
  47. return panel1;
  48. }
  49.  
  50. public static void main(String[] args) {
  51.  
  52. JFrame f = new JFrame("StopResume");
  53. f.setLayout(new GridLayout(1,2));
  54. f.add(createpanels());
  55. f.add(createpanels());
  56.  
  57. f.addWindowListener(new WindowAdapter() {
  58. public void windowClosing(WindowEvent e) {
  59. int a = JOptionPane.showConfirmDialog(f, "Are you sure you wanna exit?",
  60. "Approval", JOptionPane.OK_CANCEL_OPTION);
  61. if(a == JOptionPane.OK_OPTION)
  62. System.exit(0);
  63. }
  64. });
  65.  
  66. f.pack();
  67. f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  68. f.setVisible(true);
  69. f.setLocationRelativeTo(null);
  70. }
  71.  
  72. }
  73.  
  74. import java.text.DateFormat;
  75. import java.text.SimpleDateFormat;
  76. import java.util.Calendar;
  77. import java.util.Date;
  78.  
  79. import javax.swing.JTextArea;
  80.  
  81. public class MyThread extends Thread {
  82. JTextArea ta;
  83. boolean suspended = false;
  84.  
  85. public MyThread(JTextArea ta) {
  86. this.ta = ta;
  87. suspended = false;
  88. }
  89.  
  90. public void run() {
  91. while(isAlive()) {
  92. synchronized (this) {
  93. while(suspended) {
  94. try {
  95. wait();
  96. } catch (InterruptedException e1) {
  97. // TODO Auto-generated catch block
  98. e1.printStackTrace();
  99. }
  100. }
  101. while(!suspended) {
  102. Calendar cal = Calendar.getInstance();
  103. ta.append(cal.getTime().toString() + "\n");
  104. try {
  105. Thread.sleep((long) (Math.random() * 1500 + 500));
  106. } catch (InterruptedException e) {
  107. // TODO Auto-generated catch block
  108. e.printStackTrace();
  109. }
  110. }
  111. }
  112.  
  113. }
  114. }
  115.  
  116. public void suspendThread() {
  117. suspended = true;
  118. }
  119.  
  120. public void resumeThread() {
  121. suspended = false;
  122. synchronized(this) {
  123. notify();
  124. }
  125. }
  126.  
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement