Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package awtHomework9;
- import java.awt.*;
- import java.awt.event.*;
- public class AWTHomework9 extends Frame implements WindowListener, ActionListener, Runnable {
- Panel left, right;
- Label thread1, thread2, stepLabelLeft, stepLabelRight;
- TextField leftText, rightText;
- Choice stepChoiceLeft, stepChoiceRight;
- Button startLeft, startRight, stopLeft, stopRight;
- private Thread tLeft, tRight;
- private int sumLeft, sumRight = 0;
- private boolean leftIsRunning = false, rightIsRunning = false;
- public AWTHomework9() {
- // left part
- thread1 = new Label("Thread 1");
- leftText = new TextField();
- stepLabelLeft = new Label("Step: ");
- stepChoiceLeft = new Choice();
- stepChoiceLeft.addItem("1");
- stepChoiceLeft.addItem("5");
- stepChoiceLeft.addItem("10");
- startLeft = new Button("Start");
- startLeft.addActionListener(this);
- stopLeft = new Button("Stop");
- stopLeft.addActionListener(this);
- Panel [] panels = new Panel[4];
- for(int i=0; i<4; i++) {
- panels[i] = new Panel( new GridLayout(1,0));
- }
- panels[0].add(thread1);
- panels[1].add(leftText);
- panels[2].add(stepLabelLeft);
- panels[2].add(stepChoiceLeft);
- panels[3].add(startLeft);
- panels[3].add(stopLeft);
- left = new Panel(new GridLayout(0,1));
- for(Panel x: panels){
- left.add(x);
- }
- // right part
- thread2 = new Label("Thread 2");
- rightText = new TextField();
- stepLabelRight = new Label("Step: ");
- stepChoiceRight = new Choice();
- stepChoiceRight.addItem("1");
- stepChoiceRight.addItem("5");
- stepChoiceRight.addItem("10");
- startRight = new Button("Start");
- startRight.addActionListener(this);
- stopRight = new Button("Stop");
- stopRight.addActionListener(this);
- Panel [] panels2 = new Panel[4];
- for(int i=0; i<4; i++) {
- panels2[i] = new Panel( new GridLayout(1,0));
- }
- panels2[0].add(thread2);
- panels2[1].add(rightText);
- panels2[2].add(stepLabelRight);
- panels2[2].add(stepChoiceRight);
- panels2[3].add(startRight);
- panels2[3].add(stopRight);
- right = new Panel(new GridLayout(0,1));
- for(Panel x: panels2){
- right.add(x);
- }
- //the hole thing
- setLayout( new GridLayout(1,0));
- addWindowListener(this);
- add(left);
- add(right);
- setTitle("Thread Demo");
- setSize(500, 350);
- setVisible(true);
- }
- public static void main(String[] args) {
- new AWTHomework9();
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource().equals(startLeft)) {
- if(!leftIsRunning) { //за да не се стартира повече от 1 нишка
- tLeft = new Thread( this );
- leftIsRunning = true;
- tLeft.start();
- }
- }
- if (e.getSource().equals(stopLeft)) {
- stopLeft();
- }
- if (e.getSource().equals(startRight)) {
- if(!rightIsRunning) {
- tRight = new Thread( this );
- rightIsRunning = true;
- tRight.start();
- }
- }
- if (e.getSource().equals(stopRight)) {
- stopRight();
- }
- }
- public void run() {
- if (Thread.currentThread().equals(tLeft)) {
- while(leftIsRunning) {
- try {
- Thread.sleep(1000);
- switch(stepChoiceLeft.getSelectedIndex()) {
- case 0: sumLeft += 1;
- break;
- case 1: sumLeft += 5;
- break;
- case 2: sumLeft += 10;
- break;
- }
- leftText.setText(""+sumLeft);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- if (Thread.currentThread().equals(tRight)) {
- while(rightIsRunning) {
- try {
- Thread.sleep(1000);
- switch(stepChoiceRight.getSelectedIndex()) {
- case 0: sumRight += 1;
- break;
- case 1: sumRight += 5;
- break;
- case 2: sumRight += 10;
- break;
- }
- rightText.setText(""+sumRight);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public void stopLeft() {
- leftIsRunning = false;
- }
- public void stopRight() {
- rightIsRunning = false;
- }
- @Override
- public void windowActivated(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowClosed(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- @Override
- public void windowDeactivated(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowDeiconified(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowIconified(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void windowOpened(WindowEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment