(c) Copyright 2003, 2004 by Daniel Berleant

 

    

Here is information on GUI components in the swing library

http://java.sun.com/docs/books/tutorial/uiswing/index.html

 

 

Frames and their Children

    A child of a frame is simply some GUI component on the frame

    A Frame can have many children

    A JFrame has

        only one child

        it is present automatically

        it is of class JRootPane

               A JRootPane contains a few things including a content pane object

                    A content pane holds GUI components

                    (There is also a glass pane, etc., see figure)

 

The following text describes this graphic.

(from http://java.sun.com/j2se/1.4.2/docs/api/index.html under "JRootPane")

 

Lesson: add buttons-n-stuff to a JFrame's content pane not directly to the JFrame!

 

 

 


 

This is the "same" program but with swing components instead of awt components:

 

import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
 
public class MyJGUI{
   static JButton B;
   static JFrame F;
   public static void main(String args[]){
      F=new JFrame("Hello, I am a Frame");
      F.setSize(700,500);
      F.setVisible(true);
      F.getContentPane().setBackground(Color.orange);
      //F.setBackground(Color.orange);

      //F.setBackground
      //  (new
      //     Color((float)0.99,
      //     (float)0.0,(float)0.99));
      Font myFont;
      myFont=
new Font("TimesRoman",
                      Font.PLAIN, 60);
      JLabel L=new
                JLabel
                  ("Type password in box below");
      L.setFont(myFont);
      F.getContentPane().add("North",L);
      F.show();
       
      JPasswordField T;
      //JTextField T;
      T=new JPasswordField("Password Here");
      //T=new JTextField("Password here");
      // T=new JTextField( );
      // T=new JTextField(20);
      T.setFont(myFont);
      F.getContentPane().add("West",T);
      F.show();
 
      JLabel L2=new JLabel("Now click button:");
      L2.setFont(myFont);
      F.getContentPane().add("Center",L2);
      F.show();
 
      B=new JButton("Click this button");
      B.setFont(new Font("Helvetica",
                          Font.ITALIC, 60));
      B.setBackground(Color.cyan);
      B.setForeground(Color.red);
      F.getContentPane().add("South",B);
      F.show();
  
      F.setSize(1100,500);
      F.show();
 
      JPanel onRight;
      onRight=new JPanel( );
      onRight.setLayout(new GridLayout(3,1));
      JButton B1, B2, B3;
      B1=new JButton("Ok");
      B1.setFont(myFont);
      B2=new JButton("Cancel");
      B2.setFont(myFont);
      B3=new JButton("Menu");
      B3.setFont(myFont);
      onRight.add(B1);
      onRight.add(B2);
      onRight.add(B3);
      F.getContentPane().add("East",onRight);
      F.show();
 
      JMenu M;
      M=new JMenu("Options");
      M.setFont(myFont);
      M.add("save");
      M.add("print");
      M.add("help");
      M.add("status");
 
      JMenu N;
      N=new JMenu(" More Options");
      N.setFont(myFont);
      N.add("stop");
      N.add("go");
      N.add("go backward");
      N.add("click here for ice cream");
 
      JMenuBar MB;
      MB=new JMenuBar( );
      MB.add(M);
      MB.add(N);
      F.setJMenuBar(MB);
      F.show();
 
      JDialog D;
      D=new JDialog(F, "My dialog box");
      //D.getContentPane().setBackground(Color.green);
      //D.setBackground(Color.green);
      // D=Dialog( );
      D.show();
 
      JCheckBox C1, C2, C3;
      C1=new JCheckBox(
                "Here is a Checkbox in" +
                " the Dialog object..."
             );
      C2=new JCheckBox("Hello");
      C3=new JCheckBox("Goodbye");
      C1.setFont(myFont);
      C2.setFont(myFont);
      C3.setFont(myFont);
      D.getContentPane().setLayout(new GridLayout(3,1));
      D.getContentPane().add(C1);
      D.getContentPane().add(C2);
      D.getContentPane().add(C3);
      D.show();
 
      D.setSize(100,500);
      D.show();
 
      doSomething S=new doSomething();
      B.addActionListener(S);
      T.addActionListener(S);
   }
}
 
class doSomething implements ActionListener{
   public void actionPerformed(ActionEvent e){
      System.out.println(e.getActionCommand());
  }
}