Download presentation
Presentation is loading. Please wait.
Published byJemima Walsh Modified over 6 years ago
1
GUIs Model/View/Controller Pattern Using BreezySwing
Computer Science 209 GUIs Model/View/Controller Pattern Using BreezySwing
2
Graphical User Interfaces
A GUI provides a human user with a view of the state of a data model and with controls for manipulating it A GUI consists of windows, icons, a mouse, and pull-down menus (WIMP), among other things
3
A GUI for a Student Object
4
Event-Driven Programming
An application sets up and displays a window The application waits for user events, such as mouse clicks on buttons or menu items The application responds to these events by manipulating the data model and updating the display
5
Model/View/Controller Pattern
In the MVC pattern The model is responsible for managing the data and updating the view The view is responsible for displaying the data and controls (buttons, etc.) The controller listens for user events and informs the model of them
6
Model/View/Controller Pattern
displays View listens Controller Model notifies
7
Actual Java Classes and Methods
displays MainView listens buttonClicked Student notifies
8
GUIApp Class Create the model Create the view, passing it the model
public class GUIApp{ public static void main(String[] args){ final Student s = new Student("Ken Lambert", 5) final MainView view = MainView(s); view.setSize(300, 400); view.setVisible(true); } } Create the model Create the view, passing it the model Set the view’s attributes and open it
9
MainView Class Import resources from two packages
import javax.swing.*; import BreezySwing.*; public class MainView extends GBFrame{ private IntegerField scoreField, positionField; private JButton updateButton; private JTextArea displayArea; private Student model; Import resources from two packages Declare variables for GUI widgets and the model GBFrame supports basic window functionality
10
Class Consturctor import javax.swing.*; import BreezySwing.*;
public class MainView extends GBFrame{ . public MainView(Student s){ this.setTitle("Student View"); model = s; JLabel positionLabel = addLabel("Position",1,1,1,1); JLabel scoreLabel = addLabel( "Score", 2,1,1,1); positionField = addIntegerField(0, ,2,1,1); scoreField = addIntegerField(. 0, ,2,1,1); displayArea = addTextArea( "", ,1,2,1); displayArea.setEditable(false); updateButton = addButton( "Update", 4,1,2,1); displayArea.setText(model.toString()); scoreField.setHorizontalAlignment(JTextField.CENTER); positionField.setHorizontalAlignment(JTextField.CENTER); }
11
The Controller/Event Handler
import javax.swing.*; import BreezySwing.*; public class MainView extends GBFrame{ . public void buttonClicked(JButton button){ int position = positionField.getNumber(); int score = scoreField.getNumber(); model.setScore(position, score); displayArea.setText(model.toString()); } One method for all button clicks Can compare button argument to existing buttons when choices must be made
12
The BreezySwing Site
13
Interfaces Collections Type Parameters
For Friday Interfaces Collections Type Parameters
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.