Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5.  
  6. public class SE3SimpleGui {
  7.  
  8.     private volatile JFrame frame;
  9.     private volatile JButton btn;
  10.     private volatile JTextField txt;
  11.     private volatile Count count;
  12.     private volatile Paint paint;
  13.     private volatile boolean currentlyCounting;
  14.  
  15.     private void createAndShow() {
  16.         /* Threads instanziieren */
  17.         currentlyCounting = false;
  18.  
  19.         /* links: ein Button mit Beschriftung */
  20.         btn = new JButton();
  21.         btn.addActionListener(new ActionListener());
  22.         btn.setText("Start");
  23.  
  24.         /* rechts: ein Textfeld */
  25.         txt = new JTextField();
  26.  
  27.         /* Hauptfenster */
  28.         frame = new JFrame();
  29.         /* Aktion beim schließen: JVM beenden */
  30.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.         /* Layout festlegen und Button & Textfeld hinzufügen */
  32.         frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),
  33.                 BoxLayout.X_AXIS));
  34.         frame.getContentPane().add(btn);
  35.         frame.getContentPane().add(txt);
  36.  
  37.         /* Größe setzen und anzeigen */
  38.         frame.setPreferredSize(new Dimension(300, 100));
  39.         frame.pack();
  40.         frame.setVisible(true);
  41.     }
  42.  
  43.     public static void main(String[] args) {
  44.         /* Im EDT ausführen: new SE3SimpleGui().createAndShow(); */
  45.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  46.             public void run() {
  47.                 new SE3SimpleGui().createAndShow();
  48.             }
  49.         });
  50.     }
  51.  
  52.     private class ActionListener implements java.awt.event.ActionListener {
  53.  
  54.         @Override
  55.         public void actionPerformed(ActionEvent e) {
  56.             if (!currentlyCounting) {
  57.                 btn.setText("Stop");
  58.                 count = new Count();
  59.                 paint = new Paint(count);
  60.                 count.start();
  61.                 paint.start();
  62.             } else {
  63.                 btn.setText("Start");
  64.                 count.stopping();
  65.                 paint.stopping();
  66.             }
  67.  
  68.             currentlyCounting = !currentlyCounting;
  69.             frame.pack();
  70.         }
  71.     }
  72.  
  73.     private class Count extends Thread {
  74.  
  75.         private volatile boolean stopped = false;
  76.         private volatile int count;
  77.  
  78.         @Override
  79.         public void run() {
  80.             while (!stopped) {
  81.                 count++;
  82.             }
  83.         }
  84.  
  85.         public void stopping() {
  86.             this.stopped = true;
  87.         }
  88.  
  89.     }
  90.  
  91.     private class Paint extends Thread {
  92.  
  93.         private volatile boolean stopped = false;
  94.         private Count c;
  95.  
  96.         public Paint(Count c) {
  97.             this.c = c;
  98.         }
  99.  
  100.         @Override
  101.         public void run() {
  102.             while (!stopped) {
  103.                 txt.setText(String.valueOf(c.count));
  104.                 frame.pack();
  105.                 try {
  106.                     Thread.sleep(500);
  107.                 } catch (InterruptedException ex) {
  108.                     System.err.println("Error Paint");
  109.                 }
  110.             }
  111.         }
  112.  
  113.         public void stopping() {
  114.             this.stopped = true;
  115.         }
  116.  
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement