Professional Documents
Culture Documents
Paradigm
Advance OO Programming
BSCS Semester 6
MCS 3
Course Instructor: Ms. Iram
Window Fundamental
Component
Container
Window
Frame
Panel
Applet
7-2
7-3
Swing vs AWT
AWT is Javas original set of classes for building GUIs
Swings
PLAF (Pluggable Look and Feel).
Java provides several LAFs, including:
try
{
UIManager.setLookAndFeel(plafName);
}
catch (Exception ex) { ... }
16-5
GUI Components
Components are created using constructors:
JLabel guest = new JLabel ("Guest);
16-6
JFrame
Frames are the basis of any Java GUI
Frame is the actual window that encompasses your
Frame/Pane
Panel/JPanels
If a frame is a window, a panel is the glass
Panes hold a windows GUI components
Every frame has at least one pane, the default
Content Pane
Useful for layout
Needed to add components to the frame
When a frame is created, the content pane is created
with it
JScrollPane is useful for embedding tables and text
areas
JScrollPane
Swing components
Components discussed:
JLabel
JButton
JToggleButton
JCheckBox
JComboBox
Chapter:
16
JSlider
JTextField
JPasswordField
JTextArea
16-13
Model-View-Controller
Design pattern often used in Swing objects
Breaks a GUI object down into three parts
Model-View-Controller
Swing components that use the MVC pattern,
such as JList and JTable, generally have one
class that controls both the view and the
controller and a separate class that controls
the model
Model-View-Controller
Programmer instantiates a model (e.g., the
Layout Managers
Every panel has a layout manager
Layout managers tell Java where to put components when
you add them to a pane
Default Layouts
Each component has a default layout
BorderLayout
FlowLayout
BoxLayout
16-18
FlowLayout
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add (new JButton ("Back to Start"));
c.add (new JButton ("Previous Slide"));
c.add (new JButton ("Next Slide"));
c.add (new JButton ("Last Slide"));
c.add (new JButton ("Exit"));
16-19
BoxLayout
In a horizontal box, components are placed
horizontally, left to right.
BoxLayout (contd)
BoxLayout is the default layout for a Box
container.
BoxLayout (contd)
Container c = getContentPane();
c.setLayout(new FlowLayout());
Box box = Box.createVerticalBox();
box.add (new JButton ("Next Slide"));
box.add (new JButton ("Previous Slide"));
box.add (Box.createVerticalStrut (20) );
box.add (new JButton ("Exit"));
c.add (box);
Adds extra
vertical space
between
components
16-22
GridLayout
Splits the panel into a grid with given
16-23
GridLayout (contd)
Container c = getContentPane();
c.setLayout (new GridLayout(3, 2, 10, 20 ));
c.add (new JButton ("Back to Start"));
c.add (new JButton ("Previous Slide"));
c.add (new JButton ("Next Slide"));
Extra space
c.add (new JButton ("Last Slide"));
between the
c.add (new JButton ("Exit"));
cells (in pixels)
16-24
BorderLayout
Divides the area into five regions and adds a
component to the specified region.
NORTH
WEST
CENTER
EAST
SOUTH
16-25
BorderLayout (contd)
Container c = getContentPane();
c.setLayout(new BorderLayout()); // optional: default
c.add (new JButton ("Next Slide"), BorderLayout.EAST);
c.add (new JButton ("Previous Slide"), BorderLayout.WEST);
c.add (new JButton ("Back to Start"), BorderLayout.NORTH);
c.add (new JButton ("Last Slide"), BorderLayout.SOUTH);
c.add (new JButton ("Exit"), BorderLayout.CENTER);
16-26