Docsity

Lab 8: Creating a GUI Frame using Facade Pattern in Java - Prof. Paul C. Grabow, Lab Reports of Software Engineering

Toggle sidebar
bg1
1
CSI3471
Lab8:FaçadePattern
Revised10/23/2008
CopytheexistingEclipseprojectcalledLab8Facadefromthefileserver.Youwillbemodifyingit.Please
renameitasyouhavedoneinthepast,e.g.,L8Grabow.Whenyouarefinishedwiththelabplease
uploadittoyourSVNrepository.
YouwillbeaddingJavacodetodisplaythefollowingGUIframe,whichcanbeusedtoaddastudentto
anexistingsetofstudents.TheGUIusesasinglefaçadeobjecttodoalloftheworkassociatedwith
storingStudentobjects.Beforeyoubegin,lookattheexistingcodeinsideLab8Facade.
Step1:CreateaJFrameobjectandamainclass
CreateapackagecalledtextinputandcreateaclasscalledAddStudentFrame,containingthe
followingcode:
class AddStudentFrame extends JFrame {
public AddStudentFrame() {
setTitle("Add Student");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;
}
Thiscode,however,isinadequatebecausethereisnothingtoinstantiateoneoftheseframes.Therefore,
createanotherclasscalledMainAddStudent,alsoinpackagetextinput,thatcontainsthefollowingcode:
public class MainAddStudent {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
AddStudentFrame frame = new AddStudentFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
pf3

Partial preview of the text

Download Lab 8: Creating a GUI Frame using Facade Pattern in Java - Prof. Paul C. Grabow and more Lab Reports Software Engineering in PDF only on Docsity!

CSI 3471

Lab 8: Façade Pattern

Revised 10/23/

Copy the existing Eclipse project called Lab8Facade from the file server. You will be modifying it. Please

rename it as you have done in the past, e.g., L8Grabow. When you are finished with the lab please

upload it to your SVN repository.

You will be adding Java code to display the following GUI frame, which can be used to add a student to

an existing set of students. The GUI uses a single façade object to do all of the work associated with

storing Student objects. Before you begin, look at the existing code inside Lab8Facade.

Step 1 : Create a JFrame object and a main class

Create a package called textinput and create a class called AddStudentFrame , containing the

following code:

class AddStudentFrame extends JFrame {

public AddStudentFrame() {

setTitle("Add Student");

setSize( DEFAULT_WIDTH , DEFAULT_HEIGHT );

public static final int DEFAULT_WIDTH = 300;

public static final int DEFAULT_HEIGHT = 300;

This code, however, is inadequate because there is nothing to instantiate one of these frames. Therefore,

create another class called MainAddStudent, also in package textinput , that contains the following code:

public class MainAddStudent {

public static void main(String[] args) { EventQueue. invokeLater ( new Runnable() { public void run() {

AddStudentFrame frame = new AddStudentFrame(); frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE ); frame.setVisible( true ); }

Compile and execute the code that you have created by highlighting MainAddStudent in the

Package Explorer window (which is normally on the left side of your screen), then choose Run as

Java application (using the right mouse button). Then you should see the following:

Step 2 : The following steps add code to the constructor for AddStudentFrame:

1. Inside the frame that you have created, declare the following objects:

final JTextField firstnameField = new JTextField();

final JTextField lastnameField = new JTextField();

final JTextField idField = new JTextField();

2. Declare a JPanel and set the layout to “GridLayout” with 3 rows and 2 columns. Then add labels as

indicated:

JPanel northPanel = new JPanel();

northPanel.setLayout( new GridLayout(3, 2));

northPanel.add( new JLabel("First name: \t", SwingConstants. RIGHT ));

northPanel.add(firstnameField);

northPanel.add( new JLabel("Last name: ", SwingConstants. RIGHT ));

northPanel.add(lastnameField);

northPanel.add( new JLabel("ID: ", SwingConstants. RIGHT ));

northPanel.add(idField);

3. Add the northPanel to the “north” portion of the gridlayout.

4. Create a JTextArea called textArea that is 8 by 40 pixels. Then create a JScrollPane called scrollPane which

has textArea as the only parameter in its constructor.

5. Add the scrollPane to the “center” portion of the gridlayout.

6. Create another JPanel called southPanel.

7. Create a JButton called insertButton, with “Insert” as the only parameter in its constructor.