Advertisement
Guest User

Part 2

a guest
Feb 14th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Lab04Part2 extends JFrame {
  6.  
  7. public boolean keepGoing = true;
  8.  
  9. public JButton jbButton = new JButton("Start");
  10.  
  11. public static void main(String[] args){
  12. new Lab04Part2();
  13. }
  14.  
  15. public Lab04Part2(){
  16. setupWindow();
  17.  
  18. InnerProgress r1 = new InnerProgress(1);
  19. InnerProgress r2 = new InnerProgress(2);
  20.  
  21. this.setLayout(new GridLayout(0,1));
  22. this.add(jbButton);
  23.  
  24. this.add(r1);
  25. this.add(r2);
  26.  
  27. jbButton.addActionListener(new ActionListener(){
  28. public void actionPerformed(ActionEvent ae){
  29. if(keepGoing == false){
  30. keepGoing = true;
  31. }
  32.  
  33. Thread t1 = new Thread(r1);
  34. Thread t2 = new Thread(r2);
  35.  
  36. t1.start();
  37. t2.start();
  38.  
  39. jbButton.setEnabled(false);
  40. Thread extra = new Thread(){
  41. public void run(){
  42. try{
  43. t1.join();
  44. t2.join();
  45. }catch(InterruptedException ie){System.out.println(ie);}
  46. jbButton.setEnabled(true);
  47. }};
  48. extra.start();
  49.  
  50.  
  51. }
  52. });
  53. this.setVisible(true);
  54. }
  55.  
  56.  
  57.  
  58.  
  59. public void setupWindow(){
  60. this.setTitle("Lab04Part2");
  61. this.pack();
  62. this.setLocationRelativeTo(null);
  63. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64.  
  65. }
  66.  
  67.  
  68. class InnerProgress extends JPanel implements Runnable {
  69.  
  70. public int line = 0;
  71.  
  72. public JLabel jlOne = new JLabel();
  73. public JProgressBar jprogOne = new JProgressBar(0,80);
  74.  
  75. public InnerProgress(int i){
  76. line = i;
  77. jlOne.setText("Progess " + line + ": ");
  78. this.add(jlOne);
  79. this.add(jprogOne);
  80. jprogOne.setStringPainted(true);
  81. jprogOne.setIndeterminate(true);
  82.  
  83. }
  84. public void run(){
  85. jprogOne.setIndeterminate(false);
  86. jprogOne.setValue(0);
  87. for(int i = 0; i <= 80 && keepGoing == true; i++){
  88. double r = Math.random();
  89.  
  90. double inMilli = r * 100;
  91.  
  92. try{
  93. Thread.sleep((long)inMilli);
  94. }catch(InterruptedException ie){System.out.println("Interrupted" + ie);}
  95.  
  96. jprogOne.setValue(i);
  97. }
  98. keepGoing = false;
  99. System.out.println("This is running thread " + line + ". Finishing in " + System.currentTimeMillis() + " milliseconds.");
  100.  
  101. }
  102.  
  103.  
  104. }
  105.  
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement