20-CS-694 Advanced Programming Techniques Spring 2012
Layouts

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

       Previous     Next     Applet Source     All lectures

Set Maximum and Minimum Size

// Try resizing the window and moving the mouse over the widgets
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;

class MyPanel extends Panel implements MouseMotionListener {
   FST fst;

   public MyPanel(FST fst) {
      this.fst = fst;
      addMouseMotionListener(this);
      setPreferredSize(new Dimension(200,200));
   }

   public void mouseMoved (MouseEvent evt) {
      fst.t1.setText(String.valueOf(evt.getX()));
      fst.t2.setText(String.valueOf(evt.getY()));
   }

   public void mouseDragged (MouseEvent evt) {
      fst.t1.setText(String.valueOf(evt.getX()));
      fst.t2.setText(String.valueOf(evt.getY()));
   }
}

class FST extends JFrame {
   JTextField t1, t2;
   Canvas c;

   public FST () {
      setLayout(new FlowLayout());
      add(new MyPanel(this));
      Panel p = new Panel();
      p.setLayout(new GridLayout(4,1));
      p.add(new JButton("Three"));
      p.add(t1 = new JTextField());
      p.add(t2 = new JTextField());
      p.add(new JButton("Four"));
      p.setMinimumSize(new Dimension(100,100));
      p.setMaximumSize(new Dimension(100,100));
      p.setPreferredSize(new Dimension(100,100));
      add(p);
   }
}

public class fst extends Applet {
   FST f;
   
   public void init () {
      f = new FST();
      f.setSize(600,600);
      f.setVisible(true);
   }

   public void finalize () { f.dispose(); }
}