C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
Applets, AWTS CompSci 230 Software Construction.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Chapter 13 Java AWT – Part II (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Session 9 CannonWorld, Event-driven Programming, and Border Layout.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
Session 10 CannonGame and Event-driven Programming.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE Java: An Eventful Approach An innovative.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
Graphical User Interfaces (GUI). PART ONE About GUI’s.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Graphical User Interface (GUI)
C13b, AWT cont.. Panel Container that acts like a Component Can be added into other components (like Frames, other Panels) private Panel makeScrollBars()
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 5
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
A Quick Java Swing Tutorial
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Welcome To java
Aum Amriteshwaryai Namah
Advanced GUIs II CS Lecture
Abstract Window ToolKit (AWT)
CSE 114 – Computer Science I Event Programming
Chap 7. Building Java Graphical User Interfaces
JAVA AWT.
AWT.
Components.
AWT Components and Containers
GUI building with the AWT
Graphics Programming - Frames
Border Layout, using Panels, Introduction to PinBallGame
Programming Graphical User Interface (GUI)
GUI building with the AWT
AWT Components Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e.
Advanced GUIs II CS Lecture
Presentation transcript:

C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems (uses polymorphism)

AWT class hierarchy Object Component Button TextComponent Checkbox Choice Container Label List Scrollbar Canvas TextArea TextField Panel Window ScrollPane Dialog Frame

(some) Component methods setEnabled(boolean) setLocation(int,int),getLocation() setSize(int,int),getSize() setVisible(boolean) setForeground(Color),getForeground() setBackground(Color),getBackground() setFont(Font),getFont() repaint(Graphics), paint(Graphics) addMouseListener(MouseListener) addKeyListener(KeyListener)

Some AWT classes Component: 2D screen for user interaction Container: component that can nest other components within itself: –setLayout(LayoutManager) –add(Component),remove(Component) Window: a Container, that can be displayed, can stack windows: –show() –toFront() –toBack()

Frames Frame: a window with title bar, menu bar, cursor, border, … –setTitle(String),getTitle() –setCursor(int) –setResizable() –setMenuBar(MenuBar) Application class CannonWorld (from ch.6) uses: –setTitle(String) inherited from Frame –setSize(int,int) from Component –show() from Window –repaint() from Component –paint() overridden (inherited from Component)

Polymorphism again: Parent class code (Component, Window, …) written WITHOUT any reference to a particular application Application: simple override necessary bits and pieces (e.g. paint(), listeners) to define application-specific behavior Reuse: combine inheritance, overriding, and polymorphism

Layout manager holds Container LayoutManager inherits implements Application GridLayout Again: combine inheritance, composition, interface implementation

5 standard LayoutManager types BorderLayout: max. 5 components GridLayout: rectangular array of equal- sized components FlowLayout: place components left-to- right, top-to-bottom, variable-sized CardLayout: stack components vertically, only one visible at a time GridBagLayout: non-uniform grid of squares, most flexible

LayoutManager code examples Panel p = new Panel(); p.setLayout(new GridLayout(4,4,3,3)); p.add(new ColorButton(Color.black,”black”)); CardLayout lm = new CardLayout(); Panel p = new Panel(lm); p.add(“One”, new Label(“Number one”)); p.add(“Two”, new Label(“Number two”)); … lm.show(p,”Two”);

User interface components Label: Label lab = new Label(“score: 0 to 0”); add(“South”, lab); getText(), setText(String) Canvas: –Simple component, can be target for drawing operations (see ScrollPane example)

Buttons and ActionListeners Button b = new Button(“do it!”); b.addActionListener(new DoIt()); … private class DoIt implements ActionListener { public void actionPerformed( ActionEvent e) { // whatever }} Alternative: inheritance + interface implementation: private class ColorButton extends Button implements ActionListener { private Color c; public ColorButton(Color c1, String name) { super(name); c = c1; addActionListener(this); //  OURSELVES !!! } public void actionPerformed(ActionEvent e) { setFromColor(c);}}

Abstract ButtonAdapter class abstract class ButtonAdapter extends Button implements ActionListener { public ButtonAdapter(String name) { super(name); addActionListener(this); } public void actionPerformed(ActionEvent e) {pressed();} public abstract void pressed(); } Use with anonymous class: Panel p = new Panel(); p.add(new ButtonAdapter(“Quit”) {public void pressed() {System.exit(0);}} );

Scrollbar A slider to specify integer values Same trick as above: private class ColorBar extends Scrollbar implements AdjustmentListener { public ColorBar(Color c) { super(Scrollbar.VERTICAL,40,0,0,255); setBackground(c); addAdjustmentListener(this); }//  ourselves!! public void adjustmentValueChanged(AdjustmentEvent e) { setFromBar(); }} // possibly using getValue()

Text components TextField: fixed-sized block TextArea: uses scrollbars for text larger than the area setText(String), getText() append(String) for TextArea only As always need listener: interface TextListener extends EventListener { public void textValueChanged(TextEvent e); }

Checkbox Maintain/display labeled binary state (on/off, yes/no) getLabel(), setLabel(String),getState(), setState(String) And again, a listener: –An ItemListener for an ItemEvent

Checkbox code example class CheckTest extends Frame { private Checkbox cb = new Checkbox(“off”); public static void main(String[] args) {CheckTest w = new CheckTest(); w.show();} public CheckTest() { setTitle(“CheckBox”); setSize(300,70); cb.addItemListener( new CheckListener()); add(“Center”, cb); } private class CheckListener implements ItemListener { public void itemStateChanged(ItemEvent e) { cb.setLabel((cb.getState()) ? “ON” : “OFF”); }}}

Checkbox groups, Choices, Lists Select one of a number of possibilities –CheckboxGroup (radio buttons): only one active, use for small groups (<5) –Choice: display current selection, pop-up menu for alternatives –List: display some alternatives class ChoiceTest extends Frame { public static void main(String[] args) { ChoiceTest w = new ChoiceTest(); w.show();} private String[] c = {“One”, …, “Ten”}; private Label display = new Label(); private Choice theC = new Choice(); private List theL = new List(); private CheckboxGroup theG = new CheckboxGroup(); private ItemListener theListener = new ChoiceListener();

public ChoiceTest() { setTitle(“Selection example”); setSize(300,300); for(int i = 0; i<10; i++) { theC.addItem(c[i]); theL.addItem(c[i]); } theC.addItemListener(theListener); theL.addItemListener(theListener); add(“West”,makeCheckBoxes()); add(“North”,theC);add(“East”,theL),add(“South”,display); } private class ChoiceListener implements ItemListener { public void itemStateChanged(ItemEvent e) { display.setText(theG.getSelectedCheckboxGroup().getLabel() + theL.getSelectedItem() + theC.getSelectedItem()); }} private Panel makeCheckBoxes() { panel p = new Panel( new GridLayout(5,2)); for(int i = 0; i < 10; i++) { Checkbox cb = new Checkbox(c[i], theG, false); cb.addItemListener(theListener); p.add(cb); } return p; }}