Java Swing How to - Resize JFrame in Desktop Application








Question

We would like to know how to resize JFrame in Desktop Application.

Answer

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//from   w  ww  .j  a  va2  s . co  m
import javax.swing.JFrame;

public class Main extends JFrame implements MouseListener {
  public static void main(String[] args) {
    Main frame = new Main();
    frame.setVisible(true);
    frame.setSize(200, 200);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.addMouseListener(frame);
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    if (this.getExtendedState() == JFrame.MAXIMIZED_BOTH) {
      this.setExtendedState(JFrame.NORMAL);
    } else {
      this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
  }
  @Override
  public void mousePressed(MouseEvent e) {
  }
  @Override
  public void mouseReleased(MouseEvent e) {
  }
  @Override
  public void mouseEntered(MouseEvent e) {
  }

  @Override
  public void mouseExited(MouseEvent e) {
  }
}