CENG325 Software Applications and Design: Introduction To
CENG325 Software Applications and Design: Introduction To
Lecture1
Introduction to:
1
Content
• Introduction
• GUI Importance
• Netbeans Environment
• Exercises
• GUI components and functions
• Adding functionalities to GUI elements
2
Introduction
GUI: graphical user interface “gooey”
3
GUI for JAVA
4
Example: non-GUI vs GUI programs
5
Version1-Using scanner and SOP
Try it 6
Version2-Using GUI Containers
• in this course we will be using Java Swing : a GUI toolkit that
includes a rich set of widgets.
• It is available for any IDE and we will be using Netbeans
• Java Swing class Hierarchy Diagram
Fully functioning
window with its title
and icons
7
Version2-Using JOptionPane for Input/Output
8
Version2-JOptionPane Dialogs
Confirm Dialog
Input Dialog
Message Dialog
9
Version2- Back to our example:
Try it
10
Version2- With Jframe (fully functional GUI)
• design a GUI using Netbeans IDE:
• adds the JAVA swing components: textfields, labels, combobox,
buttons, radio buttons…
• code the behavior of different components for input and output.
11
GUI- Input components
• When the user interacts (uses) with the application the following
action are performed:
1. Reading User entry: for example using
a. Textfields
b. Combobox
b. Tables
13
Creating a JFrame Container
14
Version2- With Jframe (fully functional GUI)
You have to create a Jframe object for your GUI and set the action
performed when Submit button is clicked to the following:
String fname=fn.getText();
//fn is the variable name of the first textfield
String lname=ln.getText();
//ln is the variable name of the second textfield
Try it
15
Example2: Simple Calculator
double num1;
double num2;
char op;
boolean done = false;
num1 = input.nextDouble();
op = input.next().charAt(0);
num2 = input.nextDouble();
16
Example2: Simple Calculator(continue)
switch (op) {
case '+':
System.out.println("your answer is " + (num1 + num2));
break;
case '-':
System.out.println("your answer is " + (num1 - num2));
break;
case '*':
System.out.println("your answer is " + (num1 * num2));
break;
case '/':
System.out.println("your answer is " + (num1 / num2));
break;
default:
System.out.println("Unknown operation, program end!");
done = true;
break;
} } Try it
input.close();
17
Example2: Simple Calculator with GUI
Let us Start with the simple adder
1. The user will enter 2 numbers two textfields for the user inputs.
2. Then, he will press a button to perform the addition and display the result a
button and a textArea or label to display the result.
Try it
19
EXTRA TO PRACTICE
20
Outline
21
Netbeans IDE Environment
• All Java development in the IDE takes place within projects, we first
need to create a new project within which to store sources and other
project files.
22
Creating a project ***Step1
• These steps are repeated every time you want to create a new
activity:
1. To create a new project, launch the NetBeans IDE and choose New
Project from the File menu:
23
Creating a project *** Step2
• Next, select Java from the Categories column, and Java Application
from the Projects column:
• Press the button labeled "Next" to proceed.
24
Creating a project *** Step3
• Choose a name for your project . The project name contains no
spaces.
• Project will be saved in the default Netbeans project folder in the
Documents. (browse to change if you like)
• It is better to remove the main class if you are going to use only GUI
• Clink “Finish”
25
Outline
26
Get Started !
27
Get Started ! (No GUI)
28
Creating a JFrame Container
• To add a JFrame container:
1.In the Projects window, right-click the Main Class node and choose
New > JFrame Form.
2.Enter PrintNum as the Class Name.
3.Click Finish.
The IDE creates the PrintNum form and opens it in the GUI Builder.
And it creates the PrintNum.java class within the application.
29
Creating a JFrame Container
30
Creating a JFrame Container
• Design the following GUI:
• The components of the GUI are found in the
palette.
31
Creating a JFrame Container
• From this list, our application will use JLabel
(a basic text label), JTextField, JTextArea
and JButton to trigger count.
• Adjust the text and format for the JLabel
JTextField and Jbutton.
32
Property Editor
• It allows you to edit the properties of each component.
• Such as background color, foreground color, font, and cursor.
• When you click on a component, the Property Editor is shown : a series of
rows — one row per property — that you can click and edit.
33
Text and Font edit
34
Text and Font edit
You can check the design preview by pressing Preview Design in the toolbar of
the GUI Builder.
35
Button Functionality
• Your application, after pressing the Count button, must print, in the
textArea, the numbers from 1 to the number entered by the user in the
textfield.
1. First the value entered by the user in the textfield must be read.
2. Second show the numbers in the TextArea.
a. To read the input written in a textField the getText( ) method is used. It
returns the input as a string. textFieldVariableName.getText( )
b. To write the output in the TextArea, the setText( ) method is used. It takes
the string to write as argument.
textAreaVariableName.setText(string)
36
Button Functionality
37
Change Variable Name
• When you have to add a code in the source of your design you have
to use the variable names of the used components (button, label,
textarea)
• The default names are not very relevant, so it makes sense to change
them from their defaults to something that is more meaningful.
• On the design, Right-click each component and choose "Change
variable name.“
• Name the textfield: “in”
button: “count”
textArea: “out”
38
Navigator
39
Button Functionality
• Write the following code in the ActionPerformed handler: (Notice the button name changed in the handler)
private void countActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String In=in.getText();//read the user input and store it in a string variable
for(int i=1;i<=lim;i++){ Or
s+=i+"\n";//catenate the string with the new number
41
Exercise2: Time calculator
42
Exercise2: Time calculator
• For each button ActionPerformed handler make the folllowing:
1. Read the days textfield and parse to double.
2. Read the hous textfield and parse to double.
3. Read the minutes textfield and parse to double.
43