20-CS-694 Advanced Programming Techniques Spring 2012
Questions

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

       Previous     Next     Applet Source      JFrame Source     All lectures

Variable Borders

import java.awt.*;
import javax.swing.*;
import java.applet.*;

public class Borders extends JFrame {
   SpringLayout layout;
   JButton b1, b2;
   int hgt = 40;
   String WEST = SpringLayout.WEST;
   String EAST = SpringLayout.EAST;
   String NORTH = SpringLayout.NORTH;
   String SOUTH = SpringLayout.SOUTH;

   public Borders () {
      setLayout(layout = new SpringLayout());
      add(b1 = new JButton("Top Button"));
      add(b2 = new JButton("Bottom Button"));
   
      layout.putConstraint(WEST, b1, 10, WEST, this);
      layout.putConstraint(WEST, b2, 10, WEST, this);
      layout.putConstraint(NORTH, b1, 20, NORTH, this);
      layout.putConstraint(NORTH, b2, hgt, SOUTH, b1);
   }

   public void paint (Graphics g) {
      Dimension d = getSize();
      System.out.println(d.width+" "+d.height);
      layout.putConstraint(NORTH, b2, d.height/10, SOUTH, b1);
   }
}