Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {
    private JButton buzz = new JButton("BUZZ ME!!");

    public void prepareGUI() {
        buzz.addActionListener(new BuzzActionListener(this));
        setSize(300, 200);
        getContentPane().add(buzz, BorderLayout.NORTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String st[]) {
        Main bFrame = new Main();
        bFrame.prepareGUI();
        bFrame.setVisible(true);

    }
}

class BuzzActionListener implements ActionListener {
    JFrame frame;
    Point currLocation;
    int iDisplaceXBy = 5;
    int iDisplaceYBy = -10;

    public BuzzActionListener(JFrame frame) {
        this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        currLocation = frame.getLocationOnScreen();

        Point position1 = new Point(currLocation.x + iDisplaceXBy, currLocation.y + iDisplaceYBy);
        Point position2 = new Point(currLocation.x - iDisplaceXBy, currLocation.y - iDisplaceYBy);
        for (int i = 0; i < 20; i++) {
            frame.setLocation(position1);
            frame.setLocation(position2);
        }
        frame.setLocation(currLocation);
    }
}