Computer Science 2: Java GUI Programming
Computer Science 2: Java GUI Programming
04/14/15
What is a GUI?
Button
Label
Text Field
Check Radio Buttons
Box
Combo
Box
/usr/local/pub/sps/courses/cs2/gui/ShowComponents/
04/14/15
What is AWT?
04/14/15
What is AWT?
/usr/local/pub/sps/courses/cs2/gui/AWTComponents
04/14/15
What is Swing?
04/14/15
Objec
t
FontMetri
cs
Colo
r
Graphics
Component
Heavyweight
LayoutManage
r
Pane
l
Contain
er
Window
Apple
t
JApple
t
Frame
JFrame
Dialo
g
JDial
og
JComponen
t
Swing: javax.swing
Lightweight
04/14/15
AbstractBut
ton
JComponen
t
JTextCompone
nt
JMenuItem
JCheckBoxMenuIt
em
JButton
JMenu
JToggleButt
on
JRadioButtonMenuI
tem
JEditorPa
ne
JCheckBox
JTextFi
eld
JTextAr
ea
JRadioButton
JPasswordFie
ld
JLab
JLi
JComboBox JPan
JOptionPa
JScroll
JSlid
el
st
el
ne
Bar
er
JTabbedPan JSplitP
JLayeredPa JSeparat
JScrollP
JRootPa
e
ane
ne
or
ane
ne
JToolB JMenuBar JPopupMen
JFileChoo
JColorChoos JToolT
ar
u
ser
er
ip
JTre JTabl JTableHead JInternalFr
JProgress
JSpinn
e
e
er
ame
Bar
er
04/14/15
Container Classes
04/14/15
Frames
300 pixels hi
/
usr/local/pub/sps/courses/cs2/gui/Frames/MyFrame.java
04/14/15
Content Pane
Layered pane
Glass pane
Menu bar
Content pane
10
11
Layout Managers
04/14/15
12
FlowLayout
Horizontal g
of 10 pixels
/
usr/local/pub/sps/courses/cs2/gui/Layout/ShowFlowLayout.j
ava
04/14/15
13
Extending JFrame
public class GUIMain extends JFrame {
// construct GUI interface with components
public GUIMain() {
// set the layout manager
Container container = getContentPane();
container.setLayout();
// create UI components and add
container.add()
} // GUIMain
// create instance of GUIMain and set
// frame behaviors
public static void main(String args[]) {
GUIMain frame = new GUIMain();
frame.setTitle();
} // main
} // GUIMain
04/14/15
14
GridLayout
15
GridLayout
2,4
10, 10
0,4
4,4
04/14/15
16
BorderLayout
BorderLayout.WEST
BorderLayout.CENTER
BorderLayout.EAST
BorderLayout.SOUTH
/
usr/local/pub/sps/courses/cs2/gui/Layout/ShowBorde
rLayout.java
04/14/15
17
BorderLayout
04/14/15
18
BorderLayout
19
Color
04/14/15
// blueish
20
Color
/
usr/local/pub/sps/courses/cs2/gui/Color/ColorButtons.ja
va
04/14/15
21
Panels
text
field
button
12 buttons
04/14/15
22
Panels
text
field
button
12 buttons
Panel 2
uses a
border
layout:
text: North
Panel 1:
Center1
Panel
uses a
grid
layout
/
usr/local/pub/sps/courses/cs2/gui/Panels/MicrowaveUI.java
04/14/15
23
Fonts
24
Graphics
/
usr/local/pub/sps/courses/cs2/gui/Graphics/DrawStuff.java
04/14/15
25
Graphics
line
filled
oval
filled
arc
filled rectangle
unfilled rectangle
filled
polygon
04/14/15
string
unfilled
oval
unfilled
arc
26