Chapter 13 Review Question Answers
Chapter 13 Review Question Answers
Chapter 13
6. You pass a JMenu object as an argument to the JMenuBar object's add method:
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
Algorithm Workbench
1.
JTextField textField = new JTextField(20);
textField.setEditable(false);
2.
String[]days={"Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday",
"Sunday"};
JListdayList=newJList(days);
3.
dayList.setVisibleRowCount(4);
JScrollPanescrollPane=newJScrollPane(dayList);
4.
selection=(String)myList.getSelectedValue();
5.
selectionIndex=myComboBox.getSelectedIndex();
10.
JTextAreatextInput=JTextArea(10,15);
JScrollPanescrollPane=newJScrollPane(textInput);
textInput.setLineWrap(true);
textInput.setWrapStyleWord(true);
11.
//CreateanOpenmenuitem.
JMenuItemopenItem=newJMenuItem("Open");
openItem.setMnemonic(KeyEvent.VK_O);
openItem.addActionListener(newOpenListener());
//CreateaPrintmenuitem.
JMenuItemprintItem=newJMenuItem("Print");
printItem.setMnemonic(KeyEvent.VK_P);
printItem.addActionListener(newPrintListener());
//CreateanExitmenuitem.
JMenuItemexitItem=newJMenuItem("Exit");
exitItem.setMnemonic(KeyEvent.VK_X);
exitItem.addActionListener(newExitListener());
//CreateaJMenuobjectfortheFilemenu.
JMenufileMenu=newJMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
//AddthemenuitemstotheFilemenu.
fileMenu.add(openItem);
fileMenu.add(printItem);
fileMenu.add(exitItem);
//Createthemenubar.
JMenuBarmenuBar=newJMenuBar();
Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e 4
//Addthefilemenutothemenubar.
menuBar.add(fileMenu);
//Setthewindow'smenubar.
setJMenuBar(menuBar);
12.
JSliderslider=newJSlider(JSlider.HORIZONTAL,
0,1000,500);
slider.setMajorTickSpacing(100);
slider.setMinorTickSpacing(25);
slider.setPaintTickMarks(true);
slider.setPaintLabels(true);
Short Answer
1. Single selection mode
2. JComboBox
3. An uneditable combo box combines a button with a list, and allows the user to
only select items from its list. An editable combo box combines a text field and a
list. In addition to selecting items from the list, the user may also type input into
the text field. The default type of combo box is uneditable.
4. You can pass the text as an argument to the constructor, and then pass an
ImageIcon object to the setIcon method. Another way is to pass the text, an
ImageIcon object, and an int specifying horizontal alignment to the JLabel
constructor.
5. A mnemonic is a key on the keyboard that you press in combination with the Alt
key to quickly access a component such as a button. When you assign a
mnemonic to a button, the user can click the button by holding down the Alt key
and pressing the mnemonic key.
6. The first occurrence of that letter appears underlined.
7. A tool tip is text that is displayed in a small box when the user holds the mouse
cursor over a component. The box usually gives a short description of what the
component does.
8. Add them to a ButtonGroup object.
9. The item is deselected, which causes the check mark to disappear. The checked
menu item component also generates an action event.
10. Dialog, DialogInput, SansSerif, Serif, and Monospaced
11. Because, as the user moves the JSlider component's knob, it will only take on
values within its established range.
12. Metal, Motif, and Windows