.


:




:

































 

 

 

 


2 4




11.5.

?

import java.awt.*; class Quest5 { public static void main(String[] args) { Component b = new Button(""); System.out.print(((Button) b).getLabel()); } }

1) ;

2) ;

3) : Quest5 Applet;

4) : Component Button;

5) .

 
12

( , .) , . , Java 2, . (EventListener), , . , , , , , , . - .

Listener , , java.awt.event. , , . . - .

. add Listener( _ );

- (Listener) . , . remove Listener().

: (JButton, JCheckbox, JRadioButton), , -. , mouse- key-. , , . - add Listener()

, . - , , , . . , , .

, , EventListener . , . , , :

ActionListener actionPerformed(ActionEvent e)
AdjustmentListener adjustmentValueChanged( AdjustmentEvent e)
ComponentListener componentResized( ComponentEvent e) componentMoved(ComponentEvent e) componentShown(ComponentEvent e) componentHidden(ComponentEvent e)
ContainerListener componentAdded(ContainerEvent e) componentRemoved( ContainerEvent e)
FocusListener focusGained(FocusEvent e) focusLost(FocusEvent e)
ItemListener itemStateChanged(ItemEvent e)
KeyListener keyPressed(KeyEvent e) keyReleased(KeyEvent e) keyTyped(KeyEvent e)
MouseListener mouseClicked(MouseEvent e) mousePressed(MouseEvent e) mouseReleased(MouseEvent e) mouseEntered(MouseEvent e) mouseExited(MouseEvent e)
MouseMotionListener mouseDragged(MouseEvent e) mouseMoved(MouseEvent e)
TextListener textValueChanged(TextEvent e)
WindowListener windowOpened(WindowEvent e) windowClosing(WindowEvent e) windowClosed(WindowEvent e) windowIconified(WindowEvent e) windowDeiconified(WindowEvent e) windowActivated(WindowEvent e)

, , . EventObject java.util. : getSource(), , toString(), . AWTEvent java.awt AWT-, . getID() , . , AWTEvent, java.awt.event:

ActionEvent : ; ; ;

AdjustmentEvent ;

ComponentEvent , , , ;

FocusEvent , ;

TextEvent ;

ItemEvent .

InputEvent ( ). KeyEvent, MouseEvent.

- , , , KeyListener. KEY_PRESSED. keyPressed(). , KEY_RELEASED keyReleased(). , KEY_TYPED keyTyped().

- addListener(KeyListener el ), . el .

/* # 1: : MyKey.java */

package chapt12;

import java.awt.*;

import java.awt.event.*;

import javax.swing.JApplet;

 

public class MyKey extends JApplet {

private String msg = " ";

private int x = 0, y = 20; //

 

private class AppletKeyListener

implements KeyListener {

// KeyListener

public void keyPressed(KeyEvent e) {

showStatus ("Key Down");

} //

public void keyReleased(KeyEvent e) {

showStatus("Key Up");

} //

public void keyTyped(KeyEvent e) {

msg += e.getKeyChar();

repaint(); //

}

}

public void init() {

/* */

addKeyListener(new AppletKeyListener());

requestFocus(); //

}

public void paint(Graphics g) {

//

g.drawString(msg, x, y);

}

}

. 12.1.

( , ) keyTyped(), keyPressed().

init() AppletKeyListener. , KeyListener.

MouseListener MouseEvent.

/* # 2: : MyRect.java */

