Title: Test02- Requires Respondus LockDown Browser
Course: Java Programming Intermediate (APT TEMPLATE) > Section: Test02

Test for Event Handling competency.

Q: True or False: This program displays a small Frame object on the computer screen. When you hold down a mouse button and move your mouse pointer into the client area of the Frame, the coordinates of the mouse pointer appear directly above the pointer. As you move the mouse pointer around in the client area while holding the button down, the coordinates of the pointer continue to be displayed above the pointer.

If you release the mouse button while moving the mouse in the client area, the coordinates of the point where you released the button appear and remain there until you press the mouse button, at which time the coordinates of the mouse pointer resume being displayed.

Clicking the X-button terminates the program and returns control to the operating system.

/*File Q28.java
**********************************************************/
import java.awt.*;
import java.awt.event.*;

public class Q28 {
public static void main(String[] args){
GUI gui = new GUI();
}//end main
}//end class Q28
//=========================================================

class MyFrame extends Frame{
int xCoor;
int yCoor;

MyFrame(String name){//constructor
this.setTitle("Q28");
this.setSize(300,200);
this.setName(name);
}//end constructor

public void paint(Graphics g){
g.drawString("" + xCoor + ", " + yCoor, xCoor, yCoor);
}//end paint()
}//end class MyFrame
//==========================================================

class GUI {
public GUI(){//constructor
MyFrame myFrame1 = new MyFrame("Frame1");
myFrame1.setVisible(true);

myFrame1.addWindowListener(new WProc1());

MyMouseMotionProcessor mouseMotionProc =
new MyMouseMotionProcessor(myFrame1);
myFrame1.addMouseMotionListener(mouseMotionProc);

}//end constructor
}//end class GUI definition
//=========================================================

class MyMouseMotionProcessor extends MouseMotionAdapter{
MyFrame refToFrame1; //save references to the Frame

MyMouseMotionProcessor(MyFrame inFrame1){
refToFrame1 = inFrame1;
}// end constructor

public void mouseMoved(MouseEvent e){
refToFrame1.xCoor = e.getX();
refToFrame1.yCoor = e.getY();
refToFrame1.repaint();
}//end mouseMoved()

}//end class MyMouseMotionProcessor
//=========================================================

class WProc1 extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}//end windowClosing()
}//end class WProc1
//=========================================================
- True
- False
A: False

Q: True or False: The program shown below displays a small JFrame object on the computer screen. Each time the user clicks the mouse inside the JFrame object, the mouse coordinates are displayed near the mouse pointer and the old mouse coordinates from the previous click are erased.

When the user clicks the X-button in the upper right corner of the Frame, the program terminates and returns control to the operating system.

/*File Q33.java
**********************************************************/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Q33 {
public static void main(String[] args){
new Q33gui();
}//end main
}//end class Q33
//=======================================================//

