Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 17 Swing I – absolute java test bank


 

Download  file with the answers

Not a member!
Create a FREE account here to get access and download this file with answers


Chapter 17
Swing I
 Multiple Choice
1) A programming style that uses a signal and response approach to programming is called:
(a) Object Oriented Programming
(b) Structured Programming
(c) Event-driven Programming
(d) Bottom-up Programming

2) A ____________ is the smallest unit of space on which your screen can write.
(a) pixel
(b) dot
(c) bit
(d) font

3) The JFrame method setVisible() takes one argument of type:
(a) byte
(b) boolean
(c) int
(d) short

4) A button component should have a registered _____________ associated with it.
(a) pixel
(b) JFrame
(c) listener
(d) none of the above

5) A button fires events know as:
(a) static events
(b) passive events
(c) dynamic events
(d) action events

6) The ActionListener interface requires that the method ____________ be implemented.
(a) actionEvent()
(b) actionFired()
(c) actionRegistered()
(d) actionPerformed()

7) Inheritance indicates a/an ____________ relationship.
(a) Is-a
(b) Has-a
(c) both A & B
(d) none of the above

8) Composition indicates a/an _______________ relationship.
(a) Is-a
(b) Has-a
(c) both A & B
(d) none of the above

9) The _____________ manager places components into five regions.
(a) BorderLayout
(b) FlowLayout
(c) GridLayout
(d) AbsoluteLayout

10) Standard layout managers are defined in the ____________ package.
(a) javax.swing
(b) java.util
(c) java.awt
(d) java.lang

11) The _____________ manager is the simplest layout manager. It arranges components one after the other, going from left to right, in the order in which you add them.
(a) BorderLayout
(b) FlowLayout
(c) GridLayout
(d) AbsoluteLayout

12) The _______________ manager arranges components in a two-dimensional table with some number of rows and columns.
(a) BorderLayout
(b) FlowLayout
(c) GridLayout
(d) AbsoluteLayout

13) A _____________ is an object that is a simple container class that groups other objects.
(a) JPanel
(b) JMenu
(c) JButton
(d) JTextArea

14) A menu item is an object of the class:
(a) JMenu
(b) JMenuBar
(c) JMenuItem
(d) none of the above

15) A _____________ allows multiple lines of text to be displayed.
(a) JTextField
(b) JTextArea
(c) JTextBox
(d) JTextValue

16) The getText() method of a JTextField object returns an object of type:
(a) Double
(b) Integer
(c) Boolean
(d) String

 True/False
1) GUIs are windowing interfaces that handle user input and output.

2) In event-driven programming, your program determines the order in which things happen.

3) The event-driven paradigm requires implementing methods that will never be invoked.

4) The close-window button is in the class JButton.

5) The actionPerformed method of the ActionListener interface returns a Boolean value.

6) A Swing program does not end until it encounters a System.exit statement.

7) When using the BorderLayout manager you must specify all five regions.

8) One of the main functions of JPanel objects is to subdivide a JFrame (or other container) into different areas.

9) Events and listeners for menu items are handled in exactly the same way as they are for buttons.

10) A JMenu can not be a menu item in another menu.

11) In a Swing program, throwing an uncaught exception may leave the program in an unpredictable state.

 Short Answer/Essay
1) Create a Java GUI application that displays a window that is 300 pixels high by 300 pixels wide with a button labeled “Click Me”. The button should have no functionality at this point.
import javax.swing.JFrame;
import javax.swing.JButton;

public class FirstGui
{
public static final int WIDTH = 300;
public static final int HEIGHT = 300;
public static void main(String args[])
{
JFrame window = new JFrame();
window.setSize(WIDTH, HEIGHT);

JButton btnClick = new JButton(“Click Me”);
window.getContentPane().add(btnClick);

window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
}
2) Write a Java statement to add a title to the window in the GUI created in number 1 above that says “First Window”.
Answer: window.setTitle(“First Window”);
3) Add button functionality to the resulting GUI of number 2 above. When the button is clicked, the title of the window changes to “Modified First Window”.
Answer: Additional code for button functionality appears below in bold text.
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FirstGui
{
public static final int WIDTH = 300;
public static final int HEIGHT = 300;

public static void main(String args[])
{
final JFrame window = new JFrame();
window.setSize(WIDTH, HEIGHT);

JButton btnClick = new JButton(“Click Me”);
window.getContentPane().add(btnClick);

btnClick.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
window.setTitle(“Modified First Window”);
}
});

window.setTitle(“First Window”);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
}
4) Create a Java GUI application that contains a label and a button. The label reads “Click the button to change the background color.” When the button is clicked, the background is changed to a different color.
Answer:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ColorChange extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 300;

private static Container c;

public static void main(String args[])
{
ColorChange gui = new ColorChange();
gui.setVisible(true);
}

public ColorChange()
{
super(“Change Background Color Demo”);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c = getContentPane();

JLabel label1 = new JLabel(“Click the button to change the background
color”);
c.add(label1);

JButton button = new JButton(“Change Color”);
c.add(button);
}

public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand();
if(buttonString.equals(“Change Color”));
c.setBackground(Color.WHITE);
}
}
5) What are three common objects you must deal with when using a Swing container class?
Answer: The three common objects encountered when using Swing include the container, the components that are added to the container, and the layout manager.
6) Explain the model-view-controller pattern when applied to a typical GUI.
Answer: The model-view-controller pattern when applied to the task of designing a GUI allows the task to be divided into two main subtasks: 1) Designing and coding the appearance of the GUI on the screen; 2) Designing and coding the actions performed in response to button clicks and other user actions. This approach allows GUI components to send messages to determine which actions should be taken.
7) Write a Java statement to create a menu with the title “Drawing Template”.
Answer: JMenu m = new JMenu(“Drawing Template”);
8) Write Java statements to add menu items named “Circle”, “Square”, and “Rectangle” to the menu created in number 7 above.
Answer:
JMenuItem circleChoice = new JMenuItem(“Circle”);
m.add(circleChoice);

JMenuItem squareChoice = new JMenuItem(“Square”);
m.add(squareChoice);

JMenuItem rectangleChoice = new JMenuItem(“Rectangle”);
m.add(rectangleChoice);
9) Write Java statements to add the menu created in number 7 above to the menu bar.
Answer:
JMenuBar bar = new JMenuBar();
bar.add(m);
setJMenuBar(bar);
10) Create a Java GUI that allows the user to enter a desired user name and password. Provide a button, when clicked, informs the user that their account was successfully created. Include labels as appropriate.
Answer:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginGui extends JFrame implements ActionListener
{
public static final int WIDTH = 200;
public static final int HEIGHT = 100;

public static void main(String args[])
{
LoginGui gui = new LoginGui();
gui.setVisible(true);
}

public LoginGui()
{
super(“Account Login”);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c = getContentPane();
c.setLayout(new GridLayout(3,2));

JLabel label1 = new JLabel(“User Name:”);
JLabel label2 = new JLabel(“Password:”);
JTextField userName = new JTextField(20);
JTextField password = new JTextField(20);
JButton login = new JButton(“Login”);

c.add(label1);
c.add(userName);
c.add(label2);
c.add(password);
c.add(login);
login.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, “Account successfully created!”);
}

}
11) What is an action listener? What is an action event?
Answer: An action listener is an object that listens for action events. An action event occurs when a GUI component, like a JButton, fires a signal. This signal, known as an event, has a registered listener that invokes specific handling code.

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!