GUI Compact-Practical
GUI Compact-Practical
STIA1014 : Introduction To
Programming
GUI
Graphical User interface
[email protected] 1
JAVA GUI STIA1014 Session A232
Palette
Design
Area
Navigator
Properties
[email protected] 2
JAVA GUI STIA1014 Session A232
Getting Started
1) Drag Label
& Button to the
design area
2) Right click at
Label & choose
Edit Text, change
to “Display Label”
3) Perform similarly
for Button, change
to “Click Me to
Display Label”
[email protected] 3
JAVA GUI STIA1014 Session A232
GUI Coding
1) Right click at Label
& choose Change
Variable Name,
change to “lblOut”.
2) Double
click Button.
GUI Coding
1) Make sure you’re
in the right
jButtonAction area,
create some spaces
for your coding
2) Type:
lblOut.setText(“Hello GUI”);
3) Run it again.
Click your button
after it is executed
[email protected] 4
JAVA GUI STIA1014 Session A232
10
[email protected] 5
JAVA GUI STIA1014 Session A232
11
12
[email protected] 6
JAVA GUI STIA1014 Session A232
13
-Double-click the JTextField to select its display text & press Delete.
- The width of the JTextField will be reduced. You need to resize (make it
wider) the JTextField by dragging the JTextField's right edge resize handle
toward the right edge of the enclosing JFrame
14
[email protected] 7
JAVA GUI STIA1014 Session A232
15
16
[email protected] 8
JAVA GUI STIA1014 Session A232
17
18
[email protected] 9
JAVA GUI STIA1014 Session A232
19
20
[email protected] 10
JAVA GUI STIA1014 Session A232
Generated
codes
21
22
[email protected] 11
JAVA GUI STIA1014 Session A232
23
24
[email protected] 12
JAVA GUI STIA1014 Session A232
25
• Repeat the previous steps to rename the variable for Reset button from
jButton2 to resetBtn
resetBtn
26
[email protected] 13
JAVA GUI STIA1014 Session A232
27
28
[email protected] 14
JAVA GUI STIA1014 Session A232
widthTF
areaTF
perimeterTF
29
30
[email protected] 15
JAVA GUI STIA1014 Session A232
31
• The action performed by the Compute button can be divided into 3 steps:
1. It is going to accept user inputs, length & width, from the textfields
lengthTF and widthTF and convert the inputs from type String to
double,
2. It will then perform calculation of rectangle area & perimeter based on
the inputs length & width,
3. It will convert the area & perimeter to type String and place them in the
areaTF & perimeterTF respectively.
32
[email protected] 16
JAVA GUI STIA1014 Session A232
Step 1
Step 2
Step 3
33
34
[email protected] 17
JAVA GUI STIA1014 Session A232
•Next, we add the codes to implement what we want the Reset Button to do
(replacing the // TODO add your handling code here: line)
35
36
[email protected] 18
JAVA GUI STIA1014 Session A232
37
38
[email protected] 19
JAVA GUI STIA1014 Session A232
Exercise 1
39
Exercise 2
40
[email protected] 20
JAVA GUI STIA1014 Session A232
Exercise 3
41
Exercise 4
42
[email protected] 21
JAVA GUI STIA1014 Session A232
Exercise 5
43
Exercise 6
44
[email protected] 22
JAVA GUI STIA1014 Session A232
Exercise 7
45
Reset &Exit
Button
46
[email protected] 23
JAVA GUI STIA1014 Session A232
47
Multiple jFrames
48
[email protected] 24
JAVA GUI STIA1014 Session A232
EXAMPLE
49
50
[email protected] 25
JAVA GUI STIA1014 Session A232
51
JOptionPane.showMessageDialog(null,
"Uh-oh!", "Error",
JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null,
"I like turtles.", "Customized Dialog",
JOptionPane.INFORMATION_MESSAGE,
icon);
52
[email protected] 26
JAVA GUI STIA1014 Session A232
JOptionPane.showInputDialog
String m =
JOptionPane.showInputDialog("Any
one there?");
String m =
JOptionPane.showInputDia
log("Anyone there?");
taOut.setText("Hello:
\n"+m);
53
JOptionPane.showInputDialog
String m =
JOptionPane.showInputDialog("Any
one there?", 42);
String m =
JOptionPane.showInputDialog(null,
"Broccoli is tasty!", "Green dinner",
JOptionPane.INFORMATION_MESSAGE);
Integer[] options = {2, 3, 5, 7, 9, 11};
int n =
(Integer)JOptionPane.showInputDialog
(null, "Pick a number that is not prime:",
"Prime numbers",
JOptionPane.QUESTION_MESSAGE, null,
options, options[2]);
54
[email protected] 27
JAVA GUI STIA1014 Session A232
55
56
[email protected] 28
JAVA GUI STIA1014 Session A232
57
Sample Solution
58
[email protected] 29
JAVA GUI STIA1014 Session A232
Implementing Options
with radio Button
59
Exercise 2
60
[email protected] 30
JAVA GUI STIA1014 Session A232
61
Implementing
Counter with Button
62
[email protected] 31
JAVA GUI STIA1014 Session A232
Exercise 3
63
64
[email protected] 32
JAVA GUI STIA1014 Session A232
65
Reset/Again Button
66
[email protected] 33
JAVA GUI STIA1014 Session A232
Finish
Button
67
Exercise 4
68
[email protected] 34
JAVA GUI STIA1014 Session A232
69
JTABLE
70
[email protected] 35
JAVA GUI STIA1014 Session A232
Variable name:
tblResult
71
1. DefaultTableModel model =
(DefaultTableModel)tblResult.getModel();
2. model.setValueAt(value, indColumn, indRow);
72
[email protected] 36
JAVA GUI STIA1014 Session A232
Exercise 5
73
74
[email protected] 37
JAVA GUI STIA1014 Session A232
Exercise 6
75
76
[email protected] 38
JAVA GUI STIA1014 Session A232
77
78
[email protected] 39
JAVA GUI STIA1014 Session A232
Finish
Button
79