200 likes | 281 Views
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!
E N D
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 SE204:2004/2005: Lecture 3
We can now place this in a frame, in an applet, or any other higher-level container! SE204:2004/2005: Lecture 3
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 SE204:2004/2005: Lecture 3
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 iscalled a layout. SE204:2004/2005: Lecture 3
Laying out components There are a number of predefined layout classes • FlowLayout • BorderLayout • GridLayout • GridBagLayout • CardLayout These all implement the LayoutManager interface SE204:2004/2005: Lecture 3
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. SE204:2004/2005: Lecture 3
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. SE204:2004/2005: Lecture 3
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 SE204:2004/2005: Lecture 3
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. SE204:2004/2005: Lecture 3
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. SE204:2004/2005: Lecture 3
Grid Layout • This allows you to lay out components in a grid of rows and columns. SE204:2004/2005: Lecture 3
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 SE204:2004/2005: Lecture 3
Other Layouts • There are other layouts available • These include CardLayout, BoxLayout SE204:2004/2005: Lecture 3