COP 2800 (Java Programming I) Homework Assignment #7

 

Due: by the start of class on the date shown on the syllabus

Description:

Answer the following questions as briefly (but completely) as possible:

  1. How do you create a frame (AWT)?  How do you set the size for a frame (AWT)?  What would happen if the statements frame.setSize(400, 300) and frame.setVisibile(true) (lines 9 and 11) were swapped in the following program?
    import javax.swing.*;
    
    public class MyFrameWithComponents {
       public static void main ( String [] args ) {
          JFrame frame = new JFrame( "MyFrameWithComponents" );
          // Add a button to the frame:
          JButton okayBtn = new JButton( "OK" );
          frame.add( okayBtn );
          frame.setSize( 400, 300 );
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          frame.setLocationRelativeTo( null ); // Center frame on screen
          frame.setVisible( true );
       }
    }
    1. Why do you need to use layout managers?
    2. What is the default layout manager for a Frame or JFrame?
    3. How do you add a component to a Frame or JFrame?
  2. The following program is supposed to display a button in a frame, but the button doesn't display.  What is the problem?  How would you fix that?
    import javax.swing.*;
    
    public class Test extends JFrame {
       public Test () {
          add( new JButton( "OK" ) );
       }
       
       public static void main ( String [] args ) {
          JFrame frame = new JFrame();
          frame.setSize( 100, 200 );
          frame.setVisible( true );
       }
    }
    1. What is the default layout manager for a JPanel?
    2. How do you add a component to a JPanel?
    1. How do you create a Color object?
    2. What is wrong with the following code to create a Color?
          new Color(400, 200, 300)
    3. Which of these two colors is darker?
          new Color(10, 0, 0)
          new Color(200, 0, 0)
  3. How do you create a Font object with the font name Courier, size 20 points, and style bold?
  4. What happens if you add a button to a container several times, as shown below?  Is this a syntax error, logic error, runtime error, or more than one of these?
    JButton btn = new JButton( "click me" );
    JPanel panel = new JPanel();
    panel.add( btn );
    panel.add( btn );
    panel.add( btn );
  5. Will the following code display three buttons (assume the image is found and is correct)?  Will the buttons display the same icon?
    import javax.swing.*;
    import java.awt.*;
    
    public class Test extends JFrame {
       public static void main ( String [] args ) {
          JFrame frame = new Test();
          frame.setTitle( "Button Icons" );
          frame.setSize( 200, 100 );
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          frame.setVisible( true );
       }
       
       public Test () {
          ImageIcon usIcon = new ImageIcon( "images/us.gif" );
          JButton jbt1 = new JButton( usIcon );
          JButton jbt2 = new JButton( usIcon );
          
          JPanel p1 = new JPanel();
            p1.add( jbt1 );
          
          JPanel p2 = new JPanel();
            p2.add( jbt2 );
                
          JPanel p3 = new JPanel();
            p3.add( jbt1 );
          
          add( p1, BorderLayout.NORTH );
          add( p2, BorderLayout.SOUTH );
          add( p3, BorderLayout.CENTER );
       }
    }
  6. Given a JLabel object jlblMap, write the Java statements needed to set the label’s foreground to red, background to yellow, mnemonic to M, tool tip text to “Map Image”, horizontal alignment to RIGHT, vertical alignment to BOTTOM, horizontal text position to LEFT, vertical text position to TOP, and the icon text gap to 5.
  7. Can a button (either AWT or swing) fire a MouseEvent?  Can a button fire a KeyEvent?  Can a button fire an ActionEvent?
  8. What method do you use to get the timestamp of an ActionEvent?
  9. Why does the ActionListener interface have no listener interface adapter (that is, no ActionAdapter)?
  10. How do you set focus on a component so it can listen for key events?
  11. How do you create a scrollable text area using swing?
  12. How do you specify a line wrap for a text area, in swing?  How do you specify wrapping on characters?  How do you specify wrapping on words?

To be turned in:

Email your homework assignment, by copy-and-paste (no attachments please), to (homework submission).  If possible use the “text” and not the “HTML” mode of your email program.  Please use the subject similar to “Java Programming I Homework Assignment #7 Submission”, so I can tell which emails are submitted homework assignments.

Homework assignments will not be returned.  Please do not send as attachments.  Refer to the Homework and the Submitting Assignments sections of your syllabus for more information.

Confused?  Send questions about the homework assignment to (homework questions).  Please use a subject similar to “Java Programming I Homework Assignment #7 Questions” so I can tell which emails are questions about assignment quiz (and not submissions).