class Q33gui extends JFrame{
public Q33gui(){//constructor
setSize(200,200);
setTitle("Q33");
getContentPane().add(new DisplaySpace(),"Center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}//end constructor
}//end Q33gui
//=======================================================//

class DisplaySpace extends JPanel{

int clickX;
int clickY;

DisplaySpace(){
addMouseListener(
new MouseAdapter(){//anonymous class definition
public void onMouseEvent(MouseEvent e){
clickX = e.getX();
clickY = e.getY();
repaint();
}//end onMouseEvent()
}//end MouseAdapter
);//end addMouseListener()
}//end constructor
//-----------------------------------------------------//

public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString(
"" + clickX + ", " + clickY, clickX, clickY);
}//end paintComponent
}//end class DisplaySpace
//=======================================================//
- True
- False
A: False

Q: True or False: The program shown below displays a small Frame object on the computer screen. Each time the user clicks the mouse inside the Frame object, the mouse coordinates are displayed near the mouse pointer and the old mouse coordinates from the previous click are erased.

When the user clicks the X-button in the upper right corner of the Frame, the program terminates and returns control to the operating system.

/*File Q34.java
*/
import java.awt.*;
import java.awt.event.*;

public class Q34 {
public static void main(String[] args){
GUI gui = new GUI();
}//end main
}//end class Q34
//---------------------------------------------------------

class GUI extends Frame{
int clickX;
int clickY;

public GUI(){//constructor
setSize(150,150);
setTitle("Q34");
setVisible(true);

addWindowListener(
new WindowAdapter(){//anonymous class definition
public void windowClosing(WindowEvent e){
System.exit(0);
}//end windowClosing
}//end WindowAdapter
);//end addWindowListener()

addMouseListener(
new MouseAdapter(){//anonymous class definition
public void mousePressed(MouseEvent e){
clickX = e.getX();
clickY = e.getY();
repaint();
}//end mousePressed()
}//end MouseAdapter
);//end addMouseListener()
}//end constructor
//-----------------------------------------------------//

public void paint(Graphics g){
g.drawString(
"" + clickX + ", " + clickY, clickX, clickY);
}//end paint

}//end class GUI definition
//---------------------------------------------------------
- True
- False
A: True

Q: True or False: The program shown below displays a small JFrame object on the computer screen.

When you move your mouse pointer around inside the client area of the frame, without pressing either mouse button, the coordinates of the mouse pointer appear directly above the pointer. In this case, the coordinates are displayed using black characters.

When you move your mouse pointer around inside the client area of the frame, while pressing either mouse button, the coordinates of the mouse pointer appear directly above the pointer. In this case, the coordinates are displayed using red characters.

When you click X-button in the upper right corner of the frame, the program terminates and control is returned to the operating system.

/*File Q35.java
**********************************************************/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Q35 {
public static void main(String[] args){
new Q35gui();
}//end main
}//end class Q35
//=======================================================//

class Q35gui extends JFrame{
public Q35gui(){//constructor
setSize(200,200);
setTitle("Q35");
getContentPane().add(new DisplaySpace(),"Center");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}//end constructor
}//end Q35gui
//=======================================================//

class DisplaySpace extends JPanel{

int clickX;
int clickY;

DisplaySpace(){
addMouseMotionListener(
new MouseMotionAdapter(){//anonymous class definition
public void mouseMoved(MouseEvent e){
setForeground(Color.RED);
clickX = e.getX();
clickY = e.getY();
repaint();
}//end mouseMoved
public void mouseDragged(MouseEvent e){
setForeground(Color.BLACK);
clickX = e.getX();
clickY = e.getY();
repaint();
}//end mouseDragged
}//end MouseAdapter
);//end addMouseListener()
}//end constructor
//-----------------------------------------------------//

public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString(
"" + clickX + ", " + clickY, clickX, clickY);
}//end paintComponent
}//end class DisplaySpace
//=======================================================//
- True
- False
A: False

Q: True or False: The program shown below displays a small JFrame object containing two JButton objects and a JLabel object. The JLabel has a green background and blue letters. The captions on the two JButton objects are Top and Bottom.

The three objects are displayed vertically in the JFrame. The Top JButton appears at the top of the column of objects. The Bottom JButton appears at the bottom of the column. The JLabel appears between the two JButton objects.

At startup, the JLabel displays the word Top. When you click the Bottom button, the word in the JLabel changes to Bottom. When you click the Top button, the word in the JLabel changes to Top. When you click X-button on the JFrame, the program terminates and returns control to the operating system.

/*File Q36.java
**********************************************************/
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;

public class Q36{
public static void main(String[] args){
JFrame aFrame = new Q36gui();
}//end main
}//end class Q36
//=======================================================//

class Q36gui extends JFrame implements ActionListener{
JButton button1 = new JButton("Top");
JButton button2 = new JButton("Bottom");
JLabel label = new JLabel("Top");
//-------------------------------------------------------//

public Q36gui(){//constructor
this.setTitle("Q36");
this.getContentPane().add(button1,BorderLayout.NORTH);
label.setBackground(Color.GREEN);
label.setForeground(Color.BLUE);
label.setOpaque(true);
this.getContentPane().add(label,BorderLayout.CENTER);
this.getContentPane().add(button2,BorderLayout.SOUTH);
button1.addActionListener(this);
button2.addActionListener(this);
this.setSize(150,100);
this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//end constructor
//-------------------------------------------------------//

public void actionPerformed(ActionEvent e){
if(e.getActionCommand().indexOf("Top") != -1)
label.setText("Top");
else
label.setText("Bottom");
}//end actionPerformed()

}//end class Q36gui
//=======================================================//
- True
- False
A: True

Q: True or False: The program shown below displays a small Frame object containing two Button objects and a Label object. The Label has a green background and blue letters. The captions on the two Button objects are Top and Bottom.

The three objects are displayed vertically in the Frame. The Top Button appears at the top of the column of objects. The Bottom Button appears at the bottom of the column. The Label appears between the two Button objects.

At startup, the Label displays the word Top. When you click the Bottom button, the word in the Label changes to Bottom. When you click the Top button, the word in the Label changes to Top. When you click X-button on the Frame, the program terminates and returns control to the operating system.

/*File Q37.java
**********************************************************/
//import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Button;
import java.awt.Label;
import java.awt.BorderLayout;

import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;

public class Q37{
public static void main(String[] args){
Frame aFrame = new Q37gui();
}//end main
}//end class Q37
//=======================================================//

class Q37gui extends Frame
implements WindowListener, ActionListener{
Button button1 = new Button("Top");
Button button2 = new Button("Bottom");
Label label = new Label("Top");
//-------------------------------------------------------//

public Q37gui(){//constructor

this.setTitle("Q37");
add(button1,BorderLayout.NORTH);
label.setBackground(Color.GREEN);
label.setForeground(Color.BLUE);
add(label,BorderLayout.CENTER);
add(button2,BorderLayout.SOUTH);
button1.addActionListener(this);
button2.addActionListener(this);
this.setSize(150,100);
this.setVisible(true);

this.addWindowListener(this);
}//end constructor
//-------------------------------------------------------//

public void windowClosing(WindowEvent e){
//terminate the program when the window is closed
System.exit(0);
}//end windowClosing
//-------------------------------------------------------//

public void windowOpened(WindowEvent e){}//dummy
public void windowClosed(WindowEvent e){}//dummy
public void windowIconified(WindowEvent e){}//dummy
public void windowDeiconified(WindowEvent e){}//dummy
public void windowActivated(WindowEvent e){}//dummy
public void windowDeactivated(WindowEvent e){}//dummy
//-------------------------------------------------------//

public void actionPerformed(ActionEvent e){
if(e.getActionCommand().indexOf("Top") != -1)
label.setText("Top");
else
label.setText("Bottom");
}//end actionPerformed()

}//end class Q37gui
//=======================================================//
- True
- False
A: True

Q: True or False: The program shown below displays a JSlider centered in a JFrame. The JSlider is calibrated to extend from -100 on the left end to 100 on the right end with 0 at the center. There is a tick mark every 10 units and every other tick mark is labeled, including -100, 0, and 100.

When you move the thumb on the slider, the value of the current position of the thumb is displayed on the command-line screen.

When you click the X-button, the program terminates and returns control to the operating system.

/*File Q31
*********************************************************/
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.BorderLayout;
public class Q31{

public static void main(String[] args){
new Q31gui();
}//end main method
}//end class Q31
//======================================================//

class Q31gui extends JFrame{

private JSlider slider = new JSlider(-100,100);

public Q31gui(){//constructor
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);

getContentPane().add(slider);

setSize(300,100);
setTitle("Q31");
setVisible(true);
//--------------------------------------------------//

slider.addChangeListener(
new ChangeListener(){
public void stateChanged(ChangeEvent e){
System.out.print(" " + slider.getValue());
}//end stateChanged
}//end new ChangeListener
);//end addChangeListener
//--------------------------------------------------//
}//end constructor
//----------------------------------------------------//

}//end class Q31gui
//======================================================//
- True
- False
A: True

Q: True or False: The program shown below will compile and run successfully. When it starts running, it displays a small window and doesn't display anything on the command-line screen.

Whenever the small window is iconified to the system try, the following is displayed on the command-line screen:

******** WProc2 windowIconified test msg

Whenever the small window is deiconified from the system try, the following is displayed on the command-line screen:

******** WProc2 windowDeiconified test msg

Nothing happens when the user clicks the X-button. The program does not terminate and does not return control to the operating system.

/*File Q17.java
**********************************************************/

import java.awt.*;
import java.awt.event.*;

public class Q17 {
public static void main(String[] args){
GUI gui = new GUI();//instantiate a GUInterface object
}//end main
}//end class Q17
//=======================================================//

class GUI{
public GUI(){//constructor
//Create a new Frame object
Frame displayWindow = new Frame();
displayWindow.setSize(300,200);
displayWindow.setTitle("Copyright 1997, R.G.Baldwin");

WProc2 winProcCmd2 = new WProc2();
displayWindow.addWindowListener(winProcCmd2);

displayWindow.setVisible(true);

}//end constructor
}//end class GUI definition
//=======================================================//

