Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://www.facebook.com/groups/1403852566495675/permalink/2397128547168067/
- package com.sample;
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.Frame;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class ActionEvent2 {
- private static Frame frame = new Frame();
- private static Action action = new Action();
- private static Button btn1 = new Button("Blue");
- private static Button btn2 = new Button("Green");
- private static Button btn3 = new Button("Red");
- private static Button btn4 = new Button("Exit");
- public static void main(String args[]) {
- btn1.addActionListener(action);
- btn2.addActionListener(action);
- btn3.addActionListener(action);
- btn4.addActionListener(action);
- frame.setTitle("Action Event");
- frame.setLayout(new FlowLayout(FlowLayout.LEFT));
- frame.setSize(400, 250);
- frame.setLocation(350, 250);
- frame.add(btn1);
- frame.add(btn2);
- frame.add(btn3);
- frame.add(btn4);
- frame.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- frame.setVisible(true);
- }
- private static class Action implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- Button button = (Button) e.getSource();
- if (button == btn1) {
- frame.setBackground(Color.blue);
- } else if (button == btn2) {
- frame.setBackground(Color.green);
- } else if (button == btn3) {
- frame.setBackground(Color.red);
- } else {
- System.exit(0);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement