Java Object Oriented Programming Components Objectives l Learn

Java Object Oriented Programming Components

Objectives: l Learn about the Java Layout Managers. l Learn how to use Light Weight Components. l Learn how to implement an Action. Listener. Lab 22 -2

Java Layout Managers Lab 22 -3

Java Layout Managers l l The Java Layout Managers are a collection of classes that know how to lay out Containers. Every container has a default Layout Manager. Lab 22 -4

Border. Layout l l l A Border. Layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER. When adding a component to a container with a border layout, you must use an add( ) method that takes the object to be added as the first argument, and one of these five constants as a second argument. Lab 22 -5

Border. Layout JPanel north. Panel = new JPanel(); add(north. Panel, Border. Layout. NORTH); The following example shows a JFrame containing five JPanel’s, one in each area. Lab 22 -6

Border. Layout is the default layout for all frames and windows. Lab 22 -7

Flow. Layout A flow layout arranges components in a directional flow, much like lines of text in a paragraph. The flow direction is determined by the container's component. Orientation property and may be one of two values: Default § Component. Orientation. LEFT_TO_RIGHT § Component. Orientation. RIGHT_TO_LEFT l Flow layouts are typically used to arrange buttons in a panel. It arranges buttons horizontally until no more buttons fit on the same line. The line alignment is determined by the align property. The possible values are: § LEFT § RIGHT Default § CENTER § LEADING § TRAILING l Lab 22 -8

Flow. Layout is the default layout for all panels. Lab 22 -9

Grid. Layout l The Grid. Layout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equalsized rectangles, and one component is placed in each rectangle. For example, the following code creates a JPanel that contains 3 rows and 4 columns: JPanel panel = new JPanel(new Grid. Layout(3, 4)); Lab 22 -10

Light Weight Components Lab 22 -11

JComponents JComponent is an object having a graphicical representation that can be displayed on the screen and that can interact with the user. Examples of components are the buttons, textfields, checkboxes, and scrollbars of a typical graphical user interface. Lab 22 -12

JComponents Method Summary Color Graphics int String int get. Background() Gets the background color of this component. get. Graphics() Returns this component's graphics context, which lets you draw on a component. get. Height() Returns the current height of this component. get. Text() Returns the text contained in this Text. Component. get. Width() Returns the current width of this component. void paint(Graphics g) Paints this component. void paint. Component(Graphics g) Paints this component. Called by the paint method. void repaint() Repaints this component.

JComponents Method Summary void set. Background(Color bg) Sets the background color of this component. void set. Double. Buffered(boolean a. Flag) Sets whether this component should us a buffer to paint. void set. Foreground(Color fg) Sets the foreground color of this component. void set. Border(Border border) Sets the border of this component. void set. Enabled(boolean enabled) Sets whether or not this component is enabled. void set. Font(Font f) Sets the font for this component. void set. Layout(Layout. Manager manager) Sets the Layout. Manager. void set. Preferred. Size(Dimension preferred. Size) Sets the preferred size of this component. void set. Size(int width, int height) Resizes this component so that it has width and height. void set. Text(String t) Sets the text of this Text. Component to the specified text. void set. Visible(boolean a. Flag) Makes the component visible or invisible.

JPanel Lab 22 -15

JPanel is a generic light weight component. It can be used as a container to hold other light weight components or as a drawing surface. Lab 22 -16

JPanel vs JComponent Opaque Double Buffered Background Color Layout. Manager JComponent false null JPanel true Default Color Flow. Layout Lab 22 -17

Java Object Oriented Programming Top Down Design

Top Down Programming Start JCreator. q Create a new file called “Lab 21. java”. q Save the new file in your Lab 21 folder. q Lab 24 -19

Top Down Programming package components; import java. awt. *; import javax. swing. *; public class Lab 21 extends JFrame { } Lab 24 -20

