Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- public class ClickCounterGUI extends JFrame implements ActionListener
- {
- private JTextField tf1 = new JTextField("Clicked 0 time(s).", 15);
- private JTextField tf2 = new JTextField("Copied text here.", 15);
- private JButton b1;
- private JButton b2;
- private JButton b3;
- private int numClicks = 0;
- private String b1Text = "Click the button to increment the counter.";
- private String b2Text = "Clear contents of both text fields.";
- private String b3Text = "Copy contents of first text field to second text field.";
- ClickCounterGUI()
- {
- b1 = new JButton("Click Me!");
- b2 = new JButton("Clear");
- b3 = new JButton("Copy");
- b1.addActionListener(this);
- b2.addActionListener(this);
- b3.addActionListener(this);
- b1.setToolTipText(b1Text);
- b2.setToolTipText(b2Text);
- b3.setToolTipText(b3Text);
- add(b1);
- add(b2);
- add(b3);
- add(tf1);
- add(tf2);
- setSize(250, 400);
- setLayout(new GridLayout(5, 0));
- setTitle("Click Counter GUI");
- tf1.setBackground(Color.PINK);
- tf1.setForeground(Color.BLACK);
- tf2.setBackground(Color.MAGENTA);
- tf2.setForeground(Color.BLACK);
- tf1.setEditable(false);
- setResizable(false);
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- setVisible(true);
- }
- public void actionPerformed(ActionEvent e)
- {
- if (e.getActionCommand().equals("Click Me!"))
- {
- numClicks++;
- tf1.setText("Clicked " + numClicks + " time(s).");
- }
- else if (e.getActionCommand().equals("Clear"))
- {
- numClicks = 0;
- tf1.setText("");
- tf2.setText("");
- }
- else if (e.getActionCommand().equals("Copy"))
- {
- tf2.setText(""+tf1.getText());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment