Guest User

Untitled

a guest
Dec 10th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. import java.awt.event.*;
  5.  
  6. public class Countdown extends JPanel implements Runnable{
  7.  
  8. private Thread th;
  9. private JLabel p;
  10. private boolean partito=false;
  11. private boolean pausa;
  12.  
  13.  
  14. public Countdown(){
  15. setBackground(Color.white);
  16. setPreferredSize(new Dimension(500,200));
  17. JFrame f = new JFrame("Countdown");
  18. p = new JLabel("Contiamo....");
  19. add(p);
  20. f.setLayout(new BorderLayout());
  21. JPanel t = new JPanel();
  22. t.setPreferredSize(new Dimension (500,100));
  23. t.setBackground(Color.red);
  24.  
  25. JButton bottonestart = new JButton("Start");
  26. ActionListener listenstart = new ActionListener(){
  27. public void actionPerformed(ActionEvent ae){
  28. if(!partito){
  29. start();
  30. System.out.println("è partito il countdown...");
  31. partito=true;
  32. }
  33.  
  34. }
  35. };
  36. bottonestart.addActionListener(listenstart);
  37. t.add(bottonestart);
  38.  
  39. JButton bottonepausa = new JButton("Pausa");
  40. ActionListener listenpausa = new ActionListener(){
  41. public void actionPerformed(ActionEvent ae){
  42. if(partito){
  43. stop();
  44. }
  45. partito=false;
  46.  
  47. }
  48. };
  49. bottonepausa.addActionListener(listenpausa);
  50. t.add(bottonepausa);
  51.  
  52. JButton bottonecolore = new JButton("Colore");
  53. ActionListener listencolore = new ActionListener(){
  54. public void actionPerformed(ActionEvent ae){
  55. //cambio colore del pannello
  56. Color colore = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
  57. setBackground(colore);
  58. }
  59. };
  60. bottonecolore.addActionListener(listencolore);
  61. t.add(bottonecolore);
  62.  
  63. f.add(this, BorderLayout.CENTER);
  64. f.add(t, BorderLayout.SOUTH);
  65. f.pack();
  66. f.setVisible(true);
  67. f.setResizable(false);
  68. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  69.  
  70.  
  71. }
  72.  
  73. public void stop(){
  74. System.out.println("Stai bloccando il thread"+th);
  75. th = null;
  76.  
  77. }
  78.  
  79. public boolean isRunning(){
  80.  
  81.  
  82. return th != null;
  83.  
  84. }
  85.  
  86.  
  87.  
  88. public void start(){
  89.  
  90. if(!isRunning()){
  91.  
  92. th = new Thread(this);
  93. System.out.println("Sta partendo il thread"+th);
  94. th.start();
  95.  
  96. }
  97.  
  98.  
  99.  
  100. }
  101.  
  102.  
  103. public void run(){
  104.  
  105.  
  106. while(isRunning()){ // se il thread Ë partito
  107.  
  108. for(int i=10; i >= 0; i --){
  109. p.setText("siamo al numero .... "+i);
  110. try{
  111.  
  112. Thread.sleep(1000);
  113.  
  114. }catch(InterruptedException e){
  115.  
  116. e.printStackTrace();
  117. }
  118.  
  119. }
  120.  
  121. p.setText("Il countdown del thread "+th+" è finito :D");
  122. partito=false;
  123. stop();
  124. }
  125.  
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. public static void main(String[] args) {
  133.  
  134. new Countdown();
  135. }
  136.  
  137. }
Add Comment
Please, Sign In to add comment