Example usage for javax.swing JFrame addComponentListener

List of usage examples for javax.swing JFrame addComponentListener

Introduction

In this page you can find the example usage for javax.swing JFrame addComponentListener.

Prototype

public synchronized void addComponentListener(ComponentListener l) 

Source Link

Document

Adds the specified component listener to receive component events from this component.

Usage

From source file:MoveAdapter.java

public static void main(String[] args) {
    JFrame f = new JFrame();

    f.addComponentListener(new MoveAdapter());

    f.setSize(310, 200);/*w  w w.j a v  a2  s  .c  o  m*/
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("");
    frame.pack();/*from  w w w.  ja v  a 2s.co m*/

    frame.addComponentListener(new Main());
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setMaximumSize(new Dimension(350, 250));
    frame.addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent evt) {
            Dimension size = frame.getSize();
            Dimension max = frame.getMaximumSize();
            if (size.getWidth() > max.getWidth()) {
                frame.setSize((int) max.getWidth(), (int) size.getHeight());
            }//ww  w  . j  a v a  2s. com
            if (size.getHeight() > max.getHeight()) {
                frame.setSize((int) size.getWidth(), (int) max.getHeight());
            }
        }
    });
    frame.setSize(200, 100);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setMinimumSize(new Dimension(200, 200));
    frame.addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent evt) {
            Dimension size = frame.getSize();
            Dimension min = frame.getMinimumSize();
            if (size.getWidth() < min.getWidth()) {
                frame.setSize((int) min.getWidth(), (int) size.getHeight());
            }/*  www . j  av a2s  .co m*/
            if (size.getHeight() < min.getHeight()) {
                frame.setSize((int) size.getWidth(), (int) min.getHeight());
            }
        }
    });
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Window cannot be moved to hide part of it");
    frame.setSize(300, 300);//from  w w  w  .  j a va2 s .  c  o  m

    frame.addComponentListener(new Main());
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JDialog dialog = new JDialog(frame, false);
    dialog.setSize(200, 50);//  www . j  ava  2s .c  o  m
    frame.addComponentListener(new ComponentAdapter() {
        Point lastLocation;

        @Override
        public void componentMoved(ComponentEvent e) {
            if (lastLocation == null && frame.isVisible()) {
                lastLocation = frame.getLocation();
            } else {
                Point newLocation = frame.getLocation();
                int dx = newLocation.x - lastLocation.x;
                int dy = newLocation.y - lastLocation.y;
                dialog.setLocation(dialog.getX() + dx, dialog.getY() + dy);
                lastLocation = newLocation;
            }
        }
    });
    frame.setSize(400, 200);
    frame.setVisible(true);
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("");
    JLabel label = new JLabel("Move this window's title bar to demonstrate screen edge snapping.");
    frame.getContentPane().add(label);//  ww  w .  jav a  2 s .  com
    frame.pack();

    frame.addComponentListener(new Main());
    frame.setVisible(true);
}

From source file:WindowSnapper.java

public static void main(String[] args) {
    JFrame frame = new JFrame("");
    JLabel label = new JLabel("Move this window's title bar to demonstrate screen edge snapping.");
    frame.getContentPane().add(label);//w ww .java2s  .c  o  m
    frame.pack();

    frame.addComponentListener(new WindowSnapper());
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
    statusBar.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(4, 4, 4, 4)));
    final JLabel status = new JLabel();
    statusBar.add(status);//from w  ww.j a  v  a2s.  co  m

    JLabel content = new JLabel("Content in the middle");
    content.setHorizontalAlignment(JLabel.CENTER);

    final JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(content);
    frame.add(statusBar, BorderLayout.SOUTH);

    frame.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            status.setText(frame.getWidth() + "x" + frame.getHeight());
        }
    });

    frame.setBounds(20, 20, 200, 200);
    frame.setVisible(true);
}

From source file:edu.gmu.isa681.client.Main.java

public static void main(String[] args) {
    log.info("Setting look and feel...");

    try {/*w  w  w .  j av a2s .  com*/
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }

    } catch (Exception ex1) {
        log.warn(ex1.getMessage(), ex1);
        log.warn("Nimbus is not available.");
        log.warn("Switching to system look and feel");
        log.warn("Some GUI discrepancies may occur!");

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex2) {
            log.error(ex2.getMessage(), ex2);
            log.error("Could not setup a look and feel.");
            System.exit(1);
        }
    }

    log.info("Initializing GUI...");

    final JFrame frame = new JFrame();
    frame.setTitle("GoForward");
    frame.setBackground(new Color(0, 100, 0));
    UIManager.put("nimbusBase", new Color(0, 100, 0));
    //UIManager.put("nimbusBlueGrey", new Color(0, 100, 0));
    UIManager.put("control", new Color(0, 100, 0));

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    frame.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            frame.setPreferredSize(frame.getSize());
        }
    });

    Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    if (dim.width < 1366) {
        frame.setPreferredSize(new Dimension(800, 600));
    } else {
        frame.setPreferredSize(new Dimension(1200, 700));
    }

    //frame.setResizable(false);
    frame.setLocationByPlatform(true);
    frame.pack();

    Client client = new Client("localhost", Constants.SERVER_PORT);
    Controller controller = new Controller(client, frame);
    controller.applicationStarted();

    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    log.info("Started");
}