Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package conrol.in.action;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- public class ControlInAction {
- public static void main(String[] args) {
- ABC j = new ABC();
- j.setVisible(true);
- j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- j.setBounds(400, 300, 500, 500);
- j.setTitle("Java SWING Examples");
- }
- }
- class ABC extends JFrame {
- private Container c;
- private JLabel j1, j2;
- private JTextField tf1;
- private JButton b1, b2, b3;
- ABC() {
- c = this.getContentPane();
- c.setLayout(null);
- c.setBackground(Color.WHITE);
- j1 = new JLabel("Control in action in JButton");
- j1.setBounds(150, 50, 200, 50);
- c.add(j1);
- tf1 = new JTextField();
- tf1.setBounds(150, 300, 150, 20);
- c.add(tf1);
- b1 = new JButton("OK");
- b1.setBounds(100, 150, 60, 20);
- c.add(b1);
- b2 = new JButton("Submit");
- b2.setBounds(170, 150, 90, 20);
- c.add(b2);
- b3 = new JButton("Cancle");
- b3.setBounds(280, 150, 80, 20);
- c.add(b3);
- Handler h = new Handler();
- b1.addActionListener(h);
- b2.addActionListener(h);
- b3.addActionListener(h);
- }
- class Handler implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == b1) {
- tf1.setText("OK button clicked");
- } else if (e.getSource() == b2) {
- tf1.setText("Submit button clicked");
- } else if (e.getSource() == b3) {
- tf1.setText("Cancle button clicked");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment