keyDown()
method to get a key.public boolean keyDown(Event e, int key) { ... char c = (char)
key; ... }
mouseUp()
, mouseDown()
and
mouseDrag()
methods to get the position of mouse clicks and
movements.repaint()
at the end to update
the graphics screen.MouseListener
must define all its
methods:mousePressed(), mouseReleased(), mouseClicked()
are called
when the mouse clicks an item.mouseEntered(), mouseExited()
are called when the mouse
moves over an item.MouseEvent
object as its argument.MouseMotionListener
, define mouseDragged()
.repaint()
method to request the window to
repaint itself, since repainting is not automatic.paint()
method should store everything that needs to be
redrawn.keyPressed()
is called when an action key is pressed:keyTyped()
is called when another key is pressed.keyReleased()
is called when any key is released.KeyEvent
object which has
these methods:getKeyText(), getKeyChar(), getKeyCode()
class MyClass extends Applet { // an applet (could be an application)
public MyClass() { // constructor
class MyListener implements ListenerInterface //
an inner class
{ public void
eventOccurred(EventClass event) { /* add event action here */ } }
MyListener listener = new MyListener();
eventSource.addAListener(listener);
}
public void paint(Graphics g) { /* paint something here */ }
}
MouseListener
you can extend
MouseAdapter
.java.awt | javax.swing | Description |
---|---|---|
Label | JLabel | displays a line of text; not directly editable by the user |
TextField | JTextField | displays a line of text and can be edited by the user and used for input |
TextArea | JTextArea | displays several lines of text; can be edited and used for input |
Button | JButton | a rectangular push button that starts an action when clicked |
Checkbox | JCheckBox | a small square button that can be selected or unselected |
CheckboxGroup | JRadioButton | a group of radio buttons (small round buttons to select only one option) |
List | JList | a scrolling list of selectable text items; click to select one or more |
Choice | a drop-down list of text items; click to select one | |
MenuBar, Menu | JMenuBar, JMenu | used to create menus at the top of the frame |
Scrollbar | JScrollBar | a vertical or horizontal slider with arrows at each end |
Canvas | area for catching user events e.g. for drawing | |
JComboBox | combines a text box with a pull-down list of choices (for easier data entry) |
Button okButton = new button("OK");
// create an OK buttonCheckbox bold = new Checkbox("Bold");
// create a checkbox
with label Bold, uncheckedCheckbox italics = new Checkbox("Italics", cg, true);
// a
checkbox with label Italics, CheckboxGroup cg, checkedList colorList = new List(3, true);
// create a
multiple-select list with three items visiblecolorList.add("Red"); colorList.add("Green"); colorList.add("Blue");
// add items to the listLabel myLabel = new Label("Hello");
TextField username = new TextField("", 14);
// create
an empty text field with width 14 charactersTextArea message = new TextArea("Type your message here.", 60, 10);
actionPerformed()
method defines the action to perform
when the button is clicked.getState()
returns whether the box is
currently checked (true or false).void setEditable(true)
// allows the user to type and
change the text; default is not editableString getText()
// returns the text in the fieldvoid setText(String s)
// puts the string s in the text
fieldvoid add(String s)
// add an item to the listvoid addItemListener(Applet a)
// add an item event handler
to the appletString getSelectedItem()
// return the text of the selected
itemint getSelectedIndex()
// return the index of the selected
itemString getItem(int i)
// return item with index iresize(w,h)
, pack()
,
show()
and hide()
.A layout manager is an object which arranges components in a container.
Layout Manager | Description |
---|---|
FlowLayout | place elements left to right on one row; put on next row if not enough
space (the easiest layout) alignment can be FlowLayout.LEFT, CENTER or RIGHT |
BorderLayout | place elements in predefined areas; center area expands to fill extra
space areas are BorderLayout.NORTH, SOUTH, EAST, WEST, CENTER |
GridLayout | place elements in rows and columns; all elements take an equal amount of space |
CardLayout | place elements in the same space; show only one at a time to save space (e.g. tabbed window or photo browser) |
GridBagLayout | a layout which allows precise positioning (the most complex layout) |
BoxLayout | place elements in one row or one column (javax.swing) |
OverlayLayout | (javax.swing) |
Components are usually set up in the init()
method of the
applet.
FlowLayout fl = new FlowLayout (FlowLayout.CENTER);
//
centered flow layoutBorderLayout bl = new BorderLayout (10, 20);
// horizontal
spacing 10 pixels, vertical spacing 20 pixelsGridLayout gl = new GridLayout (3, 4);
// 3 rows and 4
columnssetLayout(f)
;add(okButton); add(cancelButton);