Top Down Programming main() Lab 24 -21
![Top Down Programming public class Lab 21 extends JFrame { public static void main(String[] Top Down Programming public class Lab 21 extends JFrame { public static void main(String[]](http://slidetodoc.com/presentation_image_h2/7e9ae028c408db264dd53fac97338ec4/image-22.jpg)
Top Down Programming public class Lab 21 extends JFrame { public static void main(String[] args) { Lab 21 lab = new Lab 21(); lab. set. Visible(true); } } Lab 24 -22

Top Down Programming Constructor Lab 24 -23
![Top Down Programming public class Lab 21 extends JFrame { public static void main(String[] Top Down Programming public class Lab 21 extends JFrame { public static void main(String[]](http://slidetodoc.com/presentation_image_h2/7e9ae028c408db264dd53fac97338ec4/image-24.jpg)
Top Down Programming public class Lab 21 extends JFrame { public static void main(String[] args) { Lab 21 lab = new Lab 21(); lab. set. Visible(true); } public Lab 21() { set. Size(400, 300); set. Title(“Components”); init. Text. Fields(); init. Button(); init. Menu(); } } Lab 24 -24

Top-Down Programming (cont’d) init. Text. Fields(), init. Button(), and init. Menu() will be used to add components to this JFrame object. Lab 12 -25

Top-Down Programming (cont’d) public void init. Text. Fields() { } public void init. Button() { } public void init. Menu() { } Lab 12 -26

init. Text. Fields public void init. Text. Fields() { JPanel center = new JPanel(); center. set. Layout(new Flow. Layout(Flow. Layout. LEFT)); center. set. Background(Color. yellow); Border. Layout is the default Layout. Manager for the JFrame class. add(center, Border. Layout. CENTER); } Lab 24 -27

JPanel Lab 24 -28

JText. Field Lab 22 -29

JText. Field is a lightweight component that allows the editing of a single line of text. Constructor Summary JText. Field() Constructs a new Text. Field. JText. Field(Document doc, String text, int columns) Constructs a new JText. Field that uses the given text storage model and the given number of columns. JText. Field(int columns) Constructs a new empty Text. Field with the specified number of columns. JText. Field(String text) Constructs a new Text. Field initialized with the specified text. JText. Field(String text, int columns) Constructs a new Text. Field initialized with the specified text and columns. Lab 22 -30

JText. Field Method Summary String get. Selected. Text() Returns the selected text contained in this Text. Component. void set. Editable(boolean b) Sets the specified boolean to indicate whether or not this Text. Component should be editable. void set. Horizontal. Alignment(int alignment) Sets the horizontal alignment of the text. Valid keys are: JText. Field. LEFT JText. Field. CENTER JText. Field. RIGHT Lab 22 -31

JText. Field public class Lab 21 extends JFrame { private JText. Field text 1 = new JText. Field(“”, 32); private JText. Field text 2 = new JText. Field(“”, 32); public static void main(String[] args) { new Lab 20(); } public Lab 20() { /* code not shown */ } } Lab 22 -32

init. Text. Fields public void init. Text. Fields() { JPanel center = new JPanel(); center. set. Layout(new Flow. Layout(Flow. Layout. LEFT)); center. set. Background(Color. yellow); center. add(text 1); center. add(text 2); add(center, Border. Layout. CENTER); Flow. Layout. CENTER is the } default Layout. Manager for the JPanel class. Lab 24 -33

JText. Field Lab 24 -34

JLabel Lab 22 -35

JLabel Jlabel can display either text, an image, or both. You can specify where in the label's display area the label's contents are aligned by setting the vertical and horizontal alignment. By default, labels are vertically centered in their display area. Text-only labels are leading edge aligned, by default; imageonly labels are horizontally centered, by default. Lab 22 -36

JLabel Constructor Summary JLabel() Creates a JLabel instance with no image and with an empty string for the title. JLabel(Icon image) Creates a JLabel instance with the specified image. JLabel(Icon image, int horizontal. Alignment) Creates a JLabel instance with the specified image and horizontal alignment. JLabel(String text) Creates a JLabel instance with the specified text. JLabel(String text, Icon icon, int horizontal. Alignment) Creates a JLabel instance with the specified text, image, and horizontal alignment. JLabel(String text, int horizontal. Alignment) Creates a JLabel instance with the specified text and horizontal alignment. Lab 22 -37

JLabel Method Summary Icon. get. Icon() Returns the default icon. void set. Horizontal. Alignment(int alignment) Sets the horizontal alignment of the text. Valid keys are: Swing. Constants. LEFT Swing. Constants. CENTER Swing. Constants. RIGHT void set. Icon(Icon default. Icon) Sets the button's default icon. Lab 22 -38

init. Text. Fields In a Flow. Layout objects public void init. Text. Fields() { are arranged in the order JPanel center = new JPanel(); they are added. center. set. Layout(new Flow. Layout(Flow. Layout. LEFT)); center. set. Background(Color. yellow); center. add(new JLabel(“First Text Field: ”)); center. add(text 1); center. add(new JLabel(“Second Text Field: ”)); center. add(text 2); add(center, Border. Layout. CENTER); } Lab 24 -39

JLabel In Flow. Layout objects are arranged in the order in which they are added. The default alignment is Flow. Layout. CENTER. Lab 22 -40

JButton Lab 22 -41

JButton is an implementation of a "push" button. Buttons can be configured, and to some degree controlled, by Actions. To configure a button to respond to user input (i. e. mouse clicks) you must add an Action. Listener to each button. Constructor Summary JButton() Creates a button with no set text or icon. JButton(Action a) Creates a button where properties are taken from the Action supplied. JButton(Icon icon) Creates a button with an icon. JButton(String text) Creates a button with text. JButton(String text, Icon icon) Creates a button with initial text and an icon. Lab 22 -42

JButton Method Summary void add. Action. Listener(Action. Listener l) Adds an Action. Listener to the button. Icon. get. Icon() Returns the default icon. void set. Icon(Icon default. Icon) Sets the button's default icon. Lab 22 -43

init. Button private void init. Button() { } Lab 22 -44

init. Button private void init. Button() { JPanel south = new JPanel(); } Lab 22 -45

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); } Lab 22 -46

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); } Lab 22 -47

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); } Lab 22 -48

init. Button We need an import statement to use the swing Borders: import javax. swing. border. Bevel. Border; private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); } Lab 22 -49

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); copy. set. Preferred. Size(new Dimension(60, 30)); } Lab 22 -50

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); copy. set. Preferred. Size(new Dimension(60, 30)); south. add(copy); } Lab 22 -51

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); copy. set. Preferred. Size(new Dimension(60, 30)); south. add(copy); add(south, Border. Layout. SOUTH); } Lab 22 -52

