Published: November 8, 2004
Revised: January 9, 2005
By Richard G. Baldwin
File: Inew2338Sg005.htm
These lessons provide questions, answers, and explanations designed to help you to understand the essential Java features covered by the Advanced Java course.
The textbook for this course is Advanced Java Internet Applications, Second Edition by Art Gittleman, ISBN 1-57676-096-0. This study guide is for Chapter 5 in the textbook.
In addition to the textbook, I recommend that you study my extensive collection of online Java tutorial lessons. Those tutorial lessons are published at http://www.dickbaldwin.com.
For this particular study guide, you should
study lessons 108 through 120, lesson 124, lessons 190 through 212 and lessons 1000 through 1068 at the URL given above. These lessons
provide a great deal of information about the use of Swing that goes far beyond
the information contained in the textbook.
1. Which of the following comes closest to representing the output that would be produced by the following program when the program is executed using Java SDK 1.4.2 or a later version of the SDK under a Windows operating system?
import javax.swing.UIManager; class Inew2338_070{ public static void main(String[] args){ UIManager.LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels(); for(int cnt = 0;cnt < laf.length;cnt ++){ String lafStr = laf[cnt].toString(); System.out.println(lafStr.substring( lafStr.lastIndexOf(".") + 1, lafStr.length() - 1)); }//end for loop }//end main }//end class Inew2338_070 |
2. A Java application and three GUI images are
provided below. Which image best matches the GUI that would be produced by this application
when run under a Windows operating system, A, B, or C? Assume that the
Java SDK is version 1.4.2 or later.
import javax.swing.*; import java.awt.*; class Inew2338_072a extends JFrame{ public static void main(String[] args){ new Inew2338_072a(); }//end main Inew2338_072a(){ System.out.println(UIManager. getLookAndFeel()); getContentPane().setLayout(new FlowLayout()); getContentPane().add(new JButton("OK")); setSize(275,100); setTitle("Copyright 2004, R.G.Baldwin"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setVisible(true); }//end constructor }//end class Inew2338_072a |
Choice A |
Choice B |
Choice C |
3. A Java application and three GUI images are
provided below. Which image best matches the GUI that would be produced by this application
when it is run under Windows, A, B, or C? Assume that the Java SDK is
version 1.4.2 or later.
class Inew2338_074 extends JFrame{ public static void main(String[] args){ new Inew2338_074(); }//end main Inew2338_074(){ try{ UIManager.setLookAndFeel(UIManager. getSystemLookAndFeelClassName()); }catch(Exception e){ e.printStackTrace(); }//end catch getContentPane().setLayout(new FlowLayout()); getContentPane().add(new JButton("OK")); setSize(275,100); setTitle("Copyright 2004, R.G.Baldwin"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setVisible(true); }//end constructor }//end class Inew2338_074 |
Choice A |
Choice B |
Choice C |
4. The first box below shows a Java application. The second box shows an image contained in a file in the current directory named inew2338sg005g.gif. The third box shows a GUI.
True or false. The Java application is capable of producing the GUI shown.
/*File Inew2338_076b.java Copyright 2004, R.G.Baldwin Tested using SDK 1.4.2 and WinXP ************************************************/ import javax.swing.*; import java.awt.*; class Inew2338_076b extends JFrame{ public static void main(String[] args){ new Inew2338_076b(); }//end main Inew2338_076b(){ getContentPane().setLayout(new FlowLayout()); JButton theButton = new JButton("OK"); theButton.setIcon("inew2338sg005g.gif"); getContentPane().add(theButton); setSize(275,100); setTitle("Copyright 2004, R.G.Baldwin"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setVisible(true); }//end constructor }//end class Inew2338_076b |
The red ball image file named inew2338sg005g.gif |
5. The following box contains a Java application. The next three boxes show the contents of image files in the current directory with the file names shown. The fifth box shows the GUI produced by the program when it first appears on the screen.
True or false? Pointing to the light bulb in the JButton with the mouse pointer causes the image of the light bulb to change to an image of an illuminated light bulb. Pressing the JButton causes the image of the light bulb to change to a blue light bulb.
import javax.swing.*; import java.awt.*; class Inew2338_078 extends JFrame{ public static void main(String[] args){ new Inew2338_078(); }//end main Inew2338_078(){ getContentPane().setLayout(new FlowLayout()); JButton theButton = new JButton("OK"); theButton.setIcon(new ImageIcon( "inew2338sg005j.gif")); theButton.setPressedIcon(new ImageIcon( "inew2338sg005m.gif")); theButton.setRolloverIcon(new ImageIcon( "inew2338sg005k.gif")); getContentPane().add(theButton); setSize(275,100); setTitle("Copyright 2004, R.G.Baldwin"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setVisible(true); }//end constructor }//end class Inew2338_078 |
Image file inew2338sg005j.gif |
Image file inew2338sg005m.gif |
Image file inew2338sg005k.gif |
6. The following box contains a Java application, which produces the GUI shown in the second box.
True or false? Holding down the ctrl key and pressing the B key (without holding down the shift key) produces the same result that is produced by pressing the JButton with the mouse pointer.
import javax.swing.*; import java.awt.*; class Inew2338_080 extends JFrame{ public static void main(String[] args){ new Inew2338_080(); }//end main Inew2338_080(){ getContentPane().setLayout(new FlowLayout()); JButton theButton = new JButton("OK"); theButton.setMnemonic('B'); getContentPane().add(theButton); setSize(275,100); setTitle("Copyright 2004, R.G.Baldwin"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setVisible(true); }//end constructor }//end class Inew2338_080 |
7. A Java application, which produces a GUI, is shown in the following box.
True or false? Given that the file named inew2338sg005p.gif contains a picture of a small blue ball, this application produces the GUI shown in the second box below.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; class Inew2338_082 extends JFrame{ //---------------------------------------// public static void main(String args[]) { new Inew2338_082(); }//end main() //---------------------------------------// //The purpose of this method is to create // and return an opaque pink JLabel with // a border. The text content of the // label is provided as the first // parameter. The border type is provided // as the second parameter. When the // label is displayed, the left and top // insets are displayed following the // text content of the label. JLabel makeLabel( String content,Border borderType){ JLabel label = new JLabel(); label.setBorder(borderType); label.setOpaque(true); label.setBackground(Color.pink); label.setText(content + "," +label.getInsets().left + "," +label.getInsets().top); return label; }//end makeLabel() //---------------------------------------// Inew2338_082(){//constructor getContentPane().setLayout( new FlowLayout()); getContentPane().add(makeLabel( "EtchedBorder",new EtchedBorder())); getContentPane().add(makeLabel( "BevelBorder RAISED",new BevelBorder( BevelBorder.RAISED))); getContentPane().add(makeLabel( "BevelBorder LOWERED",new BevelBorder( BevelBorder.LOWERED))); getContentPane().add(makeLabel( "EmptyBorder",new EmptyBorder( 5,5,5,5))); getContentPane().add(makeLabel( "Compound, Empty + BevelBorder RAISED", new CompoundBorder(new BevelBorder( BevelBorder.RAISED),new EmptyBorder( 5,5,5,5)))); getContentPane().add(makeLabel( "Compound, Empty + BevelBorder LOWERED", new CompoundBorder(new BevelBorder( BevelBorder.LOWERED),new EmptyBorder( 5,5,5,5)))); getContentPane().add(makeLabel( "Compound, Empty + SoftBevelBorder " + "RAISED", new CompoundBorder(new SoftBevelBorder( SoftBevelBorder.RAISED),new EmptyBorder( 5,5,5,5)))); getContentPane().add(makeLabel( "Compound, Empty + SoftBevelBorder " + "LOWERED", new CompoundBorder(new SoftBevelBorder( SoftBevelBorder.LOWERED),new EmptyBorder( 5,5,5,5)))); getContentPane().add(makeLabel( "Compound, Empty + LineBorder", new CompoundBorder(new LineBorder( Color.blue,5),new EmptyBorder( 5,5,5,5)))); getContentPane().add(makeLabel( "Compound, Empty + MatteBorder Image", new CompoundBorder(new MatteBorder( 19,19,19,19,new ImageIcon( "inew2338sg005p.gif")),new EmptyBorder( 5,5,5,5)))); getContentPane().add(makeLabel( "Compound, Empty + MatteBorder Color", new CompoundBorder(new MatteBorder( 19,19,19,19,Color.blue), new EmptyBorder(5,5,5,5)))); getContentPane().add(makeLabel( "Compound, Empty + TitledBorder", new CompoundBorder(new TitledBorder( "Title"),new EmptyBorder(5,5,5,5)))); setTitle("Copyright 2000, R.G.Baldwin"); setSize(329,500); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setVisible(true); }//end constructor }//end class Inew2338_082 |
8. The Java application in the following box produces one of the dialogs shown in the second, third, and fourth boxes. Which dialog is produced, Choice A, B, or C?
import javax.swing.*; import java.awt.*; import java.awt.event.*; class Inew2338_084b extends JFrame{ static JFrame thisFrame; public static void main(String[] args){ thisFrame = new Inew2338_084b(); }//end main Inew2338_084b(){ JOptionPane.showConfirmDialog(thisFrame, "Message"); }//end constructor }//end class Inew2338_084b |
Choice A |
Choice B |
Choice C |
9. The Java application in the following box produces a GUI consisting of a JTabbedPane component. The JTabbedPane component has three layers or pages consisting of the following components:
True or false? This application produces the GUI shown in the second box below.
import javax.swing.*; class Inew2338_086a extends JFrame{ public static void main(String[] args){ new Inew2338_086a(); }//end main Inew2338_086a(){ JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab( "JButton",new JButton("OK")); tabbedPane.addTab("JLabel",new JLabel("OK")); tabbedPane.addTab("JPanel",new JPanel()); getContentPane().add(tabbedPane); setSize(275,100); setTitle("Copyright 2004, R.G.Baldwin"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setVisible(true); }//end constructor }//end class Inew2338_086a |
10. True or false? The Java application in the following box produces the GUI shown in the second box below. This GUI consists of a vertically-scrollable table of empty cells with 5 rows and 6 columns. The user can enter data into the cells as shown in the GUI.
import javax.swing.*; class Inew2338_086a extends JFrame{ public static void main(String[] args){ new Inew2338_086a(); }//end main Inew2338_086a(){ JTable aTable = new JTable(5,6); JScrollPane scrollPane = new JScrollPane(aTable); getContentPane().add(scrollPane); setSize(275,100); setTitle("Copyright 2004, R.G.Baldwin"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); setVisible(true); }//end constructor }//end class Inew2338_086a |
Copyright 2004, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which have gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
You can learn more about this topic at http://www.codeguru.com/java/Swing/JTable/index.shtml.
You might also want go to Google and search for the following keywords:
This might help you to locate some useful publications on this topic.
The default case produced by instantiating the JTabbedPane using the following constructor
new JTabbedPane();
results in a tabbed pane with the tabs along the top. To cause the tabs to be on the right, instantiate the JTabbedPane object using the following constructor and parameter:
new JTabbedPane(JTabbedPane.RIGHT);
The tabs can be placed on the top, bottom, left, or right by instantiating the JTabbedPane object and passing one of the following constants to the constructor:
As mentioned earlier, the top location is the default if the object is instantiated using the constructor that takes no parameters.
You can learn more about this topic at http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTabbedPane.html.
You might also want go to Google and search for the following keywords:
This might help you to locate some useful publications on these topics.
You can learn more about this topic at http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html.
You might also want go to Google and search for the following keywords:
This might help you to locate some useful publications on these topics.
You might also want go to Google and search for the following keywords:
This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier. Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.
You can learn more about this topic at http://java.sun.com/docs/books/tutorial/uiswing/components/button.html.
You might also want go to Google and search for the following keywords:
This might help you to locate some useful publications on the topic.
You can learn more about this topic in lesson 124 at http://www.dickbaldwin.com/tocadv.htm.
You might also want go to Google and search for the following keywords:
This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier. Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.
One class that implements Icon is ImageIcon. Therefore, the compilation error can be eliminated and the program can be made to produce the GUI shown by replacing the statement that reads
theButton.setIcon("inew2338sg005g.gif");
with a statement that reads
theButton.setIcon(new ImageIcon("inew2338sg005g.gif"));
You can learn more about this topic in lesson 124 at http://www.dickbaldwin.com/tocadv.htm.
You might also want go to Google and search for the following keywords:
This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier. Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
According to Sun, this method
"Returns the name of the LookAndFeel class that implements the native systems look and feel if there is one, otherwise the name of the default cross platform LookAndFeel class."
Because the Windows version of the SDK 1.4.2 or later provides a LookAndFeel that implements the native look and feel for the operating system, this program displays a GUI that exhibits the Windows LookAndFeel.
You can learn more about this topic in lesson 2 at http://www.dickbaldwin.com/java/Java002.htm.
You might also want go to Google and search for the following keywords:
This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier. Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.
Choice B shows the Motif LookAndFeel, while Choice C shows the Windows LookAndFeel.
You can learn more about this topic in lesson 9010 at http://www.austincc.edu/baldwin/Java9010.htm.
You might also want go to Google and search for the following keywords:
This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier. Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.
Note however, that the WindowsLookAndFeel may not be supported on all operating systems. Therefore, when the program is executed under an operating system other than Windows, the correct answer may be:
B. MetalLookAndFeel
MotifLookAndFeel
You can learn more about this topic in lesson 111 at http://www.dickbaldwin.com/tocadv.htm.
You might also want go to Google and search for the following keywords:
This might help you to locate some of Prof. Baldwin's publications on these topics that were not included in the lessons listed earlier. Go to the last page of the Google results and click on the link that reads repeat the search with the omitted results included to make certain that Google didn't omit any links that might be useful to you.
Copyright 2004, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which have gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-