Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 3 Writing Classes for GUI Applications
Student Name: ______________________________________________________
TA Initial and Checkout time: ___________________________________________
TA Comments: ______________________________________________________
Prelab: Answer the following questions before working on the lab computers.
1. Knowledge of the java GUI APIs
a. What are the two packages that hold most of the built-in Java GUI classes or interfaces?
 _________________________________________________________________________
b. What is the Java built-in class that provides a window GUI component?
 _________________________________________________________________________
c. Is a JPanel object light-weighted or heavy-weighted?
 _________________________________________________________________________
d. List at least 3 Java built-in GUI components that can be placed inside a JPanel object and 
allow mouse push interactions:
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
e. List at least 3 Java built-in GUI interfaces that associate user operations to certain GUI 
components:
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
CSci112 Fall 2012 Closed Lab Handout! Lab 3, Friday, September 7, 2012
Prepared by Jianxia Xue, Last Update Tuesday, September 4, 2012! 1 of 3
2. Building your own JPanel extension
class MyPanel _________ JPanel ____________ ActionListener{
 private JButton button;
 
 public MyPanel() {  }
 public void actionPerformed(ActionEvent e) { }
}
a. What are the two keywords to fill the blanks in the class header? List them following the 
order of the blanks.
 _________________________________________________________________________
b. Specify what  you would do in the class constructor that enables a MyPanel object to hold a 
clickable button, you can use concrete code or pseudo code:
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
c. Specify what you would do in the method actionPerformed so when the button is clicked, 
it toggles the foreground color of the button from black to red, or from red to black. Again 
you can use concrete code or pseudo code:
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
 _________________________________________________________________________
CSci112 Fall 2012 Closed Lab Handout! Lab 3, Friday, September 7, 2012
Prepared by Jianxia Xue, Last Update Tuesday, September 4, 2012! 2 of 3
Coding Tasks
1. Debug BuggyButtons.java
The provided class BuggyButtons tries to implement an application that presents two buttons 
and a label as shown in the following figure:
The label displays a numeric value (initially 50). Each time the Increase button is pushed, the 
display  value should be increased by 1. Likewise, each time the Decrease button is pushed, the 
display value should be decreased by 1.
There are one syntax error and two logical errors. Trace the code and read the comments to 
check the details of the bugs. Remember that a JLabel object’s text can be changed on the fly 
using its setter method of setText(aStringObject).
2. Modify StyleOptionsPanel.java
Modify the StyleOptionsPanel program, so user can specify  the size of the font. Use a text 
field to obtain the size. An example interface is shown in the following figure:
When keys are pressed inside the text field, the event listener procedure will interpret user 
input string into an integer, and use the value to modify  the font size of the JLabel object. You 
can first read the event listeners for “Bold” and “Italic” checkboxes.
3. (Optional) Modify DirectionPanel.java
Modify the DirectionPanel program, so that the image is not allowed to move out of the 
visible area of the panel. Ignore any key event that would cause that to happen.
Note that this application requires four image files. Download and import the four arrow 
images into your project folder directly. Make sure you can run the original application first.
4. (Optional) Adding more key responses to DirectionPanel.java
Modify the DirectionPanel program so that, in addition to responding to the arrow keys, it 
also responds to four other keys that cause the image to move in diagonal directions. Then the T 
key is pressed, move the image up and to the left. Likewise, U key for up and right, G for down 
and left, and J for down and right. Do not move the image if it has reached a window boundary.
CSci112 Fall 2012 Closed Lab Handout! Lab 3, Friday, September 7, 2012
Prepared by Jianxia Xue, Last Update Tuesday, September 4, 2012! 3 of 3