0% found this document useful (0 votes)
47 views4 pages

12review (GUIBasics)

java GUI BASICS some questions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views4 pages

12review (GUIBasics)

java GUI BASICS some questions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 12 GUI Basics

1. There are old classes already named Button, CheckButton, RadioButton, etc. To
avoid naming confusion, the new Swing classes are named with prefix J such as
JButton, JCheckButton, and JRadioButton.

2. The AWT components are heavy weight while the Swing components are
lightweight.

3. java.awt.Component is the root of all Java GUI component classes. A container


class such as JFrame is a subclass of Component. JComponent is the root of Swing
GUI component classes.

4.

Component and JComponent are abstract classes, so you cannot create instances
from them. The last line is wrong, because you cannot add an object to a container.
Only an instance of Component can be added to a container.
Component c1 = new Component(); // Wrong, since Component is an abstract class
JComponent c2 = new JComponent();// Wrong, since JComponent is an abstract class

Component c3 = new JButton(); // OK


JComponent c4 = new JButton();// OK

5. You use the constructor of the JFrme class to create a frame. Use the setSize
method to set the size of the frame. If the statement frame.setSize(400, 300) and
frame.setVisible(true) were swapped in the MyFrameWithComponents class, you
have to resize the frame to display the components in the frame. This is because
the setVisible(true) statement causes the container to be repainted, but the
setSize(400, 300) doesn’t. When you resize the window, all the components are
repainted in the frame.

6. Yes. Yes. Yes

7. The layout manager provides a platform-independent way to place components in a


GUI interface. The default layout manager for a frame is BorderLayout. To add a
component to a frame, you have to add it to the content pane of the frame.

8. Using FlowLayout, the components are arranged in the container from left to right
in the order in which they were added. If one row becomes filled, a new row is
started. To use a FlowLayout manager, you need to set the layout in a container to
FlowLayout, such as with setLayout(new FlowLayout()). There is no limit on the
number of components that can be added to a FlowLayout container.

9. The GridLayout manager arranges components in a grid (matrix) formation with


the number of rows and columns defined by the constructor. The components are
placed in the grid from left to right, starting from the first row, then the second,
and so on, in the order in which they were added. To use a GridLayout manager,
you need to set the layout in a container to GridLayout, such as with
setLayout(new GridLayout()). The number of components you can add to a
GridLayout container is unlimited. (Please try adding more components into the
container, to see what happens when the number of components exceeds the grid
size. Interestingly, the column is enlarged.)

10. The BorderLayout manager divides the window into five areas: East, South, West,
North, and Center. Components are added to a BorderLayout using add(String,
Component), where String is "East", "South", "West", "North", or "Center". You
can use one of the following two constructors to create a new BorderLayout:

public BorderLayout(int hGap, int vGap)


public BorderLayout

You can add only one component into a section. If you need to add multiple
components in a section, group the components in a panel, and add the panel into
the section.

11. Line 7 created an instance of JFrame. It should be new Test() instead.

12. Use new JPanel(LayoutManager).

13. The default layout manager for a JPanel is FlowLayout. Components are added
directly to the JPanel.

14. No. There is no setTitle() method in JPanel. This method is defined for Frame; Panel
is used to organize components. The panel itself is invisible.

15. You could add components into a button. It is legal, but a button should not be
used as a container. See the last NOTE in Section 11.8.

16. Use the new Color(int, int, int) to create a color or use a standard color such as
Color.RED to specify a color. It is wrong to create a color using new Color(400,
200, 300), because it the values 400 and 300 are out of range. new Color(10, 0, 0) is
darker than new Color(200, 0, 0), because the smaller value, the darker is the color.

17. new Color((int)(256 * Math.random()), (int)(256 * Math.random()),


(int)(256 * Math.random())

18. jbtOK.setBackground(Color.BLUE)

19. Use new Font("Courier", Font.BOLD, 20)

20. To find out all the available colors, use

GraphicsEnvironment e = 
  GraphicsEnvironment.getLocalGraphicsEnvironment();

String[] fontnames = e.getAvailableFontFamilyNames();

21. use setBackground(Color), setForeground(Color), setFont(Font), and


setToolTipText(String) to set background, foreground, font, and a tool tip text.

22. The tool tip text is not displayed, because Line 16 sets a tool tip text on jbtOK, but
Line 17 adds a new button (different from jbtOK) to the content pane of the frame.

23. true
false

24. The effect is that only one button is added to the container. No syntax errors, nor
runtime errors.

25. new ImageIcon(“image/us.gif”).

26. It displays only two buttons. The buttons cannot be shared.

27. Yes. (See the NOTE box in the Section 11.10).

28. You use JButton jbt = new JButton("OK") to create a button b with the label OK.
You use jbt.setText("New label") to change the label to New label, for example.
Use the setIcon(ImageIcon) method to set an icon in the button. Use the
setPressedIcon(ImageIcon) method to set a pressed icon in the button. Use the
setRolloverIcon(ImageIcon) method to set a rollover icon in the button.

29. jbtOK.setForeground(Color.RED);
jbtOK.setBackground(Color.YELLOW);
jbtOK.setMnemonic('K');
jbtOK.setToolTipText("Click OK to proceed");
jbtOK.setHorizontalAlignment(SwingContants.RIGHT);
jbtOK.setVerticalAlignment(SwingContants.BOTTOM);
jbtOK.setHorizontalTextPosition(SwingContants.LEFT);
jbtOK.setVerticalTextPosition(SwingContants.TOP);
jbtOK.setIconTextGap(5);

30. text, icon, mnemonic, pressedIcon, selected.

31. You use JCheckBox jchk = new JCheckBox("Red"), for example, to create a
check box and use JCheckBox jchk = new JCheckBox("Red", true), for example,
to create a check box with the box checked initially. To determine whether a box is
checked using jchk.isSelected().

32. You use JRadioButton jrb = new JRadioButton("On"), for example, to create a
radio button and use JRadioButton jrb = new JRadioButton("On", true), for
example, to create a radio button with the radio button selected initially. Use
jrb.isSelected() to check if a radio button is selected. To group radio buttons,
create an instance of ButtonGroup, and add radio buttons to this instance.

33. You use JLabel jlbl = new JLabel("Address") to create a label named Address, and
jlbl.setText("New Name") to change the name to New Name. Use the setIcon()
method to set an icon in the label..

34. jlblMap.setForeground(Color.RED);
jlblMap.setBackground(Color.YELLOW);
jlblMap.setMnemonic('M');
jlblMap.setToolTipText("Map image");
jlblMap.setHorizontalAlignment(SwingContants.RIGHT);
jlblMap.setVerticalAlignment(SwingContants.BOTTOM);
jlblMap.setHorizontalTextPosition(SwingContants.LEFT);
jlblMap.setVerticalTextPosition(SwingContants.TOP);
jlblMap.setIconTextGap(5);

35. JTextField jtf = new JTextField("Welcome to Java", 10). To check whether a text field is
empty, use if (jtf.getText().trim().length() == 0).
36. Use JPasswordField.

You might also like