hamaXD

Lab 7 SwingCounter+event

Mar 28th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class SwingCounter extends JFrame implements ActionListener{
  7.     private JButton btnOK = new JButton("Count");
  8.     private int cout;
  9.     private JTextField text = new JTextField(10);
  10.     public SwingCounter(String title){
  11.         setTitle(title);
  12.         setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
  13.         add(new JLabel("Counter"));
  14.         add(text);
  15.         text.setHorizontalAlignment(SwingConstants.RIGHT);
  16.         add(btnOK);
  17.         btnOK.addActionListener(this);
  18.     }
  19.  
  20.     public void actionPerformed(ActionEvent e) {
  21.         ++cout;
  22.         text.setText(""+cout);
  23.       //System.out.println(++cout);
  24.     }
  25.  
  26.  
  27.  
  28.     public static void main(String s[]) {
  29.         SwingCounter frame = new SwingCounter("Swing Counter");
  30.         frame.setSize(300, 100);
  31.         frame.setLocationRelativeTo(null);
  32.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.         frame.setVisible(true);
  34.  
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment