.


:




:

































 

 

 

 


P 2 Java 5




[public] interface [extends ]

:

public interface Product

{

static final String MAKER = Cisco Corp.;

static final String Phone = 555-123-4567;

public int getPrice(int id);

}
java.applet:

public interface AudioClip

{ /** Starts playing the clip.

Each time this method is called,

the clip is restarted from the beginning. */

void play();

/** Starts playing the clip in a loop.

*/

void loop();

/** Stops playing the clip.

*/

void stop();

}
, , .
, , , :
interface Monitored extends java.lang.Runnable, java.lang.Clonable

{

boolean IsRunning();

}
, , , , . , , , . , :

class Shoe implements Product

{ public int getPrice(int id)

{

if (id==1) return 5;

else return 10;

}

public String getMaker()

{

return MAKER;

}

}
, , Java, ' . java.awt (Abstract Window Toolkit), 2.3.

TextComponent
TextField
TextArea
Container
Panel
Window
Frame
Dialog
File Dialog
Applet
Component
Canvas  
List
Label
Button
Checkbox
Choice
Scrollbar
Menu Component
MenuBar
MenuItem
Menu
CheckboxMenuItem

 


2.3 java.awt

, awt? Java, (GUI). Component. MenuComponent. : Button (), Checkbox ( ), Choice ( Windows), Label (), List ( Windows) Scrollbar ( ). , Component .

java.awt , . Panel . - Container, -, . ³ Window, . : Dialog, , Frame Windows. TextComponent TextField ( Windows) TextArea. Canvas. , 䳿 .

³ Component , ' . , , ( 3):

 

2.3

Component

   
getFont() setFont() getFontMetrics()
setForeground() getForeground()
setBackground() getBackground()
preferredSize() minimumSize() ,
resize() size()
show() hide()
isVisible() isShowing() true, , false,
disable() enable()
isEnable() true,
paint() update() repaint()
handleEvent() action()
keyDown() keyUp()

Label ():

Label Java- . ᒺ :

Label(); //

Label(String str); // ,

Label(String str, int align); //

align : Label.LEFT, Label.CENTER, Label.RIGHT.
Label 4.

2.4

Label

SetText(String str)
setAlignment(int align)
String getText()
int getAlignment()

.

Label MyLabel1 = new Label( , Label.CENTER);

Label MyLabel2 = new Label( );

Button ():

Button . . , :

Button(); //

Button(String str); //


:

setLabel() ;

getLabel() .
. action():

public boolean action(Event evt, Object wA);
evt , (evt.target ᒺ , evt.when 䳿,...);
wA .
:

...

public class Example1 extends Applet

