Specialised Components
description
Transcript of Specialised Components
J McQuillan SE204:2004/2005: Lecture 3 Slide 1
Specialised ComponentsCan create specialised components. Do this by subclassing the component that you are interested in. It now looks and behaves like we want. This subclass has all the properties of its superclass along with the extra’s we’ve added. We can now re-use this component!
can create multiple instances of it can subclass it
J McQuillan SE204:2004/2005: Lecture 3 Slide 2
import java.awt.*;import javax.swing.*;class DaysPanel extends JPanel{
JComboBox days;JLabel l;
DaysPanel(){setPreferredSize(new Dimension(300, 200));setBackground(Color.white);l = new JLabel("Choose a day");days = new JComboBox();days.addItem(“Monday");days.addItem(“Tuesday");add(l);add(days);
}}
J McQuillan SE204:2004/2005: Lecture 3 Slide 3
We can now place this in a frame, in an applet,
or any other higher-level container!
J McQuillan SE204:2004/2005: Lecture 3 Slide 4
Other Component MethodssetVisible(boolean b)
can be called on any component it makes the component visible or invisible
setEnable(boolean b) can be used to disable a component, it
cannot receive user input most components when disabled change
their appearance
J McQuillan SE204:2004/2005: Lecture 3 Slide 5
Laying out componentsCan place components in containers by
use of absolute position and size co-ordinates use of layout managers
The arrangement of several components in acontainer is called a layout.
J McQuillan SE204:2004/2005: Lecture 3 Slide 6
Laying out componentsThere are a number of predefined layout classes
FlowLayout BorderLayout GridLayout GridBagLayout CardLayout
These all implement the LayoutManagerinterface
J McQuillan SE204:2004/2005: Lecture 3 Slide 7
Using Layouts
Set the layout of the container by invoking the
setLayout() method.
This method accepts as a parameter an instance of a class that implements the
layoutmanager interface.
J McQuillan SE204:2004/2005: Lecture 3 Slide 8
Border Layout This allows you to place components in
five different regions in the container - North, South, East, West and Center.
You specify the position of each component when adding it to the container.
J McQuillan SE204:2004/2005: Lecture 3 Slide 9
Border Layout
Constructors
BorderLayout() -- creates a new border layout
BorderLayout(int hGap, int vGap) -- creates a new border layout with the specified horizontal and vertical gaps
J McQuillan SE204:2004/2005: Lecture 3 Slide 10
import java.awt.*;
import javax.swing.*;
Public class borderPanel extends JPanel{
JButton[] b;
borderPanel(){
setPreferredSize(new Dimension(300, 200));
setBackground(Color.white);
setLayout(new BorderLayout());
b = new JButton[5];
for(int i=1; i<=b.length; i++)
b[i-1] = new JButton("Button "+i);
add("North", b[0]);
add("East", b[1]);
add("South", b[2]);
add("West", b[3]);
add("Center", b[4]);}
}
J McQuillan SE204:2004/2005: Lecture 3 Slide 11
public class borderFrame extends JFrame
{
private borderPanel p1;
borderFrame(){
super("Border Frame");
p1 = new borderPanel();
getContentPane().add(p1);
pack();
setVisible(true);
}
public static void main(String args[]){
new borderFrame();
}
}
J McQuillan SE204:2004/2005: Lecture 3 Slide 12
Flow Layout This allows any number of components
in a container.
It arranges the components from left to right in a row. When that row is full, it will begin a new row.
J McQuillan SE204:2004/2005: Lecture 3 Slide 13
Flow LayoutConstructors
FlowLayout() -- creates a new flow layout with centered alignment.FlowLayout(int alignment) -- creates a new flow layout with the specified alignment.FlowLayout(int alignment, int hGap, int vGap) --creates a new flow layout with the specified alignment and horizontal and vertival gaps.
J McQuillan SE204:2004/2005: Lecture 3 Slide 14
import java.awt.*;
import javax.swing.*;
class flowPanel extends JPanel{
JButton[] b;
int n;
flowPanel(int n){
this.n = n;
setPreferredSize(new Dimension(300, 200));
setBackground(Color.white);
setLayout(new FlowLayout());
b = new JButton[n];
for(int i=1; i<=b.length; i++){
b[i-1] = new JButton("Button "+i);
add(b[i-1]);
}
}
}
J McQuillan SE204:2004/2005: Lecture 3 Slide 15
public class flowFrame extends JFrame {
private flowPanel p1;
flowFrame(){
super("Flow Frame");
p1 = new flowPanel(5);
getContentPane().add(p1);
pack();
setVisible(true);
}
public static void main(String args[]){
new flowFrame();
}
}
J McQuillan SE204:2004/2005: Lecture 3 Slide 16
Grid Layout This allows you to lay out components
in a grid of rows and columns.
J McQuillan SE204:2004/2005: Lecture 3 Slide 17
Grid LayoutConstructors
GridLayout(int rows, int cols) -- creates a new grid layout with the specified number of rows and columns
GridLayout(int rows, int cols, int hGap, int vGap) -- creates a new grid layout with the specified number of rows and columns and specified horizontal and vertical gaps
J McQuillan SE204:2004/2005: Lecture 3 Slide 18
import java.awt.*;
import javax.swing.*;
class gridPanel extends JPanel{
JButton[] b;
int n;
gridPanel(int n){
this.n = n;
setPreferredSize(new Dimension(300, 200));
setBackground(Color.white);
setLayout(new GridLayout(3,2));
b = new JButton[n];
for(int i=1; i<=b.length; i++){
b[i-1] = new JButton("Button "+i);
add(b[i-1]);
}
}
}
J McQuillan SE204:2004/2005: Lecture 3 Slide 19
public class gridFrame extends JFrame {
private gridPanel p1;
gridFrame(){
super("Grid Frame");
p1 = new gridPanel(5);
getContentPane().add(p1);
pack();
setVisible(true);
}
public static void main(String args[]){
new gridFrame();
}
J McQuillan SE204:2004/2005: Lecture 3 Slide 20
Other Layouts There are other layouts available
These include CardLayout, BoxLayout