Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JFrame implements ComponentListener, ActionListener {
    JButton ok = new JButton("OK");
    Dialog dialog = new Dialog();

    public Main() {
        ok.addActionListener(this);
        JPanel panel = new JPanel();
        panel.add(ok);

        getContentPane().add(panel, "South");
        addComponentListener(this);
        setVisible(true);
        setSize(new Dimension(200, 200));
        validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Main();
    }

    public void actionPerformed(ActionEvent ae) {
        dialog.setVisible(true);
    }

    @Override
    public void componentHidden(ComponentEvent arg0) {
    }

    @Override
    public void componentMoved(ComponentEvent arg0) {
        int x = this.getX() + this.getWidth();
        int y = this.getY();

        dialog.setDialogLocation(x, y);
    }

    @Override
    public void componentResized(ComponentEvent arg0) {
    }

    @Override
    public void componentShown(ComponentEvent arg0) {
    }
}

class Dialog extends JDialog {
    JButton done = new JButton("Done");

    public Dialog() {
        this.add(done);
        this.setSize(new Dimension(400, 200));
    }

    public void setDialogLocation(int x, int y) {
        this.setLocation(x, y);
    }
}