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

Instructions for creating a java gui frame using the facade pattern. Students are required to create a jframe and a main class, add components to the frame, and implement an actionlistener to add student objects to an existing set using a faรงade object. The document also includes references to relevant java classes and packages.

Typology: Lab Reports

Pre 2010

Uploaded on 08/16/2009

koofers-user-qij-1
koofers-user-qij-1 ๐Ÿ‡บ๐Ÿ‡ธ

8 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1๎˜ƒ
๎˜ƒ
CSI๎˜ƒ3471๎˜ƒ
Lab๎˜ƒ8:๎˜ƒFaรงade๎˜ƒPattern๎˜ƒ
Revised๎˜ƒ10/23/2008๎˜ƒ
๎˜ƒ
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);
}
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.