class WProc2 extends WindowAdapter{

public void windowIconified(WindowEvent e){
System.out.println(
"******** WProc2 windowIconified test msg");
}//end windowIconified()

public void windowDeiconified(WindowEvent e){
System.out.println(
"******** WProc2 windowDeiconified test msg");
}//end windowDeiconified()

}//end class WProc2
//=======================================================//
- True
- False
A: True

Q: True or False: The program shown below displays a small Frame object on the computer screen. When the user clicks the mouse inside the Frame object, the mouse coordinates are displayed near the mouse pointer.

When the user clicks the X-button in the upper right corner of the Frame, the program terminates and returns control to the operating system.

/*File Q19.java
*/
import java.awt.*;
import java.awt.event.*;

public class Q19 {
public static void main(String[] args){
GUI gui = new GUI();
}//end main
}//end class Q19
//---------------------------------------------------------

class MyFrame extends Frame{
int clickX;
int clickY;

public void paint(Graphics g){
g.drawString(
"" + clickX + ", " + clickY, clickX, clickY);
}//end paint()
}//end class MyFrame
//---------------------------------------------------------

class GUI {
public GUI(){//constructor
MyFrame displayWindow = new MyFrame();
displayWindow.setSize(300,300);
displayWindow.setTitle("Q19");
displayWindow.setVisible(true);

displayWindow.addWindowListener(new WProc1());

displayWindow.addMouseListener(
new MProc1(displayWindow));
}//end constructor
}//end class GUI definition
//---------------------------------------------------------

class MProc1 extends MouseAdapter{
MyFrame refToWin; //save a reference to the window here

MProc1(MyFrame inWin){//constructor
refToWin = inWin;//save ref to window
}//end constructor

public void mousePressed(MouseEvent e){
refToWin.clickX = e.getX();
refToWin.clickY = e.getY();
refToWin.repaint();//display coordinate information
}//end mousePressed()
}//end class MProc1
//---------------------------------------------------------
class WProc1 extends WindowAdapter{

public void windowClosed(WindowEvent e){
System.exit(0);
}//end windowClosed()

}//end class Wproc1
//---------------------------------------------------------
- True
- False
A: False

Q: True or False: The program shown below displays a small JFrame with a red background. Two overlapping buttons appear in front of the red background. One button is green and the other button is yellow.

At startup, the green button is in front of and partially hides the yellow button. If you click the frontmost button, the two buttons swap positions front to back. In other words, clicking the green button when it is in front brings the yellow button to the front. Clicking the yellow button when it is in front brings the green button to the front. Clicking the button in the back does not cause the buttons to swap positions.

Clicking the X-button causes the program to terminate and return control to the operating system.

/*File Q24
**********************************************************/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Q24 extends JFrame{//subclass JFrame
JLayeredPane theLayeredPane;
JButton greenButton;
JButton yellowButton;

public static void main(String[] args){
new Q24();
}//end main
//-----------------------------------------------------//

Q24(){//constructor
theLayeredPane = this.getLayeredPane();
int frameWidth = 300;
int frameHeight = 100;

JTextField redTextField = new JTextField("");
redTextField.setBackground(Color.red);
this.getContentPane().add(redTextField,"Center");

greenButton = new JButton(
"greenButton on Layered Pane");
greenButton.setBackground(Color.green);
greenButton.setBounds(10,10,240,40);
greenButton.addActionListener(new MyActionListener());
theLayeredPane.add(greenButton,new Integer(1));

yellowButton = new JButton(
"yellowButton on Layered Pane");
yellowButton.setBackground(Color.yellow);
yellowButton.setBounds(40,20,240,40);
yellowButton.addActionListener(new MyActionListener());
theLayeredPane.add(yellowButton,new Integer(-29999));

//Set title, size, and visibility of JFrame object.
this.setTitle("Q24");
this.setSize(frameWidth,frameHeight);
this.setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//end constructor
//=====================================================//

class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals(
"greenButton on Layered Pane")){
theLayeredPane.setLayer(greenButton,-29999);
theLayeredPane.setLayer(yellowButton,1);
}else{
theLayeredPane.setLayer(greenButton,1);
theLayeredPane.setLayer(yellowButton,-29999);
}//end else
}//end actionPerformed()
}//end class MyActionListener
//=====================================================//
}//end class Q24
//=======================================================//
- True
- False
A: True