20-CS-694 Advanced Programming Techniques Spring 2012
Applets

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

       Previous     Next     Applet Source     All lectures

The Panels are now (floating) Frames

Same as previous applet except the JPanels are implemented as JFrames and are floating
apart from the applet. The dimensions of the JFrames can be controlled independently of
the dimensions of the applet by using methods setSize and setVisible.

import java.awt.*;          // For the BorderLayout, GridLayout class
import java.awt.event.*;    // For ActionEvent, ActionListener class
import javax.swing.*;       // For the JButton, JTextField, JTextArea class
import java.applet.*;       // For the Applet class
   
class TopPanelF extends JFrame implements ActionListener {
   JButton a,b,c;
   BotPanelF bp;
   Prog7Frame prog;

   public TopPanelF (BotPanelF bp, Prog7Frame prog) {
      this.bp = bp;
      this.prog = prog;
      setLayout(new GridLayout(1,3));
      add (a = new JButton("One,One"));
      add (b = new JButton("One,Two"));
      add (c = new JButton("Two,One"));
      a.addActionListener(this);
      b.addActionListener(this);
      c.addActionListener(this);
   }

   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == a) {
         bp.d.setText("Now we are cooking");
         prog.text.append("Now we are cooking\n");
      } else if (evt.getSource() == b) {
         bp.e.setText("Wonderful!");
         prog.text.append("Wonderful!\n");
      } else {
         bp.f.setText(bp.d.getText());
         prog.text.append("I do not know what to do\n");
      }
   }
}

class BotPanelF extends JFrame {
   JTextField d,e,f;
    public BotPanelF () {
      setLayout(new GridLayout(1,3));
      add (d = new JTextField("Nothing Yet"));
      add (e = new JTextField("Nothing Yet"));
      add (f = new JTextField("Nothing Yet"));
      e.setHorizontalAlignment(JTextField.CENTER);
   }
}

class Prog7Frame extends JFrame {
   BotPanelF bp;
   TopPanelF tp;
   JTextArea text;
	
   public Prog7Frame () {
      setLayout(new BorderLayout());
      bp = new BotPanelF();
      bp.setSize(400,40);
      bp.setVisible(true);
      tp = new TopPanelF(bp, this);
      tp.setSize(400,40);
      tp.setVisible(true);
      add("Center", new JScrollPane(text = new JTextArea()));
   }
}

public class Prog7 extends Applet {
   public void init () {
      Prog7Frame pf = new Prog7Frame();
      pf.setSize(400,400);
      pf.setVisible(true);
   }
}