Presentation is loading. Please wait.

Presentation is loading. Please wait.

J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.

Similar presentations


Presentation on theme: "J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are."— Presentation transcript:

1 J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can 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

2 J McQuillanSE204:2004/2005: Lecture 3Slide 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); }

3 J McQuillanSE204:2004/2005: Lecture 3Slide 3 We can now place this in a frame, in an applet, or any other higher-level container!

4 J McQuillanSE204:2004/2005: Lecture 3Slide 4 Other Component Methods setVisible(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

5 J McQuillanSE204:2004/2005: Lecture 3Slide 5 Laying out components Can place components in containers by use of absolute position and size co- ordinates use of layout managers The arrangement of several components in a container is called a layout.

6 J McQuillanSE204:2004/2005: Lecture 3Slide 6 Laying out components There are a number of predefined layout classes FlowLayout BorderLayout GridLayout GridBagLayout CardLayout These all implement the LayoutManager interface

7 J McQuillanSE204:2004/2005: Lecture 3Slide 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 layout manager interface.

8 J McQuillanSE204:2004/2005: Lecture 3Slide 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.

9 J McQuillanSE204:2004/2005: Lecture 3Slide 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

10 J McQuillanSE204:2004/2005: Lecture 3Slide 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]);} }

11 J McQuillanSE204:2004/2005: Lecture 3Slide 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(); }

12 J McQuillanSE204:2004/2005: Lecture 3Slide 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.

13 J McQuillanSE204:2004/2005: Lecture 3Slide 13 Flow Layout Constructors 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.

14 J McQuillanSE204:2004/2005: Lecture 3Slide 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]); }

15 J McQuillanSE204:2004/2005: Lecture 3Slide 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(); }

16 J McQuillanSE204:2004/2005: Lecture 3Slide 16 Grid Layout This allows you to lay out components in a grid of rows and columns.

17 J McQuillanSE204:2004/2005: Lecture 3Slide 17 Grid Layout Constructors 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

18 J McQuillanSE204:2004/2005: Lecture 3Slide 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]); }

19 J McQuillanSE204:2004/2005: Lecture 3Slide 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(); }

20 J McQuillanSE204:2004/2005: Lecture 3Slide 20 Other Layouts There are other layouts available These include CardLayout, BoxLayout


Download ppt "J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are."

Similar presentations


Ads by Google