package chapt12;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MyRect extends JApplet {

private Rectangle rect =

new Rectangle(20, 20, 100, 60);

private class AppletMouseListener //

implements MouseListener {

/* MouseListener */

public void mouseClicked(MouseEvent me) {

int x = me.getX();

int y = me.getY();

if (rect.contains(x, y)) {

showStatus(

" ");

} else {

showStatus(" ");

}

}

//

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

public void init() {

setBackground(Color. WHITE);

/* */

addMouseListener(new AppletMouseListener());

}

public void paint(Graphics g) {

g.setColor(Color. BLUE);

g.fillRect(rect.x, rect.y,

rect.width, rect.height);

}

}

. 12.2.

Swing - ( ) ( , ). , .

JButton , . addActionListener() JButton. ActionListener actionPerformed(), : , .

/* # 3: , ActionEvent:

SimpleButtonAction.java */

package chapt12;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class SimpleButtonAction extends JApplet {

private JButton additionBtn = new JButton("");

private JTextField txtField1 = new JTextField(3);

private JTextField txtField2 = new JTextField(3);

private JLabel answer = new JLabel();

 

private class ButtonListener

implements ActionListener {

// -

public void actionPerformed(ActionEvent ev) {

try {

int t1, t2;

t1 = Integer. parseInt (txtField1.getText());

t2 = Integer. parseInt (txtField2.getText());

answer.setText(": " + (t1 + t2));

showStatus(" !");

} catch (NumberFormatException e) {

showStatus(" !");

}

/*

* String s1, s2;

* s1 = ((JButton)ev.getSource()).getText();

*/

//

// s2 = ev.getActionCommand();

/*

* ,

* if (ev.getSource() == additionBtn)

*

*

*/

}

}

public void init() {

Container c = getContentPane();

setLayout(new FlowLayout()); /*

*/

c.add(txtField1);

c.add(txtField2);

//

additionBtn.addActionListener(

new ButtonListener());

c.add(additionBtn);

c.add(answer);

}

}

. 12.3.

JButton , . JButton - , . init() add() Container.

-

, , . , -. , . , , .

, MouseMotionAdapter :
mouseDragged()
mouseMoved(). , MouseMotionListener. , MouseMotionAdapter mouseDragged() . mouseMoved(), .

, , . , , , . main(), . GUI, . , .

(), . , . , .. , . :

paint() update();

getGraphics() Component.

FocusEvent , . WindowEvent , .

, . PaintEditor main(). JColorChooser.

/* # 4: : PaintEditor.java */

package chapt12;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class PaintEditor extends JFrame {

private int prevX, prevY;

private Color color = Color. BLACK;

private JButton jButton =

new JButton("ColorChooser");

 

public PaintEditor() {

Container c = getContentPane();

c.setLayout(new FlowLayout());

c.add(jButton);

 

jButton.addActionListener(

new ButtonActionListener());

addMouseListener(new PaintMouseAdapter());

addMouseMotionListener(

new PaintMouseMotionAdapter());

}

private class ButtonActionListener

implements ActionListener {

public void actionPerformed(ActionEvent e) {

color = JColorChooser.

showDialog (((Component) e.getSource())

.getParent(), "Demo", color);

}

}

private class PaintMouseAdapter extends MouseAdapter{

public void mousePressed(MouseEvent ev) {

setPreviousCoordinates(

ev.getX(), ev.getY());

}

}

private class PaintMouseMotionAdapter

extends MouseMotionAdapter {

public void mouseDragged(MouseEvent ev) {

Graphics g = getGraphics();

g.setColor(color);

g.drawLine(

prevX, prevY, ev.getX(), ev.getY());

setPreviousCoordinates(

ev.getX(), ev.getY());

}

}

public void setPreviousCoordinates(

int aPrevX, int aPrevY) {

prevX = aPrevX;

prevY = aPrevY;

}

public static void main(String[] args) {

PaintEditor pe = new PaintEditor();

pe.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent ev) {

System. exit (0);

}

});

pe.setBounds(200, 100, 300, 200);

pe.setTitle("MicroPaint");

pe.setVisible(true);

}

}

. 12.4. -

PaintEditor
addMouseListener(new PaintMouseAdapter(this))
addMouseMotionListener(new PaintMouseMotionAdapter(this)) . PaintEditor , . , ,
PaintMouseAdapter PaintMouseMotionAdapter. MouseAdapter , , : mousePressed(MouseEvent e), mouseReleased(MouseEvent e). MouseMotionAdapter , , : mouseDragged(MouseEvent e), mouseMoved(MouseEvent e).

PaintEditor WindowEvent. WindowEvent, PaintEditor , WindowClosing. , PaintEditor . , .

WindowAdapter . : windowActivated(WindowEvent e), ; windowlosing(WindowEvent e), , .

12

1. . .

2. . .

3. . ( ), . .

4. . ( ), . .

5. , .

6. , (JLabel) , (JTextField), .

7. , , .

8. . , . .

9. . q, t q(1 + cos(wt))/2, w . .

10. . ( ) , .. q.

11. . .

12. , .

13. C , , , .

14. C , . . , .

12

12.1.

.

1) Applet main();

2) Applet ;

3) Applet paint();

4) Applet init();

5) Applet, public.

12.2.

:

import java.awt.*;
public class Quest2 extends Frame{ public static void main(String[] args){ Quest2 fr = new Quest2(); fr.setSize(222, 222);
fr.setVisible(true);
} }

?

1) fr.setBackground(Color.white);

2) fr.setColor(Color.white);

3) fr.Background(Color.white);

4) fr.color=Color.White;

5) fr.setColor(0,0,0).

12.3.

?

import java.awt.*;
import java.awt.event.*;
public class Quest3 extends Frame implements WindowListener { public Quest3(){
setSize(300,300);
setVisible(true);
} public void windowClosing(WindowEvent e){
System.exit(0);
}
public static void main(String args[]){
Quest3 q = new Quest3();
} }

1) ;

2) ;

3) ;

4) .

12.4.

-?

1) WindowAdapter;

2) WindowsAdapter;

3) AdjustmentAdapter;

4) ItemAdapter;

5) FocusAdapter.

12.5.

Event Listener.

1) MouseMotionListener;

2) WindowListener;

3) KeyTypedListener;

4) ItemsListener.

 
13

Java (1.0.x) AWT, , , , , . , java.awt.Component , - . write once, run everywhere ( , ) ( Swing), . JDK, JFC (Java Foundation Classes). JDK AWT , JavaSoft, JDK, . Swing, JFC JavaBeans, , .

(, ) . , , . , , , . , , .

FlowLayout, BorderLayout, GridLayout, CardLayout, BoxLayout, LayoutManager, .

FlowLayout . , , . . AWT FlowLayout , :





:


: 2016-04-03; !; : 799 |


:

:

, .
==> ...

1613 - | 1391 -


© 2015-2024 lektsii.org - -

: 0.158 .