1 Why layout managers • Can we perform layout without them? – Yes. A container’s layout property can be set to null. • Absolute positioning: specify size and position of every component within that container. – does not adjust well when the top-level container is resized – does not adjust well to differences between users and systems
44
Embed
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Why layout managers
• Can we perform layout without them?– Yes. A container’s layout property can be set to null.
• Absolute positioning: specify size and position of every component within that container.
– does not adjust well when the top-level container is resized
– does not adjust well to differences between users and systems
2
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AbsoluteExampleApplet extends JApplet {
• A layout manager is an object that implements the LayoutManager interface and determines the size and position of the components within a container.
• A container's layout manager has the final say on the size and position of the components within the container, even though Components can provide size and alignment hints
5
Layout Managers
• Layout managers– Provided for arranging GUI components
– Provide basic layout capabilities
– Processes layout details
– Programmer can concentrate on basic “look and feel”
– Interface LayoutManager– Lay out elements by their relative positions without using
distance units
6
Layout Management
– Each container has a layout manager that directs the arrangement of its components
– Components are added to a container which uses a layout manager to place them
– Three useful layout managers are:
1) Border layout2) Flow layout3) Grid layout
Page 6
7
Containers
• Top-level containers• Jdialog• Jframe• Japplet
• Intermediate level containers– E.g. Jpanel, JScrollPan, …
• Heavyweight vs Lightweigtht components
8
Layout managers.
Layout manager Description FlowLayout Default for java.awt.Applet, java.awt.Panel and
javax.swing.JPanel. Places components sequentially (left to right) in the order they were added. It is also possible to specify the order of the components using the Container method add that takes a Component and an integer index position as arguments.
BorderLayout Default for the content panes of JFrames (and other windows) and JApplets. Arranges the components into five areas: North, South, East, West and Center.
GridLayout Arranges the components into rows and columns.
Layout managers.
9
FlowLayout
• FlowLayout– Most basic layout manager (but NOT always the default
manager)
– GUI components placed in container from left to right
10
Flow Layout
• Components are added from left to right
• Most basic layout manager (but NOT always the default manager)
panel = new JPanel();
panel.add(rateLabel);
panel.add(rateField);
panel.add(button);
panel.add(resultLabel);
panel = new JPanel();
panel.add(rateLabel);
panel.add(rateField);
panel.add(button);
panel.add(resultLabel);
1 // FlowLayoutDemo.java 2 // Demonstrating FlowLayout alignments. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*;10 11 public class FlowLayoutDemo extends JFrame {12 private JButton leftButton, centerButton, rightButton;13 private Container container;14 private FlowLayout layout;15 16 // set up GUI and register button listeners17 public FlowLayoutDemo()18 {19 super( "FlowLayout Demo" );20 21 layout = new FlowLayout();22 23 // get content pane and set its layout24 container = getContentPane();25 container.setLayout( layout );26 27 // set up leftButton and register listener28 leftButton = new JButton( "Left" );29 30 leftButton.addActionListener(31 32 // anonymous inner class33 new ActionListener() {34 35 // process leftButton event
FlowLayoutDemo.java
Lines 21-25
Set layout as FlowLayout
36 public void actionPerformed( ActionEvent event )37 {38 layout.setAlignment( FlowLayout.LEFT );39 40 // re-align attached components41 layout.layoutContainer( container );42 }43 44 } // end anonymous inner class45 46 ); // end call to addActionListener47 48 container.add( leftButton );49 50 // set up centerButton and register listener51 centerButton = new JButton( "Center" );52 53 centerButton.addActionListener(54 55 // anonymous inner class56 new ActionListener() {57 58 // process centerButton event 59 public void actionPerformed( ActionEvent event )60 {61 layout.setAlignment( FlowLayout.CENTER );62 63 // re-align attached components64 layout.layoutContainer( container );65 }66 }67 );68 69 container.add( centerButton );70
FlowLayoutDemo.java
Line 38
Line 61
When user presses left JButton, left align components
When user presses center JButton, center components
71 // set up rightButton and register listener72 rightButton = new JButton( "Right" );73 74 rightButton.addActionListener(75 76 // anonymous inner class77 new ActionListener() {78 79 // process rightButton event 80 public void actionPerformed( ActionEvent event )81 {82 layout.setAlignment( FlowLayout.RIGHT );83 84 // re-align attached components85 layout.layoutContainer( container );86 }87 }88 );89 90 container.add( rightButton );91 92 setSize( 300, 75 );93 setVisible( true );94 }95 96 // execute application97 public static void main( String args[] )98 { 99 FlowLayoutDemo application = new FlowLayoutDemo();100 101 application.setDefaultCloseOperation(102 JFrame.EXIT_ON_CLOSE );103 }104 105 } // end class FlowLayoutDemo
FlowLayoutDemo.java
Line 82
When user presses right JButton, right components
FlowLayoutDemo.java
15
Nested Classes
• Java allows to define a class within another class– logically grouping classes that are only used in one place
– increase encapsulation
– lead to more readable and maintainable code
– Inner class: Non-static nested classes
class OuterClass {
...
class NestedClass {
...
}
}
16
Anonymous Inner Classes
• make the code more concise• declare and instantiate a class at the same time.• do not have a name• use only once
JPanel pricePanel = new JPanel(); // Uses FlowLayout by default
pricePanel.add(new JLabel("Your Price:"));
pricePanel.add(priceTextField);
JPanel centerPanel = new JPanel(); // Uses FlowLayout
centerPanel.add(radioButtonPanel);
centerPanel.add(checkBoxPanel); // Frame uses BorderLayout by default
add(centerPanel, BorderLayout.CENTER);
add(pricePanel, BorderLayout.SOUTH);
41
Summary: Containers and Layouts
• User-interface components are arranged by placing them inside containers– Containers can be placed inside larger containers– Each container has a layout manager that directs the
arrangement of its components– Three useful layout managers are the border layout, flow
layout, and grid layout.– When adding a component to a container with the border
layout, specify the NORTH, EAST, SOUTH, WEST, or CENTER position
• The content pane of a frame has a border layout by default
• A panel has a flow layout by default
42
Applets and Stand-alone Applications
• It is possible to write Java applications in such a way that they can be executed both as stand-alone or applets – Java applets usually don't have a main method
– Execute with a main method and call the init() and start() method of the applet.
import java.awt.*;import javax.swing.*;public class BubbleSort extends JApplet { public void init() { JTextArea outputArea = new JTextArea(); Container container = getContentPane(); container.add( outputArea ); int array[] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; String output = "Data items in original order\n"; // append original array values to String output for ( int counter = 0; counter < array.length; counter++ ) output += " " + array[ counter ]; bubbleSort( array ); // sort array output += "\n\nData items in ascending order\n"; // append sorted array values to String output for ( int counter = 0; counter < array.length; counter++ ) output += " " + array[ counter ]; outputArea.setText( output ); setSize( 375, 200 ); setVisible( true ); } // sort elements of array with bubble sort public void bubbleSort( int array2[] ) { // loop to control number of passes for ( int pass = 1; pass < array2.length; pass++ ) { // loop to control number of comparisons for ( int element = 0; element < array2.length - 1; element++ ) { // compare side-by-side elements and swap them if first element is //greater than second element if ( array2[ element ] > array2[ element + 1 ] ) swap( array2, element, element + 1 ); } } }
// swap two elements of an array public void swap( int array3[], int first, int second ) { int hold; // temporary holding area for swap hold = array3[ first ]; array3[ first ] = array3[ second ]; array3[ second ] = hold; } public static void main( String args[] ) { JFrame app = new JFrame (“An applet running as an application”); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BubbleSort applet = new BubbleSort (); applet.init(); applet.start(); // attach applet to the center of the window app.getContentPane().add(applet); app.setSize(375, 200); app.setVisible(true); }} // end class BubbleSort