Title: Swing GUI
1Lecture 5
2Introduction
- The biggest difference between the AWT
components and Swing components are - Swing components are implemented with absolutely
no native code. Native code is methods
implemented in another programming language such
as C . - Swing features are present on every platform.
- Swing can have more functionality than AWT
components. - Swing can be shipped as an add-on to JDKÂ 1.1, in
addition to being part of the Java 2 Platform.
3Introduction
- Swing components have capabilities far beyond
what the AWT components offer - Swing buttons and labels can display images
instead of, or in addition to, text. - You can easily add or change the borders drawn
around most Swing components. For example, it's
easy to put a box around the outside of a
container or label. - You can easily change the behavior or appearance
of a Swing component by either invoking methods
on it or creating a subclass of it. - Swing components don't have to be rectangular.
Buttons, for example, can be round. -
4HelloWorldSwing program
- import javax.swing.
- public class HelloWorldSwing
- public static void main(String args)
- JFrame frame new JFrame("HelloWorldSwing")
- final JLabel label new JLabel("Hello World")
- frame.getContentPane().add(label)
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
E) - frame.pack()
- frame.setVisible(true)
-
5Compile and Run Swing
- Compile Swing program
- javac SwingApplication.java
- Run Swing program
- java SwingApplication
6(No Transcript)
7Swing Components and the Containment Hierarchy
- Swing provides many standard GUI components such
as buttons, lists, menus, and text areas, which
you combine to create your program's GUI. It also
includes containers such as windows and tool
bars. - SwingApplication creates four commonly used Swing
components - a frame, or main window (JFrame)
- a panel, sometimes called a pane (JPanel)
- a button (JButton)
- a label (JLabel)
8A diagram of the containment hierarchy for the
window shown by SwingApplication.
9How to Make Frames (Main Windows)
10import java.awt. import java.awt.event.
import javax.swing. public class FrameDemo
public static void main(String s) JFrame
frame new JFrame("FrameDemo")
frame.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0) ) JLabel emptyLabel new
JLabel("") emptyLabel.setPreferredSize(new
Dimension(175, 100)) frame.getContentPane().add(e
mptyLabel, BorderLayout.CENTER) frame.pack()
frame.setVisible(true)
11JPanel
- JPanel aPanel new JPanel()
- aPanel.setLayout(new BorderLayout())
- Or
- JPanel aPanel new JPanel(new BorderLayout())
12How to Use Buttons
- To create a button, you can instantiate one of
the many classes that descend from the
AbstractButton class. - Class Description
- JButton A common button
- JCheckBox A check box button.
- JRadioButton One of a group of radio
buttons - JMenuItem An item in a menu.
- JCheckBoxMenuItem A menu item that has a
checkbox. - JRadioButtonMenuItem A menu item that has a
radio - button
13Button(JButton)
- Normal Button
- MyButton new JButton (Click Me!)
- pane.add(MyButton)
- Image Button
-
- ImageIcon leftButtonIcon new
ImageIcon(flower.gif") - b1 new JButton(Bunga", leftButtonIcon)
14Label (JLabel)
- ImageIcon icon new ImageIcon("images/middle.gif"
) - . . .
- label1 new JLabel("Image and Text", icon,
JLabel.CENTER) - //Set the position of the text, relative to the
icon label1.setVerticalTextPosition(JLabel.BOTTOM
) label1.setHorizontalTextPosition(JLabel.CENTER)
label2 new JLabel("Text-Only Label") - label3 new JLabel(icon)
-
- //Add labels to the JPanel.
- add(label1)
- add(label2)
- add(label3)
15Check Boxes
- chinButton new JCheckBox("Chin")
chinButton.setSelected(true) - glassesButton new JCheckBox("Glasses")
glassesButton.setSelected(true) - hairButton new JCheckBox("Hair")
hairButton.setSelected(true) - teethButton new JCheckBox("Teeth")
teethButton.setSelected(true) -
16- JPanel checkPanel new JPanel()
checkPanel.setLayout(new GridLayout(0, 1))
checkPanel.add(chinButton) checkPanel.add(glasses
Button) checkPanel.add(hairButton)
checkPanel.add(teethButton)
17Example JCheckBox
- import java.awt.
- import javax.swing.
- public class KotakPilihan extends JApplet
- private JCheckBox java, c cobol, vbscript, asp
- public void init()
- Container pane getContentPane()
- pane.setBackground(Color.white)
- pane.setLayout(new FlowLayout())
-
18Java
C
Cobol
ASP
VBscript
- java new JCheckBox (Java, true)
- pane.add(java)
- c new JCheckBox (C)
- pane.add(c)
- cobol new JCheckBox (Cobol)
- pane.add(cobol)
- vbscript new JCheckBox (Vbscript)
- pane.add(vbscript)
- asp new JCheckBox (ASP, true)
- pane.add(asp)
-
-
-
19Radio Button
- JRadioButton birdButton new JRadioButton(Bird)
- JRadioButton catButton new JRadioButton(Cat)
- JRadioButton dogButton new JRadioButton(Dog)
- dogButton.setSelected(true)
- JRadioButton rabbitButton new
JRadioButton(Rabbit) - JRadioButton pigButton new JRadioButton(Pig)
20- // Group the radio buttons.
- ButtonGroup group new ButtonGroup()
- group.add(birdButton)
- group.add(catButton)
- group.add(dogButton)
- group.add(rabbitButton)
- group.add(pigButton)
21List (JList)
- Define List
-
- JList ShortList
- String item one, two,three,four,
five,six - ShortList new JList(item)
- ShortList.setVisibleRowCount(5)
- ShortList.setSelectionMode(ListSelectionModel.SING
LE_SELECTION) - JScrollPane scroll new JScrollPane(ShortList)
22Example JList
- import java.awt.
- import javax.swing.
- public class Senarai extends JApplet
- Container pane getContentPane()
- JList bentuk
- String item Garis, Segiempat,
Bulatan, Segitiga, Segienam, Segilapan
23- public void init()
- pane.setBackground(Color.white)
- pane.setLayout (new FlowLayout())
- bentuk new JList (item)
- bentuk.setVisibleRowCount (4)
- bentuk.setSelectionModel(ListSelectionModel.SINGL
E_SELECTION) - JScrollPane skrol new JScrollPane(bentuk)
- pane.add(skrol)
-
-
24Selecting Items in a List
- SINGLE_SELECTION
- Only one item can be selected at a time. When the
user selects an item, any previously selected
item is deselected first. - SINGLE_INTERVAL_SELECTION
- Multiple, contiguous items can be selected. When
the user begins a new selection range, any
previously selected items are deselected first.
25- MULTIPLE_INTERVAL_SELECTION
- The default. Any combination of items can be
selected. The user must explicitly deselect
items.
26JComboBox
- String petStrings "Bird", "Cat", "Dog",
"Rabbit", "Pig" - // Create the combo box, select item at index 4.
JComboBox petList new JComboBox(petStrings) - pane.add(petList)
27JSlider
- static final int FPS_INIT 10
- JSlider framesPerSecond new JSlider(JSlider.HORI
ZONTAL, 0, 30, FPS_INIT) - framesPerSecond.setMajorTickSpacing(10)
framesPerSecond.setMinorTickSpacing(1)
framesPerSecond.setPaintTicks(true)
framesPerSecond.setPaintLabels(true) - //add the slider to the content pane
contentPane.add(framesPerSecond) -
28JTextField
- textField new JTextField(20)
textField.addActionListener(this) - ...
- contentPane.add(textField)
29JTextArea
- JTextArea ruangTeks
- ruangTeks new JTextArea(5,20)
- JScrollPane skrol new JScrollPane(ruangTeks,
- JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
- JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS)
- pane.add(skrol)
30JPassword
- JPasswordField passwordField new
JPasswordField(10) - passwordField.setEchoChar('')
- passwordField.addActionListener(new
ActionListener() ... ) -
- private static boolean isPasswordCorrect(char
input) char correctPassword 'b', 'u',
'g', 'a', 'b', 'o', 'o' - if (input.length ! correctPassword.length)
- return false
- for (int i 0 i lt input.length i )
- if (inputi ! correctPasswordi)
- return false
- return true
-
31BorderLayout
- Container contentPane getContentPane()
- //Use the content pane's default BorderLayout.
- //contentPane.setLayout(new BorderLayout())
- contentPane.add(new JButton("Button 1 (NORTH)"),
BorderLayout.NORTH) - contentPane.add(new JButton("2 (CENTER)"),
- BorderLayout.CENTER)
- contentPane.add(newJButton("Button 3 (WEST)"),
BorderLayout.WEST) - contentPane.add(new JButton("Long-Named Button 4
(SOUTH)"), BorderLayout.SOUTH) - contentPane.add(new JButton("Button 5 (EAST)"),
BorderLayout.EAST)
32(No Transcript)
33BoxLayout
- Container contentPane getContentPane()
- contentPane.setLayout(new BoxLayout(contentPane,
BoxLayout.Y_AXIS)) - addAButton("Button 1", contentPane)
- addAButton("2", contentPane)
- addAButton("Button 3", contentPane)
- addAButton("Long-Named Button 4", contentPane)
- addAButton("Button 5", contentPane)
34CardLayout
35CardLayout Program
- JPanel cards
- final static String BUTTONPANEL "JPanel with
JButtons" - final static String TEXTPANEL "JPanel with
JTextField" - public CardWindow()
- Container contentPane getContentPane()
- //Put the JComboBox in a JPanel to get a nicer
look. - String comboBoxItems BUTTONPANEL,
TEXTPANEL - JPanel cbp new JPanel()
- JComboBox c new JComboBox(comboBoxItems)
- c.setEditable(false)
- c.addItemListener(this)
- cbp.add(c)
36- //Use the default layout manager,
- BorderLayout contentPane.add(cbp,
BorderLayout.NORTH) - cards new JPanel()
- cards.setLayout(new CardLayout())
- Jpanel p1 new JPanel()
- p1.add(new JButton("Button 1"))
- p1.add(new JButton("Button 2"))
- p1.add(new JButton("Button 3"))
- JPanel p2 new JPanel()
- p2.add(new JTextField("TextField", 20))
- cards.add(p1, BUTTONPANEL)
- cards.add(p2, TEXTPANEL)
- contentPane.add(cards, BorderLayout.CENTER)
37FlowLayout
- Container contentPane getContentPane()
contentPane.setLayout(new FlowLayout())
contentPane.add(new JButton("Button 1"))
contentPane.add(new JButton("2"))
contentPane.add(new JButton("Button 3"))
contentPane.add(new JButton("Long-Named Button
4")) - contentPane.add(new JButton("Button 5"))
38GridLayout
- Container contentPane getContentPane()
- contentPane.setLayout(new GridLayout(0,2))
- contentPane.add(new JButton("Button 1"))
- contentPane.add(new JButton("2"))
- contentPane.add(new JButton("Button 3"))
- contentPane.add(new JButton("Long-Named Button
4")) - contentPane.add(new JButton("Button 5"))