Download presentation
Presentation is loading. Please wait.
1
Components
2
Objectives: Learn about the Java Layout Managers.
Learn how to use Light Weight Components. Learn how to implement an ActionListener. An unstated objective is to get a feel for the big-O of different algorithms without introducing this concept formally.
3
Java Layout Managers
4
Java Layout Managers The Java Layout Managers are a collection of classes that know how to lay out Containers. Every container has a default Layout Manager.
5
BorderLayout A BorderLayout 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.
6
BorderLayout JPanel northPanel = new JPanel();
add(northPanel, BorderLayout.NORTH); The following example shows a JFrame containing five JPanel’s, one in each area.
7
BorderLayout is the default layout for all frames and windows.
8
FlowLayout 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 componentOrientation property and may be one of two values: ComponentOrientation.LEFT_TO_RIGHT ComponentOrientation.RIGHT_TO_LEFT 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 CENTER LEADING TRAILING Default Default
9
FlowLayout is the default layout for all panels.
10
JPanel panel = new JPanel(new GridLayout(3, 4));
The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized 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 GridLayout(3, 4));
11
Light Weight Components
12
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.
13
JComponents Method Summary Color getBackground()
Gets the background color of this component. Graphics getGraphics() Returns this component's graphics context, which lets you draw on a component. int getHeight() Returns the current height of this component. String getText() Returns the text contained in this TextComponent. getWidth() Returns the current width of this component. void paint(Graphics g) Paints this component. paintComponent(Graphics g) Paints this component. Called by the paint method. repaint() Repaints this component.
14
JComponents Method Summary void setBackground(Color bg)
Sets the background color of this component. setDoubleBuffered(boolean aFlag) Sets whether this component should us a buffer to paint. setForeground(Color fg) Sets the foreground color of this component. setBorder(Border border) Sets the border of this component. setEnabled(boolean enabled) Sets whether or not this component is enabled. void setFont(Font f) Sets the font for this component. setLayout(LayoutManager manager) Sets the LayoutManager. setPreferredSize(Dimension preferredSize) Sets the preferred size of this component. setSize(int width, int height) Resizes this component so that it has width width and height height. setText(String t) Sets the text of this TextComponent to the specified text. setVisible(boolean aFlag) Makes the component visible or invisible.
15
JPanel
16
JPanel 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.
17
JPanel vs JComponent Opaque Double Buffered Background Color
LayoutManager JComponent false null JPanel true Default Color FlowLayout
18
Top Down Design
19
Top Down Programming Start JCreator.
Create a new file called “Lab21.java”. Save the new file in your Lab21 folder.
20
Top Down Programming package components; import java.awt.*; import javax.swing.*; public class Lab21 extends JFrame { }
21
Top Down Programming main()
22
Top Down Programming public class Lab21 extends JFrame { public static void main(String[] args) { Lab21 lab = new Lab21(); lab.setVisible(true); }
23
Top Down Programming Constructor
24
Top Down Programming public class Lab21 extends JFrame { public static void main(String[] args) { Lab21 lab = new Lab21(); lab.setVisible(true); } public Lab21() { setSize(400, 300); setTitle(“Components”); initTextFields(); initButton(); initMenu();
25
Top-Down Programming (cont’d)
initTextFields(), initButton(), and initMenu() will be used to add components to this JFrame object.
26
Top-Down Programming (cont’d)
public void initTextFields() { } public void initButton() public void initMenu()
27
initTextFields public void initTextFields() { JPanel center = new JPanel(); center.setLayout(new FlowLayout(FlowLayout.LEFT)); center.setBackground(Color.yellow); add(center, BorderLayout.CENTER); } BorderLayout is the default LayoutManager for the JFrame class.
28
JPanel Notice that the panel placed in the center quadrant is taking up the entire frame.
29
JTextField
30
JTextField JTextField is a lightweight component that allows the editing of a single line of text. Constructor Summary JTextField() Constructs a new TextField. JTextField(Document doc, String text, int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns. JTextField(int columns) Constructs a new empty TextField with the specified number of columns. JTextField(String text) Constructs a new TextField initialized with the specified text. JTextField(String text, int columns) Constructs a new TextField initialized with the specified text and columns.
31
JTextField Method Summary String
getSelectedText() Returns the selected text contained in this TextComponent. void setEditable(boolean b) Sets the specified boolean to indicate whether or not this TextComponent should be editable. setHorizontalAlignment(int alignment) Sets the horizontal alignment of the text. Valid keys are: JTextField.LEFT JTextField.CENTER JTextField.RIGHT
32
JTextField public class Lab21 extends JFrame { private JTextField text1 = new JTextField(“”, 32); private JTextField text2 = new JTextField(“”, 32); public static void main(String[] args) { new Lab20(); } public Lab20() { /* code not shown */
33
initTextFields public void initTextFields() { JPanel center = new JPanel(); center.setLayout(new FlowLayout(FlowLayout.LEFT)); center.setBackground(Color.yellow); center.add(text1); center.add(text2); add(center, BorderLayout.CENTER); } FlowLayout.CENTER is the default LayoutManager for the JPanel class.
34
JTextField
35
JLabel
36
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; image-only labels are horizontally centered, by default.
37
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 horizontalAlignment) 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 horizontalAlignment) Creates a JLabel instance with the specified text, image, and horizontal alignment. JLabel(String text, int horizontalAlignment) Creates a JLabel instance with the specified text and horizontal alignment.
38
JLabel Method Summary Icon. getIcon() Returns the default icon. void
setHorizontalAlignment(int alignment) Sets the horizontal alignment of the text. Valid keys are: SwingConstants.LEFT SwingConstants.CENTER SwingConstants.RIGHT setIcon(Icon defaultIcon) Sets the button's default icon.
39
initTextFields In a FlowLayout objects are arranged in the order they are added. public void initTextFields() { JPanel center = new JPanel(); center.setLayout(new FlowLayout(FlowLayout.LEFT)); center.setBackground(Color.yellow); center.add(new JLabel(“First Text Field:”)); center.add(text1); center.add(new JLabel(“Second Text Field:”)); center.add(text2); add(center, BorderLayout.CENTER); }
40
JLabel In FlowLayout objects are arranged in the order in which they are added. The default alignment is FlowLayout.CENTER.
41
JButton
42
JButton 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 ActionListener 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.
43
JButton Method Summary void
addActionListener(ActionListener l) Adds an ActionListener to the button. Icon. getIcon() Returns the default icon. void setIcon(Icon defaultIcon) Sets the button's default icon.
44
initButton private void initButton() { }
45
initButton private void initButton() { JPanel south = new JPanel(); }
46
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); }
47
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); }
48
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); }
49
initButton We need an import statement to use the swing Borders: import javax.swing.border.BevelBorder; private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); }
50
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); copy.setPreferredSize(new Dimension(60, 30)); }
51
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); copy.setPreferredSize(new Dimension(60, 30)); south.add(copy); }
52
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); copy.setPreferredSize(new Dimension(60, 30)); south.add(copy); add(south, BorderLayout.SOUTH); }
53
JButton
54
ActionListener 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 addActionListener method. When the action event occurs, that object's actionPerformed method is invoked. Method Summary void actionPerformed(ActionEvent e) Invoked when an action occurs.
55
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); copy.setPreferredSize(new Dimension(60, 30)); copy.addActionListener( ); south.add(copy); add(south, BorderLayout.SOUTH); } We need an import statement to use the ActionListener interface: import java.awt.event.*;
56
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); copy.setPreferredSize(new Dimension(60, 30)); copy.addActionListener( ); south.add(copy); add(south, BorderLayout.SOUTH); }
57
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); copy.setPreferredSize(new Dimension(60, 30)); copy.addActionListener(new ActionListener() { }); south.add(copy); add(south, BorderLayout.SOUTH); }
58
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); copy.setPreferredSize(new Dimension(60, 30)); copy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); south.add(copy); add(south, BorderLayout.SOUTH);
59
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); copy.setPreferredSize(new Dimension(60, 30)); copy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = text1.getText(); } }); south.add(copy); add(south, BorderLayout.SOUTH);
60
initButton private void initButton() { JPanel south = new JPanel(); south.setPreferredSize(new Dimension(400, 40)); south.setBackground(Color.cyan); JButton copy = new JButton(“Copy”); copy.setBorder(new BevelBorder(BevelBorder.RAISED)); copy.setPreferredSize(new Dimension(60, 30)); copy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = text1.getText(); text2.setText(text); } }); south.add(copy); add(south, BorderLayout.SOUTH);
61
ActionListener Click on the Copy button.
62
Menus
63
Menus Menus are made up of three parts.
JMenuBar which is attached to a JFrame via the setJMenuBar method.. JMenu which is added to the JMenuBar (more than one can be added). JMenuItem which is added to the JMenu (more than one can be added.
64
initMenu public void initMenu() { }
65
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); }
66
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu("File"); }
67
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu(“File”); JMenuItem exit = new JMenuItem(“Exit”); }
68
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu(“File”); JMenuItem exit = new JMenuItem(“Exit”); exit.addActionListener( ); }
69
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu(“File”); JMenuItem exit = new JMenuItem(“Exit”); exit.addActionListener( ); }
70
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu(“File”); JMenuItem exit = new JMenuItem(“Exit”); exit.addActionListener(new ActionListener() { }); }
71
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu(“File”); JMenuItem exit = new JMenuItem(“Exit”); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } });
72
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu(“File”); JMenuItem exit = new JMenuItem(“Exit”); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } });
73
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu(“File”); JMenuItem exit = new JMenuItem(“Exit”); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); file.add(exit);
74
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu(“File”); JMenuItem exit = new JMenuItem(“Exit”); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); file.add(exit); menuBar.add(file);
75
initMenu public void initMenu() { JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu(“File”); JMenuItem exit = new JMenuItem(“Exit”); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); file.add(exit); menuBar.add(file); setJMenuBar(menuBar);
76
Menus Click on the Exit menu item to close the frame.
77
Questions?
78
Begin Lab 22
79
MyNumberField for Lab22A
Extending JTextField MyNumberField for Lab22A
80
import javax.swing.JTextField;
import javax.swing.border.BevelBorder; import java.awt.event.*; public class MyNumberField extends JTextField { }
81
public MyNumberField(int columns) {
super(columns); setBorder(new BevelBorder(BevelBorder.LOWERED)); setHorizontalAlignment(JTextField.CENTER); addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if (!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) e.consume(); } });
82
public void setValue(int n) {
setText(String.valueOf(n)); } public int getValue() { try { return Integer.parseInt(getText()); catch (NumberFormatException e) return 0;
83
Declaring An Instance Of MyNumberField
MyNumberField width = new MyNumberField(24);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.