{

public void init()

{

add(new Button(Red);

add(new Button(Green);

}

public Boolean action(Event e, Object wA)

{

if (!(e.target instanceof Button))

// ,

return false;

if ((String) wA = Red) // ?

setBackground(Color.red);

else

setBackground(Color.green);

return true;

Checkbox ( ):
Checkbox . , : "" "". , . ( false).

CheckboxGroup, (radio button), , . - , ( , ).
:

Checkbox(); //

Checkbox(String str); //

Checkbox(String str, CheckboGroup group, boolean initState);

/* , ( , , null */
2.5.

2.5

Checkbox

boolean getState() Checkbox,
getCurrent() CheckboxGroup, ,
SetCurrent() ( CheckboxGroup)
setLabel() getLabel()

. action(), wA ᒺ boolean, true, , false .
.
import java.awt.*;
import java.applet.*;
public class ExCheckbox extends Applet {

Checkbox rbBlue, rbWhite;

CheckboxGroup gr;

public void init() {

add (new Checkbox("Red"));

add (new Checkbox("Green"));

gr = new CheckboxGroup();

Checkbox rbBlue = new Checkbox("Blue",gr,true);

Checkbox rbWhite = new Checkbox("White",gr,false);

add(rbBlue);

add(rbWhite);

}

public boolean action(Event e, Object wA) {

if (e.target instanceof Checkbox)

{

Checkbox cur = (Checkbox) e.target;

if (cur.getLabel() == "Red" && cur.getState())

setBackground(Color.red);

if (cur.getLabel() == "Green" && cur.getState())

setBackground(Color.green);

if (cur.getLabel() == "Blue" && cur.getState())

setBackground(Color.blue);

if (cur.getLabel() == "White" && cur.getState())

setBackground(Color.white);

return true;

}

return false;

}

}
Choice (, ):

, , Choice.
Choice();
Choice 2.6:

2.6

Choice

addItem(String str)
select(int n)
select(String str)
int countItems()
int getSelectIndex() ( 0)
String getItem(int n)
String getItem()

, . action(), wA .
.
import java.awt.*;
import java.applet.*;
public class ExChoice extends Applet
{ Choice ch;

public void init()

{ ch=new Choice();

ch.addItem("Red");

ch.addItem("Green");

ch.addItem("Blue");

ch.addItem("White");

add(ch);

}

public boolean action(Event e, Object wA)

{

if (e.target instanceof Choice)

{

Choice cur = (Choice) e.target;

switch (cur.getSelectedIndex()) {

case 0: setBackground(Color.red); break;

case 1: setBackground(Color.green); break;

case 2: setBackground(Color.blue); break;

case 3: setBackground(Color.white); break;

} return true;

} return false;

}

}

List ():

List () Choice, , , , . - , , <Enter>. , .
List 2.7.

2.7

List

addItem(String str) ;
addItem(String str, int index) index ( index = -1, );
replaceItem(String str, int index)
delItem(int index)
delItems(int start, int end) , start end;
getItem(int n)
clear() ( )
select(int n)
deselect(int n)
isSelected(int n) true, , false
countItems()
getRows()
getSelectedIndex() ; -1, ( )
getSelectedItem() ( );
int[] getSelectedIndexes() ( );
String[] getSelectedItems() ( )
allowsMultipleSelections() true,
setMultipleSelections()
makeVisible(int n)
getVisibleIndex() , makeVisible()

' List . , addItem(). . . , .
:

List (); //

List (int row, boolean mult); // ,

// row ,

// , mult -

. List action().
handleEvent(Event evt);
evt.id , :
LIST_SELECT ;
LIST_DESELECT ;
evt.arg , .
.
import java.awt.*;
import java.applet.*;
public class ExList extends Applet

{ List ch;

public void init()

{ ch=new List();

ch.addItem("Red");

ch.addItem("Green");

ch.addItem("Blue");

ch.addItem("White");

ch.addItem("Cyan");

add(ch);

}

public boolean handleEvent(Event e)

{ if (e.target == ch)

{ if (e.id==Event.LIST_SELECT)

{

Integer sel=(Integer) e.arg;

switch (sel.intValue())

{

case 0: setBackground(Color.red); break;

case 1: setBackground(Color.green); break;

case 2: setBackground(Color.blue); break;

case 3: setBackground(Color.white); break;

case 4: setBackground(Color.cyan); break;

}

}

return true;

}

return false;

}

}

, TextField TextArea, TextComponent, . : (TextField) (TextArea). ' : TextField TextArea:

:
TextField(); //

TextField(int); //
TextField(String); //
TextArea(); //
TextArea(int, int); //
TextArea(String); //
TextArea(String, int, int); // /
TextField TextArea 2.8.

2.8

TextField TextArea

getText()
setText(String) ;
select(int, int) ;
selectAll()
getSelectedText() ;
SetEditable(Boolean)
isEditable() ,
getSelectionStart()
getSelectionEnd()
getColumns() ( !)

. List, TextArea action(). 䳿 䳿 , , , , . ϳ getText() .

TextField . action(), , <Enter>.
-, Java (layout manager). ³ ' , . , , , setLayout(), :
setLayout(new BorderLayout());
FlowLayout ( ) - , . , .
:

FlowLayout(); //

FlowLayout(int align); //

FlowLayout(int align, int horp, int verp); //

//

//

align :

FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER.

 

GridLayout . . .

:
GridLayout(int nRows, int nCols); //

//

GridLayout(int nRows, int nCols, int horp, int verp);

//

// ( )
, , . , 0. , 0. , GridLayout(3, 4) GridLayout(3, 0).
BorderLayout ( ) - 5 , . BorderLayout - add() , North, South, East, West Center. , .
:

BorderLayout(); //

BorderLayout(int horp,verp); //

//

. , .
add() :

add(int poz, Component comp);

poz , , (North, South, East, West Center).
CardLayout ( ). , ( ). , . , , . , .
GridBagLayout ( ). , . , GridLayout. , . , . . ֳ ᒺ GridBagConstraints, :

gridx, gridy , . gridx = gridy = GridBagConstraint.RELATIVE, gridx , gridy ;

gridwidth, gridheight , . 1. gridwidth = GridBagConstraint.REMAINDER gridheight = GridBagConstraint. EMAINDER, ( ). , GridBagConstraint.RELATIVE; fill , , , . ³ :

GridBagConstraint.NONE ( ) ;

GridBagConstraint.HORIZONTAL ,

GridBagConstraint.VERTICAL ,

GridBagConstraint.BOTH ;

ipadx, ipady , ( 0);

insets Insets , ( ), , , ;

anchor .
:

GridBagConstraint.NTER ( ), GridBagConstraint.NORTH, GridBagConstraint.NORTHEAST,GridBagConstraint.EAST, GridBagConstraint.SOUTHEAST, GridBagConstraint.SOUTH, GridBagConstraint.SOUTHWEST,

GridBagConstraint.WEST, GridBagConstraint.NORTHWEST;





:


: 2017-03-12; !; : 331 |


:

:

, , . , .
==> ...

1656 - | 1494 -


© 2015-2024 lektsii.org - -

: 0.088 .