Example usage for javax.swing JFrame getSize

List of usage examples for javax.swing JFrame getSize

Introduction

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

Prototype

public Dimension getSize() 

Source Link

Document

Returns the size of this component in the form of a Dimension object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    JFrame window = new JFrame();
    window.setSize(300, 300);//w  w w.j  av  a  2 s . c om

    int w = window.getSize().width;
    int h = window.getSize().height;
    int x = (dim.width - w) / 2;
    int y = (dim.height - h) / 2;

    window.setLocation(x, y);

    window.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());
            }/*w  ww.  j av a 2  s.c  o  m*/
            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());
            }/*from  ww w .j a  v  a  2 s .  c  o  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();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new TestPane());
    frame.pack();//  ww w.  j  a va  2 s  . c o m
    frame.setVisible(true);
    System.out.println("Frame size = " + frame.getSize());
}

From source file:Main.java

public static void main(String[] args) {
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Rectangle bounds = env.getMaximumWindowBounds();
    System.out.println("Screen Bounds: " + bounds);

    GraphicsDevice screen = env.getDefaultScreenDevice();
    GraphicsConfiguration config = screen.getDefaultConfiguration();
    System.out.println("Screen Size  : " + config.getBounds());

    JFrame frame = new JFrame("Frame Info");
    System.out.println("Frame Insets : " + frame.getInsets());

    frame.setSize(200, 200);//w  w w.  j ava 2s.  com
    System.out.println("Frame Insets : " + frame.getInsets());
    frame.setVisible(true);

    System.out.println("Frame Size   : " + frame.getSize());
    System.out.println("Frame Insets : " + frame.getInsets());
    System.out.println("Content Size : " + frame.getContentPane().getSize());
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TestPane center = new TestPane(100, 100);
    frame.add(center);/*from ww  w.j  a v a  2 s.c  o m*/
    frame.add(new TestPane(100, 50), BorderLayout.NORTH);
    frame.add(new TestPane(100, 50), BorderLayout.SOUTH);
    frame.add(new TestPane(50, 100), BorderLayout.EAST);
    frame.add(new TestPane(50, 100), BorderLayout.WEST);

    System.out.println("Size beofre pack = " + frame.getSize() + "; " + center.getSize());

    frame.pack();

    System.out.println("Size after pack = " + frame.getSize() + "; " + center.getSize());

    frame.setLocationRelativeTo(null);
    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 {//from  w ww.j a v a2 s .  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");
}

From source file:MouseDrag.java

public static void main(String[] av) {
    JFrame f = new JFrame("Mouse Dragger");
    Container cp = f.getContentPane();

    if (av.length < 1) {
        System.err.println("Usage: MouseDrag imagefile");
        System.exit(1);/*from ww  w .ja v a2 s. co m*/
    }
    Image im = Toolkit.getDefaultToolkit().getImage(av[0]);

    // create a MouseDrag object
    MouseDrag j = new MouseDrag(im);

    cp.setLayout(new BorderLayout());
    cp.add(BorderLayout.NORTH, new Label("Hello, and welcome to the world of Java"));
    cp.add(BorderLayout.CENTER, j);
    cp.add(BorderLayout.SOUTH, status = new Label());
    status.setSize(f.getSize().width, status.getSize().height);
    f.pack();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:org.jfree.chart.demo.CompassDemo.java

/**
 * Entry point for the demo application.
 *
 * @param args  ignored.// www  .  java2s  .  c  o m
 */
public static void main(final String[] args) {

    final CompassDemo panel = new CompassDemo();

    final JFrame frame = new JFrame();
    frame.getContentPane().setLayout(new BorderLayout(5, 5));
    frame.setDefaultCloseOperation(3);
    frame.setTitle("Compass Demo");
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    frame.setSize(700, 400);
    final Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
}

From source file:MouseDragClip.java

public static void main(String[] av) {
    JFrame f = new JFrame("Mouse Dragger");
    Container cp = f.getContentPane();

    if (av.length < 1) {
        System.err.println("Usage: MouseDragClip imagefile");
        System.exit(1);//w  w  w  . j  a v  a2  s .  c  om
    }
    Image im = Toolkit.getDefaultToolkit().getImage(av[0]);

    // create a MouseDragClip object
    MouseDragClip j = new MouseDragClip(im);

    cp.setLayout(new BorderLayout());
    cp.add(BorderLayout.NORTH, new Label("Hello, and welcome to the world of Java"));
    cp.add(BorderLayout.CENTER, j);
    cp.add(BorderLayout.SOUTH, status = new Label());
    status.setSize(f.getSize().width, status.getSize().height);
    f.pack();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}