18
1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder netBeans IDE create/edit/run a project GUIBuilder JFrame, JButton, JTextField, JTextArea, … model-view-control pattern

1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder netBeans IDE create/edit/run a project GUIBuilder JFrame, JButton, JTextField,

Embed Size (px)

Citation preview

Page 1: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

1

CSC 222: Object-Oriented Programming

Spring 2012

netBeans & GUIBuilder netBeans IDE

create/edit/run a project GUIBuilder

JFrame, JButton, JTextField, JTextArea, …model-view-control pattern

Page 2: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

2

BlueJ netBeans

BlueJ is an Interactive Development Environment (IDE) designed for novice programmers simple to use visual interface makes class vs. object distinction clear can directly manipulate objects & call methods using mouse clicks can also write and execute statements in the Code Pad

however, it is somewhat limiting to experienced programmers

netBeans is a popular, industry-strength IDE for developing software free & open-source multi-platform (Mac, Windows, Linux) multi-language (Java, PHP, C/C++, …) has an integrated GUI builder

download from netbeans.org

Page 3: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Create a project

3

Page 4: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Select location & project name

4

Page 5: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Adding a file to the project

5

Page 6: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Select location & file/class name

6

Page 7: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Can then edit the default file

7

Page 8: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Completing a projecteach class in the project is its own file

one class must have a public static void main method when you run the project, this main method is executed

8

Page 9: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Editor features

• code completion when type OBJECT., see list of method options

• Source menu• Format will try to indent selected lines consistently• Shift Left / Shift Right will manually indent selected lines• Toggle Comment comments/uncomments selected lines

• Refactor Rename can change a class/variable/method name

throughout the project

• Run Build Main Project compile all classes in the project Run Main Project execute the main method

9

Page 10: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

example: Magic 8-ball

create a new project named Magic use the default option to create a default main class named TerminalUI

download the Die.java class into the src folder of this project

define a new class named Magic8Ball has a 3-sided Die object as a field has a getAnswer method that takes a String as input (a question) and returns one of

3 possible answers at random: "Definitely", "No way", or "Outlook hazy"

in the main method of the TerminalUI class, prompt the user for a question call getAnswer and display the answer

10

Page 11: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

MVC pattern

model-view-controller is a software pattern used to develop reusable, modular software goal: isolate the application-specific logic from the user interface allows for independent testing & development, easy updates

for this example: the model consists of the logic of the application – Die & Magic8Ball the view is the Java terminal window the controller is the TerminalUI class with its text-based input/output

by separating the logic from the interface, it makes it possible to plug in a different interface, e.g., a Graphical User Interface (GUI)

11

Page 12: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

GUIBuildernetBeans has an integrated GUI builder built-in

can create a (Swing) GUI using a drag-and-drop interface

12

Page 13: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Naming the GUI class

13

Page 14: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

GUI design view

14

Page 15: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Build interface

drag GUI elements from the Palette onto the Design Frame can arrange, resize, alter Properties of the elements in particular, right click and change names of elements to be meaningful

(e.g., questionArea, answerButton, answerBox)

15

Page 16: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Associate events with elements

16

right click on element, select the event you want to handle

Page 17: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Source view

click on the Source button to see the generated code

17

Page 18: 1 CSC 222: Object-Oriented Programming Spring 2012 netBeans & GUIBuilder  netBeans IDE create/edit/run a project  GUIBuilder JFrame, JButton, JTextField,

Completing the GUIadd the Controller code to the GUI class

here, Magic8Ball object is a field, initialized in constructor code for I/O & method call are entered into the event-handler method

18