JButton Lab 22 -53

Action. Listener Interface The listener interface for receiving action events. Any class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's add. Action. Listener method. When the action event occurs, that object's action. Performed method is invoked. Method Summary void action. Performed(Action. Event e) Invoked when an action occurs.

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); copy. set. Preferred. Size(new Dimension(60, 30)); copy. add. Action. Listener( ); We need an import statement to use the Action. Listener interface: import java. awt. event. *; south. add(copy); add(south, Border. Layout. SOUTH); } Lab 22 -55

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); copy. set. Preferred. Size(new Dimension(60, 30)); copy. add. Action. Listener( ); south. add(copy); add(south, Border. Layout. SOUTH); } Lab 22 -56

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); copy. set. Preferred. Size(new Dimension(60, 30)); copy. add. Action. Listener(new Action. Listener() { }); south. add(copy); add(south, Border. Layout. SOUTH); } Lab 22 -57

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); copy. set. Preferred. Size(new Dimension(60, 30)); copy. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { } }); south. add(copy); add(south, Border. Layout. SOUTH); } Lab 22 -58

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); copy. set. Preferred. Size(new Dimension(60, 30)); copy. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { String text = text 1. get. Text(); } }); south. add(copy); add(south, Border. Layout. SOUTH); } Lab 22 -59

init. Button private void init. Button() { JPanel south = new JPanel(); south. set. Preferred. Size(new Dimension(400, 40)); south. set. Background(Color. cyan); JButton copy = new JButton(“Copy”); copy. set. Border(new Bevel. Border(Bevel. Border. RAISED)); copy. set. Preferred. Size(new Dimension(60, 30)); copy. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { String text = text 1. get. Text(); text 2. set. Text(text); } }); south. add(copy); add(south, Border. Layout. SOUTH); } Lab 22 -60

Action. Listener Click on the Copy button. Lab 22 -61

Menus Lab 22 -62

Menus are made up of three parts. 1. JMenu. Bar which is attached to a JFrame via the set. JMenu. Bar method. . 2. JMenu which is added to the JMenu. Bar (more than one can be added). 3. JMenu. Item which is added to the JMenu (more than one can be added.

init. Menu public void init. Menu() { } Lab 22 -64

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); } Lab 22 -65

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu("File"); } Lab 22 -66

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu(“File”); JMenu. Item exit = new JMenu. Item(“Exit”); } Lab 22 -67

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu(“File”); JMenu. Item exit = new JMenu. Item(“Exit”); exit. add. Action. Listener( ); } Lab 22 -68

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu(“File”); JMenu. Item exit = new JMenu. Item(“Exit”); exit. add. Action. Listener( ); } Lab 22 -69

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu(“File”); JMenu. Item exit = new JMenu. Item(“Exit”); exit. add. Action. Listener(new Action. Listener() { }); } Lab 22 -70

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu(“File”); JMenu. Item exit = new JMenu. Item(“Exit”); exit. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { } }); } Lab 22 -71

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu(“File”); JMenu. Item exit = new JMenu. Item(“Exit”); exit. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { System. exit(0); } }); } Lab 22 -72

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu(“File”); JMenu. Item exit = new JMenu. Item(“Exit”); exit. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { System. exit(0); } }); file. add(exit); } Lab 22 -73

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu(“File”); JMenu. Item exit = new JMenu. Item(“Exit”); exit. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { System. exit(0); } }); file. add(exit); menu. Bar. add(file); } Lab 22 -74

init. Menu public void init. Menu() { JMenu. Bar menu. Bar = new JMenu. Bar(); JMenu file = new JMenu(“File”); JMenu. Item exit = new JMenu. Item(“Exit”); exit. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { System. exit(0); } }); file. add(exit); menu. Bar. add(file); set. JMenu. Bar(menu. Bar); } Lab 22 -75

Menus Click on the Exit menu item to close the frame. Lab 22 -76

Java Object Oriented Programming Questions?

Java Object Oriented Programming Begin Lab 22

Extending JText. Field My. Number. Field for Lab 22 A Lab 22 -79

import javax. swing. JText. Field; import javax. swing. border. Bevel. Border; import java. awt. event. *; public class My. Number. Field extends JText. Field { } Lab 22 -80

public My. Number. Field(int columns) { super(columns); set. Border(new Bevel. Border(Bevel. Border. LOWERED)); set. Horizontal. Alignment(JText. Field. CENTER); add. Key. Listener(new Key. Adapter() { @Override public void key. Typed(Key. Event e) { char c = e. get. Key. Char(); if (!(Character. is. Digit(c) || (c == Key. Event. VK_BACK_SPACE) || (c == Key. Event. VK_DELETE))) e. consume(); } }); } Lab 22 -81

public void set. Value(int n) { set. Text(String. value. Of(n)); } public int get. Value() { try { return Integer. parse. Int(get. Text()); } catch (Number. Format. Exception e) { return 0; } } Lab 22 -82

Declaring An Instance Of My. Number. Field width = new My. Number. Field(24); Lab 22 -83
- Slides: 83