Small Piece of Java AWT class hierarchy

weaverchurchΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 11 μήνες)

301 εμφανίσεις

Day 25:
Small Piece of Java AWT Class Hierarchy


class java.lang.Object

class java.awt.Component

class java.awt.Button

class java.awt.Checkbox

class java.awt.Container

class java.awt.Panel

class java.awt.Window

class java.awt.Dialog

class java.awt.Frame

class java.awt.Label

class java.awt.List

class java.awt.TextComponent

class java.awt.TextArea

class java.awt.TextField

class java.awt.Event

class java.awt.Font

class java.awt.Graphics


NetBeans Help: View / Documentation Indices / Java 2 Platform...


C
alling an Applet from an HTML file:

<applet code="MyApplet.class" width=800 height=600>

</applet>


Java Applets
:

import java.applet.Applet;

-

embedded in another application (not run on its own)

-

must be superclass of applet embedded in a Web page

-

subclass

of Panel (which was a subclass of Container)


Container:


import java.awt.*;

-

component that can contain other AWT components


Component:


import java.awt.*;

-

interacts with the user
,

has graphical representation
,

displayed on the screen

-

abstract supercl
ass of the nonmenu
-
related AWT components

-

Examples: buttons, checkboxes, text fields

-

Has methods for sizing and moving

-

public void paint(Graphics

g) // Paint
s this component


don’t call directly

-

void repaint();

//

Requests
this component

be repainted

Graphics:


import java.awt.*;

-

abstract base class for all graphics contexts

-

allow an application to draw onto components

-

encapsulates state information needed for the basic rendering

color, font, object on which to draw, etc.

-

public void drawLine(int

x1,
int

y1, int

x2, int

y2);

-

void drawString(String

str, int

x, int

y);

-

void drawOval(int

x, int

y, int

width, int

height); // x,y: upper
-
left

-

void setColor(Color

c);

// set current drawing color

-

void setFont(Font

font);


TextField:


import java.awt.*;

-

compo
nent that allows for the editing of a single line of text

-

String getText();

// returns current text in the text field

-

void setText(String

t);

// set text for the text field


List
:


import java.awt.*;

-

List of String: General “container” but NOT an AWT Cont
ainer

-

void add(String item)



adds an item to the end of the scrolling list

-

int getSelectedIndex()



g
ets the index o
f the selected item on the list

-

void remove(int position)



r
emoves the item at the specified position
index


ActionListener:


import java.awt.event.*;

-

interface for receiving action events



used by Buttons, Timers

-

void actionPerformed(ActionEvent

e); // must implement this

-

public class SimulationApplet extends java.applet.Applet


implements java.awt.event.ActionListener

-

I
n NetBeans:
Insert Code
-
> Implement Method, Check actionPerformed


ActionEvent:

import java.awt.event.*;

-

event which indicates that a component
-
defined action occurred

-

Object getSource();




// Returns reference to initiating Object

-

String ge
tActionCommand(); // returns the command string



javax.swing.Timer
:


Not AWT, in the javax.swing

-

Fires action events after a specified delay

-

private javax.swing.Timer timer = new javax.swing.Timer(500, this)
;

-

First parameter is timeout in milliseconds,

second is an ActionListener

-

Methods to start, stop, change timeout time


Ex
ample
of ActionListener for
Applet
or Frame
:



Declared in the Applet or Frame class
:



private Button myButton;



Embedded class in the Applet or Frame:



class MyButtonListe
ner implements ActionListener


{


public void actionPerformed(ActionEvent event)


{


if ( event.getSource() == myButton )


{


. . . Put code here to handle button click


}


}


}



In the init for Applet

or constructor for the Frame:



myButton = new Button(

“Click Me!”
);



MyButtonListener buttonListener = new MyButtonListener();


myButton.addActionListener(buttonListener);



add(myButton); // add the Button to the Applet

or Frame





Note that Net Beans generates all this for you.


When you drag a button onto a form in design mode and double
-
click the button, i
t make
s

a
method
such as

myButtonActionPerformed
for you to type your code into:



private void myButtonActionPerformed(jav
a.awt.event.ActionEvent evt)

{

// TODO add your handling code here:

}


But it makes all the other stuff shown above and buries it in the “hidden” parts, i.e.,
initComponents. It makes an anonymous ActionListener class and puts a call to the
myButtonAct
ionPerformed

method in the
actionPerformed method

of the anonymous
ActionListener class
.

By anonymous class, I mean that it declares and instantiates the class right
in the call, where the “
. . .


is:
myButton.addActionListener( . . . )