4.9 Week 9 - Collections  4 Operator Precedence and Associativity4.7 Week 7 - Java Object Terminology 3 - awt & swing 4.8 Week 8 - JButtons, JFrames, ...

4.8 Week 8 - JButtons, JFrames, ...

4.8.1 Reading and Web Sites

  1. Look over Java Lobby
  2. Read van der Linden Chapter 19, GUI Basics and Event Handling
  3. Look over van der Linden Chapter 21, JFC and the Swing Package

4.8.2 Notes

  1. JLabel Example of JLabel:
    /* Louis Taber   
       Pima Community College   
       3/2/2003  
       Display a .gif in a JLabel            */
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class GIF
    {
    static  Container  cp;  // Content Pane
    static  ExitButton eb;  
    
    public static void main(String[] args)
      {
      //  Get a JFrame
      JFrame aWindow = new JFrame( "apache_pb.gif");
      // Position and size JFrame
      aWindow.setBounds( 50, 100, 400, 100);
      //  Set Close opereation
      aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // Get Content Pane
      cp = aWindow.getContentPane();
    
      // Create Image Icon  
      ImageIcon gif = new ImageIcon("apache_pb.gif");
      // Add it to a JLabel
      JLabel g1 = new JLabel("Apache", gif, JLabel.CENTER);
      // Add text tip -- shows id pointer stays on JLabel
      g1.setToolTipText("http://www.apache.org/");
      cp.add( g1 );
      // Display results
      aWindow.setVisible(true);
      }
    }
    
    
    
  2. JButton Example of JButton:
    /* Louis Taber   
       Pima Community College   
       3/2/2003  
       Use a JButton            */
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class ExitButton
    {
    static  Container  cp;  // Content Pane
    static  ExitButton eb;  
    
    public static void main(String[] args)
      {
      //  Get a JFrame
      JFrame aWindow = new JFrame( "Exit JButton");
      // Position and size JFrame
      aWindow.setBounds( 50, 100, 300, 100);
      //  Set Close opereation
      aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // Get Content Pane
      cp = aWindow.getContentPane();
      // Call ComponentE to add in Exit Button
      cp.add( new ComponentE() );
      // Set background color to red
      cp.setBackground(Color.red);
      // Display the JFrame
      aWindow.setVisible(true);
      }
    }
    
    class ComponentE extends JComponent
      implements ActionListener
    {
    JButton jb;
    
    public ComponentE()
      {
      // get JButton
      jb = new JButton("EXIT");
      // Set up the colors
      jb.setBackground(Color.blue);
      jb.setForeground(Color.white);
      //  Add JButton to Content Pane  
      add( jb );
      jb.addActionListener(this);
      setLayout( new FlowLayout() );  //  ????
      }
    
    public void actionPerformed( ActionEvent e)
      {
      System.out.println("Exit NOW");
      System.exit(0);
      }
    
    }
    
  3. JTextField Example of JTextField:
    /* Louis Taber   
       Pima Community College   
       3/2/2003  
       Use a JTextField            */
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class JTextFieldExample
    {
    static  Container  cp;  // Content Pane
    static  ExitButton eb;  
    
    public static void main(String[] args)
      {
      //  Get a JFrame
      JFrame aWindow = new JFrame( "Exit JButton");
      // Position and size JFrame
      aWindow.setBounds( 50, 100, 500, 100);
      //  Set Close opereation
      aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // Get Content Pane
      cp = aWindow.getContentPane();
      // Call ComponentTF to add a Text Field
      cp.add( new ComponentTF() );
      // Set background color to green
      cp.setBackground(Color.green);
      // Display the JFrame
      aWindow.setVisible(true);
      }
    }
    
    class ComponentTF extends JComponent
      implements ActionListener,
                 KeyListener
    {
    JLabel jl;
    JTextField jt;
    
    public ComponentTF()
      {
      // get JLabel
      jl = new JLabel("What do you want? ");
      // Get text field - 30 Characters wide 
      jt = new JTextField(30);
      //  Add JButton to Content Pane  
      add( jl );
    
      // Set background color to orange
      jt.setBackground(Color.orange);
      add( jt );
      jt.addActionListener(this);
      jt.addKeyListener(this);
      setLayout( new FlowLayout() );  //  ????
      }
    
    public void actionPerformed( ActionEvent e)
      {
      System.out.println("You typed: " + e.getActionCommand() );
      System.exit(0);
      }
    
    
     /* Print character and modifiers. */
      public void keyTyped(KeyEvent e) 
        {
        char c = e.getKeyChar();
        int modifiers = e.getModifiers();
        System.out.println("Character \"" + c + "\" was typed.  " + 
                           "Modifiers: " + modifiers ); 
        }
    
      /* Part of KeyListener interface  */
      public void keyPressed(KeyEvent e) { }
      public void keyReleased(KeyEvent e) { }
    
    }
    
  4. JCheckBox Example of JCheckBox
    /* Louis Taber   
       Pima Community College   
       3/2/2003  
       Use a JButton            */
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class CheckBox
    {
    static  Container  cp;  // Content Pane
    
    public static void main(String[] args)
      {
      //  Get a JFrame
      JFrame aWindow = new JFrame( "Weather");
      // Position and size JFrame
      aWindow.setBounds( 50, 100, 300, 100);
      //  Set Close opereation
      aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // Get Content Pane
      cp = aWindow.getContentPane();
      // Call ComponentCheck to add CheckBoxes
      cp.add( new CheckBoxes() );
      // Set background color to red
      cp.setBackground(Color.red);
      // Display the JFrame
      aWindow.setVisible(true);
      }
    }
    
    class  CheckBoxes extends JComponent
      implements ItemListener
    {
    JCheckBox rain, snow;
    
    public CheckBoxes()
      {
      String [] weather_types = 
        { 
        "Rain", "Snow", "Sleet", "Hail",
        "Clear", "Fog", "Ooblick" 
        };
    
      for( int k=0; k<weather_types.length; k++)
        {
        // get JCheckBox
        JCheckBox wt = new JCheckBox(weather_types[k]); 
        // add obkect to JComponent
        add(wt);
        // Tie it into Listener
        wt.addItemListener(this);
        }
    
      setLayout( new FlowLayout() );  //  ????
      }
    
    public void itemStateChanged( ItemEvent e)
      {
      Object obj =  e.getItem();
      if( obj instanceof JCheckBox)
        {
        JCheckBox cb = (JCheckBox)obj; 
        System.out.print( cb.getText() );
        System.out.println("  " + ((e.getStateChange() == e.SELECTED) 
                            ? "SELECTED" : "DE-SELECTED") );
        }
      }
    
    }
    

4.8.3 Lab Assignment JLabel, JButton, JTextField, and JCheckBox

Write a JavaTM program that uses JLabel, JButton, JTextField, and JCheckBox with their event handlers.

  1. Create a JFrame
  2. In the content window, put up an image.
  3. Put up an "exit" JButton, which when "pushed, exits the program.
  4. Put up a JTextField, that adds another JCheckBox when the return is entered.
  5. Change the intensity of the background color in the ContentPane whenever a JCheckBox is selected. Use a minimum of 4 intensities.
  6. Change the hue of the background color in the ContentPane whenever a JCheckBox is de-selected. Use a minimum of 3 hues.
Please turn in a listing. Also show me your program running.
Instructor: ltaber@pima.edu ** My new Home at GeoApps in Tucson ** The Pima College Site

4.9 Week 9 - Collections  4 Operator Precedence and Associativity4.7 Week 7 - Java Object Terminology 3 - awt & swing 4.8 Week 8 - JButtons, JFrames, ...