Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Lab04Part2 extends JFrame {
- public boolean keepGoing = true;
- public JButton jbButton = new JButton("Start");
- public static void main(String[] args){
- new Lab04Part2();
- }
- public Lab04Part2(){
- setupWindow();
- InnerProgress r1 = new InnerProgress(1);
- InnerProgress r2 = new InnerProgress(2);
- this.setLayout(new GridLayout(0,1));
- this.add(jbButton);
- this.add(r1);
- this.add(r2);
- jbButton.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent ae){
- if(keepGoing == false){
- keepGoing = true;
- }
- Thread t1 = new Thread(r1);
- Thread t2 = new Thread(r2);
- t1.start();
- t2.start();
- jbButton.setEnabled(false);
- Thread extra = new Thread(){
- public void run(){
- try{
- t1.join();
- t2.join();
- }catch(InterruptedException ie){System.out.println(ie);}
- jbButton.setEnabled(true);
- }};
- extra.start();
- }
- });
- this.setVisible(true);
- }
- public void setupWindow(){
- this.setTitle("Lab04Part2");
- this.pack();
- this.setLocationRelativeTo(null);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- class InnerProgress extends JPanel implements Runnable {
- public int line = 0;
- public JLabel jlOne = new JLabel();
- public JProgressBar jprogOne = new JProgressBar(0,80);
- public InnerProgress(int i){
- line = i;
- jlOne.setText("Progess " + line + ": ");
- this.add(jlOne);
- this.add(jprogOne);
- jprogOne.setStringPainted(true);
- jprogOne.setIndeterminate(true);
- }
- public void run(){
- jprogOne.setIndeterminate(false);
- jprogOne.setValue(0);
- for(int i = 0; i <= 80 && keepGoing == true; i++){
- double r = Math.random();
- double inMilli = r * 100;
- try{
- Thread.sleep((long)inMilli);
- }catch(InterruptedException ie){System.out.println("Interrupted" + ie);}
- jprogOne.setValue(i);
- }
- keepGoing = false;
- System.out.println("This is running thread " + line + ". Finishing in " + System.currentTimeMillis() + " milliseconds.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement