1 / 8

Component

Class Hierarchy of AWT Components. Component. java.awt.Component. Button. Button(text). Checkbox(text, initState). Checkbox. CheckboxGroup. Canvas. Choice(), addItem(text), Action. Choice. Label. List. List(nVisibleRows, multiSelectOk), add(text). ScrollPane. Scrollbar.

drea
Download Presentation

Component

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class Hierarchy of AWT Components Component java.awt.Component Button Button(text) Checkbox(text, initState) Checkbox CheckboxGroup Canvas Choice(), addItem(text), Action Choice Label List List(nVisibleRows, multiSelectOk), add(text) ScrollPane Scrollbar TextComponent TextField TextArea Container Panel Applet Window Frame Frame(title) Dialog FileDialog FileDialog(parentFrame, title, mode) MenuComponent java.awt.MenuComponent MenuBar CheckboxMenuItem MenuItem Menu Menu(text), add(menuItem), addSeparator()

  2. Java AWT Components • 19 components, roughly divided into three categories: • Visual components • Container components • Menu components • java.awt.Component provides the following methods • getSize(), setSeize(w, h), setBounds(x,y,w,h) • setForeground(), setBackground(), setFont() • setEnabled(): if false, then the component is grayed out and does not respond to user input. • setVisible(): whether the component is to be seen on the screen, is generally used for frames. • Visual Components • Total of 11 visual components • Button, Canvas, Checkbox, Choice, FileDialog, Label, List, ScrollPane, Scrollbar, TextArea, Textfield • Four steps to use the visual components in a GUI • create an instance of the component • add the component to a container • position the component • set event notification for the component

  3. Visual Components • Button • Constructor: Button(text) • Sends Action event when clicked • Canvas • It has no default appearance or behavior, and is used for creating custom drawing regions, work areas, or as a component. • Constructor: Canvas() • The default size is usually very small, so need to call setSize(). • Receives input events from mouse and keyboard, and sends Mouse, MouseMotion, Key events. • Choice • Constructor: Choice() • Use addItem(text) to populate the choice. • Send Item event. • FileDialog • Presents a file open or file save dialog. • Need to use setVisible() • Constructor: FIleDialog(parenatFrame, title, mode) • getFIle() or getDirectory() retrieve the name of file or directory specified by the user. • Label • Constructors: Label(), Label(text), Label(text, alignment) • getText(), setText(newText)

  4. Visual Components, Cont’d. • List • A collection of text items, arranged vertically. It automatically acquires scrollbar if needed. • Constructors: List(), List(nVisibleRows), List(nVisibleRows, multiSelectOk) • add(text), add(text, index) to populate the list • getItem(index), getItemCount(), getRows() • getSelectedIndex(), getSelectedIndexes() • getSelectedItem(), getSelectedItems() • ScrollPane • Contains a single compoennt which may be taller or wider than the scroll pane itself. Will automatically acquire horizontal or vertical scrollbars if necessary. • Constructors: ScrollPane(), ScrollPane(scrollbarPolicy) • Scrollbar • Constructors: Scrollbar(), Scrollbar(orientation), Scrollbar(orientation, initValue, sliderSize, minValue, maxValue) • TextFiled and TextArea • Implement one-dimensional and two-dimensional components for text input, display, and ediating. • Both classes extend from the TextComponent class. • Constructors: • TextFiled(), TextFiled(nCols), TextFiled(text), TextFiled(text, nCols) • TextArea(), TextArea(nRows, nCols), TextArea(text), • TextArea(text, nRows, nCols), TextArea(text, nRows, nCols, scrolbarPolicy) • getSelectedText(), getText(), • setText(text), setEditable(editable)

  5. Container Components • Containers are Component that can hold other components. • A container need to associate with a layout manager. • There are four non-superclass container classes • Applet • Frame • Panel • Dialog • Applet • Typically exist in browsers. Can be run using applet viewer. • The setSize() and setBounds() may not work properly for applets. So remove them from the init() method and tray to set the size in your HTML tag. • Frame • A Frame is an independent window, capable of being moved around on the screen independent of other GUI windows. • Any application that requires a GUI must use one or more frames to contain the desired components. • Constructors: Frame(), Frame(title) • When a frame is created, it is has no size and is not displayed on the screen. Need to call setSize(), setBound(), or setVisible() methods. • Use dispose() to release the non-memory resources of a frame when it is done. • Panel • In contrast to Applet and Frame which are top-level or outermost GUI components, Panel provide an intermediate ledvel of spatial organization for GUIs. • Use panel to group related components recursively. • Dialog • A pop-up window that accepts user input in response to a specific question. It is the superclass of FileDialog class.

  6. Menu Components • Two kind of menus: • pull-down menu • pop-up menu • Three steps to use a menu • Create a menu bar and attach it to the frame. • Create and populate the menu. • Attach the menu to the menu bar. • Four types of elements can be used to populate a menu; • Menu items • Checkbox menu iatems • Separators • Menus • Example // Create a frame Frame frm = new Frame(“Menu Frame”); frame.setSize(400, 300); // Create a menu bar and attach it to the frame menubar = new MenuBar(); frm.setMenuBar(menubar); // Populate the menu with menu items and separator Menu filemenu = new Menu(“File”); filemenu.add(new MenuItem(“New”)); filemenu.add(new MenuItem(“Open”)); filemenu.addSeparator(); filemenu.add(new MenuItem(“Save”)); // attach menu to the menu bar menubar.add(filemenu); // Set frame visible frm.setVisible(true);

  7. GUI Components Demo Program import java.applet.*; import java.awt.*; import java.awt.event.*; public class MyApplet extends Applet implements ActionListener { public void start() { setBackground(Color.orange); setForeground(Color.white); Label lbl = new Label("AWT Components Demo"); lbl.setFont(new Font("Times", Font.BOLD, 18)); add(lbl); Button b1 = new Button("BUTTON"); b1.addActionListener(this); add(b1); // Checkboxes and checkbox group Panel p1 = new Panel(); p1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); CheckboxGroup cbg = new CheckboxGroup(); Checkbox cb1 = new Checkbox("Check", true, cbg); p1.add(cb1); Checkbox cb2 = new Checkbox("Credit Card", false, cbg); p1.add(cb2); add(p1); // Choice is a pull-down list Panel p2 = new Panel(); p2.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); Label lbl2 = new Label("Choice:", Label.CENTER); p2.add(lbl2); Choice ch1 = new Choice(); ch1.addItem("Hamburger"); ch1.addItem("Pizza"); ch1.addItem("Fry Chicken"); p2.add(ch1); add(p2);

  8. GUI Components Demo Program, Cont’d. // List is collection of text items, arranged vertically. Panel p3 = new Panel(); //p2.setLayout(new BorderLayout()); p3.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); Label lbl3 = new Label("List:", Label.CENTER); p3.add(lbl3); List lst = new List(3, true); lst.add("Coke"); lst.add("Pepsi"); lst.add("Coffee"); lst.add("Tea"); lst.add("Juice"); lst.add("Water"); p3.add(lst); add(p3); // ScrollPane contains a single component which may be taller // or wider than the pane itself ScrollPane sp = new ScrollPane(); Label biglb = new Label("This is a really long label with a very large font size"); biglb.setFont(new Font("Serif", Font.BOLD, 40)); sp.add(biglb); add(sp); // TextField and TextArea extend from the TExtComponent class TextField tf1 = new TextField(15); tf1.setBackground(Color.black); tf1.setForeground(Color.white); tf1.setText("This is a TextField"); add(tf1); TextArea ta1 = new TextArea(5,20); ta1.setBackground(Color.black); ta1.setForeground(Color.white); ta1.setText("TextArea Line one\nTextArea Line two\nTextArea Line three\n" +"TextArea Line four\nTextArea Line five\n"); add(ta1); } public void paint(Graphics g) {} public void actionPerformed(ActionEvent ae) { repaint(); } }

More Related