public class MinSizeFrame extends JFrame implements ComponentListener {
static final int WIDTH = 400;
static final int HEIGHT = 400;
static final int MIN_WIDTH = 300;
static final int MIN_HEIGHT = 300;
public MinSizeFrame() {
setSize(WIDTH, HEIGHT);
addComponentListener(this);
}
public void componentResized(ComponentEvent e) {
int width = getWidth();
int height = getHeight();
//we check if either the width
//or the height are below minimum
boolean resize = false;
if (width < MIN_WIDTH) {
resize = true;
width = MIN_WIDTH;
}
if (height < MIN_HEIGHT) {
resize = true;
height = MIN_HEIGHT;
}
if (resize) {
setSize(width, height);
}
}
public void componentMoved(ComponentEvent e) {
}
public void componentShown(ComponentEvent e) {
}
public void componentHidden(ComponentEvent e) {
}
public static void main(String args[]) {
MinSizeFrame f = new MinSizeFrame();
f.setVisible(true);
}
}