Advertisement
a4ary4n

AWT_Counter

Nov 11th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class AWT_Counter {
  5.    
  6.     Frame f;
  7.     Label counter;
  8.     TextField count_display;
  9.     Button c_button;
  10.     int count = 0;
  11.    
  12.     AWT_Counter() {
  13.        
  14.         f = new Frame("AWT Counter");
  15.         f.setSize(300, 100);
  16.         f.setLayout(null);
  17.         f.setVisible(true);
  18.         f.addWindowListener(new WindowAdapter() {
  19.             public void windowClosing(WindowEvent wEvent) {
  20.                 System.exit(0);
  21.             }
  22.         });
  23.        
  24.         counter = new Label("Counter");
  25.         counter.setBounds(20, 40, 50, 30);
  26.         f.add(counter);
  27.        
  28.         count_display = new TextField();
  29.         count_display.setText(Integer.toString(count));
  30.         count_display.setBounds(100, 40, 100, 30);
  31.         f.add(count_display);
  32.         count_display.setEditable(false);
  33.        
  34.         c_button = new Button("Count");
  35.         c_button.setBounds(220, 40, 50, 30);
  36.         c_button.addActionListener(new ButtonClickListener());
  37.         f.add(c_button);
  38.     }
  39.    
  40.     private class ButtonClickListener implements ActionListener {
  41.    
  42.         @Override
  43.         public void actionPerformed(ActionEvent e) {
  44.             count++;
  45.             count_display.setText(Integer.toString(count));
  46.            
  47.         }
  48.        
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.        
  53.         AWT_Counter demo = new AWT_Counter();
  54.     }
  55.  
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement