20-CS-694 Advanced Programming Techniques Spring 2012

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

    Prev     Next     All lectures        Code    

Use Services Provided by Other Classes

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

class java_2Frame
   extends JFrame implements ActionListener {
   JTextArea text;
   JButton go;
   int i;

   public java_2Frame () {
      setLayout(new BorderLayout());
          new JScrollPane(text = new JTextArea()));
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.add(go = new JButton("Run it"));
      add("South", p);
      i = 1;

   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == go) {
         text.append(i+" ");
         if ((i++ % 10) == 0) text.append("\n");
         int p = text.getDocument().getLength();

public class java_2
   extends Applet implements ActionListener {
   JButton go;

   public void init () {
      setLayout(new FlowLayout());
      add(go = new JButton("Applet"));

   public void actionPerformed (ActionEvent evt) {
      java_2Frame jf = new java_2Frame();
 -  An attribute of object-oriented languages is the ability to inherit services from other classes. In Java this is handled by extends. In the code to the left class java_2Frame inherits the ability to display a JTextArea and JButton from the JFrame class.