clanecollege

ClickCounterGUI.java

May 23rd, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JTextField;
  9.  
  10. public class ClickCounterGUI extends JFrame implements ActionListener
  11. {
  12.  
  13.     private JTextField tf1 = new JTextField("Clicked 0 time(s).", 15);
  14.     private JTextField tf2 = new JTextField("Copied text here.", 15);
  15.     private JButton b1;
  16.     private JButton b2;
  17.     private JButton b3;
  18.     private int numClicks = 0;
  19.    
  20.     private String b1Text = "Click the button to increment the counter.";
  21.     private String b2Text = "Clear contents of both text fields.";
  22.     private String b3Text = "Copy contents of first text field to second text field.";
  23.  
  24.     ClickCounterGUI()
  25.     {
  26.         b1 = new JButton("Click Me!");
  27.         b2 = new JButton("Clear");
  28.         b3 = new JButton("Copy");
  29.         b1.addActionListener(this);
  30.         b2.addActionListener(this);
  31.         b3.addActionListener(this);
  32.         b1.setToolTipText(b1Text);
  33.         b2.setToolTipText(b2Text);
  34.         b3.setToolTipText(b3Text);
  35.         add(b1);
  36.         add(b2);
  37.         add(b3);
  38.         add(tf1);
  39.         add(tf2);
  40.         setSize(250, 400);
  41.         setLayout(new GridLayout(5, 0));
  42.         setTitle("Click Counter GUI");
  43.         tf1.setBackground(Color.PINK);
  44.         tf1.setForeground(Color.BLACK);
  45.         tf2.setBackground(Color.MAGENTA);
  46.         tf2.setForeground(Color.BLACK);
  47.         tf1.setEditable(false);
  48.         setResizable(false);
  49.         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  50.         setVisible(true);
  51.     }
  52.  
  53.     public void actionPerformed(ActionEvent e)
  54.     {
  55.         if (e.getActionCommand().equals("Click Me!"))
  56.         {
  57.             numClicks++;
  58.             tf1.setText("Clicked " + numClicks + " time(s).");
  59.         }
  60.         else if (e.getActionCommand().equals("Clear"))
  61.         {
  62.             numClicks = 0;
  63.             tf1.setText("");
  64.             tf2.setText("");
  65.         }
  66.         else if (e.getActionCommand().equals("Copy"))
  67.         {
  68.             tf2.setText(""+tf1.getText());
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment