Some AWT Components - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Some AWT Components

Description:

JButton button1 = new JButton('BUTTON1'); JButton button2 = new JButton('BUTTON2' ... panel.add(button1); panel.add(button2); panel.add(button3) ... – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 23
Provided by: timothy90
Category:

less

Transcript and Presenter's Notes

Title: Some AWT Components


1
Some AWT Components
Object
Component
Container
Button
List
Scrollbar
JComponent
Label
Canvas
Button
2
Some Swing Components
JComponent
AbstractButton
JLabel
JButton
JMenuItem
JList
JToggleButton
JScrollBar
JCheckBox
JFileChooser
3
Some AWT Containers
Container
JComponent
Panel
ScrollPane
Window
Applet
Dialog
Frame
4
Swing Components That Are Not JComponents (in red)
Container
JComponent
Panel
ScrollPane
Window
Applet
Dialog
Frame
JWindow
JDialog
JFrame
JApplet
5
Some More Swing Components That Are JComponents
JComponent
JLayeredPane
JPanel
JScrollPane
JDesktopPane
JInternalFrame
JTable
JTree
6
Example A Simple Framed Window
import java.awt. import javax.swing. public
class SwingTest public static void
main(String args) JFrame frame new
JFrame("Test Frame") frame.setSize(new
Dimension(300,200)) frame.setLocation(100
,100) frame.setVisible(true)
7
Example Output Display
  • This window was managed by the K Desktop
    Environment (KDE)
  • Clicking the Close button (X) will cause the
    display to be hidden, but the program will
    continue since no listeners are set up yet
  • Can use ctl-C to kill the Java Virtual Machine

8
Changing Background Color
import java.awt. import javax.swing. public
class SwingTest public static void
main(String args) JFrame frame new
JFrame("Test Frame") frame.setSize(new
Dimension(300,200)) frame.setLocation(100
,100) Container contentPane
frame.getContentPane()
contentPane.setBackground(Color.red)
frame.setVisible(true)
9
Adding a Label and Button
import java.awt. import javax.swing. public
class SwingTest public static void
main(String args) JFrame frame new
JFrame("Test Frame") frame.setSize(new
Dimension(300,200)) frame.setLocation(100
,100) Container contentPane
frame.getContentPane() JLabel label
new JLabel("HERE IS A LABEL")
contentPane.add(label, BorderLayout.NORTH)
JButton button new JButton("BUTTON")
contentPane.add(button, BorderLayout.SOUTH)
frame.setVisible(true)
10
New Display
Resized
11
Adding a List of Options
import java.awt. import javax.swing. public
class SwingTest public static void
main(String args) JFrame frame new
JFrame("Test Frame") frame.setSize(new
Dimension(300,200)) frame.setLocation(100
,100) Container contentPane
frame.getContentPane() JLabel label
new JLabel("HERE IS A LABEL")
contentPane.add(label, BorderLayout.NORTH)
JButton button new JButton("BUTTON")
contentPane.add(button, BorderLayout.SOUTH)
String options "Option 1", "Option 2",
"Option 3"
JList list new JList(options)
contentPane.add(list, BorderLayout.CENTER)
frame.setVisible(true)
12
New Display
Note that "Option 3" has been selected.
13
Adding a Check Box and Slider
public class SwingTest public static void
main(String args) JFrame frame new
JFrame("Test Frame") frame.setSize(new
Dimension(400,200)) frame.setLocation(100
,100) Container contentPane
frame.getContentPane() JLabel label
new JLabel("HERE IS A LABEL")
contentPane.add(label, BorderLayout.NORTH)
JButton button new JButton("BUTTON")
contentPane.add(button, BorderLayout.SOUTH)
String options "Option 1", "Option 2",
"Option 3" JList list new
JList(options) contentPane.add(list,
BorderLayout.CENTER) JCheckBox cbox
new JCheckBox("Check")
contentPane.add(cbox, BorderLayout.WEST)
JSlider slider new JSlider()
contentPane.add(slider, BorderLayout.EAST)
frame.setVisible(true)
14
New Display
15
Changing the Layout
public class SwingTest public static void
main(String args) JFrame frame new
JFrame("Test Frame") frame.setSize(new
Dimension(300,200)) frame.setLocation(100
,100) Container contentPane
frame.getContentPane()
contentPane.setLayout(new FlowLayout())
JLabel label new JLabel("HERE IS A LABEL")
JButton button new JButton("BUTTON")
String options "Option 1", "Option 2",
"Option 3" JList list new
JList(options) JCheckBox cbox new
JCheckBox("Check") JSlider slider new
JSlider() contentPane.add(label)
contentPane.add(button)
contentPane.add(list) contentPane.add(cbo
x) contentPane.add(slider)
frame.setVisible(true)
16
New Display
Resized
17
JPanel Example
JFrame frame new JFrame("Test Frame")
frame.setSize(new Dimension(300,200))
frame.setLocation(100,100) Container
contentPane frame.getContentPane()
JLabel label new JLabel("HERE ARE SOME
BUTTONS",
SwingConstants.CENTER) JButton button1
new JButton("BUTTON1") JButton button2
new JButton("BUTTON2") JButton button3
new JButton("BUTTON3") JPanel
panel new JPanel() panel.add(button1)
panel.add(button2)
panel.add(button3) contentPane.add(label
, BorderLayout.NORTH) contentPane.add(pan
el, BorderLayout.CENTER)
frame.setVisible(true)
18
JPanel Example Output
Note use of SwingConstants.CENTER argument in
JLabel constructor.
19
Changing JPanel Layout
JFrame frame new JFrame("Test Frame")
frame.setSize(new Dimension(300,200))
frame.setLocation(100,100) Container
contentPane frame.getContentPane()
JLabel label new JLabel("HERE ARE SOME
BUTTONS",
SwingConstants.CENTER) JButton button1
new JButton("BUTTON1") JButton button2
new JButton("BUTTON2") JButton button3
new JButton("BUTTON3") JPanel
panel new JPanel() panel.setLayout
(new BoxLayout(panel, BoxLayout.Y_AXIS))
panel.add(button1)
panel.add(button2) panel.add(button3)
contentPane.add(label, BorderLayout.NORTH)
contentPane.add(panel,
BorderLayout.CENTER) frame.setVisible(tr
ue)
20
New Output
  • The button panel is to the west because no other
    component was placed there
  • The BoxLayout constructor requires both the
    component being laid out and either
  • BoxLayout.X_AXIS
  • BoxLayout.Y_AXIS

21
Tweaking Example
JFrame frame new JFrame("Test Frame")
frame.setSize(new Dimension(300,200))
frame.setLocation(100,100) Container
contentPane frame.getContentPane()
LayoutManager lm contentPane.getLayout()
((BorderLayout)lm).setHgap(25) JLabel
label new JLabel("HERE ARE SOME BUTTONS",
SwingConstants.CENTER)
JButton button1 new
JButton("BUTTON1") JButton button2 new
JButton("BUTTON2") JButton button3 new
JButton("BUTTON3") JPanel panel
new JPanel() panel.setLayout(new
BoxLayout(panel, BoxLayout.Y_AXIS))
panel.add(button1) panel.add(button2)
panel.add(button3)
contentPane.add(label, BorderLayout.NORTH)
contentPane.add(panel, BorderLayout.CENTER)
frame.setVisible(true)
22
Tweaking Example Output
  • The LayoutManager returned by getLayout() is an
    interface type that the BorderLayout class
    implements
  • The setHgap method we want is in the BorderLayout
    class
  • So we must cast the LayoutManager to BorderLayout
    in order to use setHgap
Write a Comment
User Comments (0)
About PowerShow.com