Professional Documents
Culture Documents
12
12
Java
12.1
3
Event
MouseEvent ActionEvent
Event Source Checkbox
Event handler
ActionEvent
12-1
Java Delegation
Model Panel
y 213 y
Java
12.1 ButtonHandler
ActionListener ActionEvent
//
12.1
EventManagerDemon.java
//
import java.awt.*;
import java.awt.event.*;
public class EventManagerDemon {
public static void main(String[ ] args) {
final Frame f = new Frame("Test");
// f
Button b = new Button("Press Me!");
// b
//
b.addActionListener(new ButtonHandler());
f.setLayout(new FlowLayout());
// FlowLayout
f.add(b);
// b
f.setSize(200,100);
//
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt) {
f.setVisible(false);
// f
f.dispose();
//
System.exit(0);
//
}
});
f.setVisible(true);
//
}
}
//ButtonHandler ActionListener ActionEvent
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e)
//ActionEvent
{
System.out.println("");
// actionPerformed
}
}
12-2
EventManagerDemon.java
12-3 EventManagerDemon.java
y 214 y
12
*Event
*Listener
add*Listener(*Listener)
12.2
Java
12.2.1
AWT
ActionEvent AdjustmentEvent
ItemEvent
ComponentEvent
ContainerEvent
WindowEvent
FocusEvent
KeyEvent
MouseEvent
ActionEvent
AdjustmentEvent
ItemEvent
TextEvent
y 215 y
Java
API
12.2.2
ActionEvent ButtonCheckBox
TextField ButtonTextField
ActionListenerActionListener
actionPerfomed()
Public void actionPerformed(ActionEvent e){
//
}
ActionEvent
getActionCommand()
getModifiers()
getWhen()
paramString()
button
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// dosomething
}
}
12.2.3
ScrollBar
ScrollBar
AdjustmentListener
public void adjustmentValueChanged(AdjustmentEvent e)
{
//
}
AdjustmentEvent
y 216 y
getAdjustable() Adjustable
getAdjustmentType()
getValue()
12
getValueIsAdjusting()
paramString()
scrollbar
scrollbar.addAdjustmentListener(new AdjustmentListener()
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
// do someting
}
});
//
12.2
AdjustmentEventDemo.java
AdjustmentEvent
//
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AdjustmentEventDemo extends Frame {
// AdjustmentEventDemo
public AdjustmentEventDemo ()
{
super();
//
init();
// init
}
public static void main(String args[ ])
{
new AdjustmentEventDemo();
// AdjustmentEventDemo
}
Scrollbar slider;
// slider
TextField value;
// TextField value
Label label;
// Label label
public void init( ) {
setLayout(new GridLayout(1, 3));
// GridLayout
slider = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,100);
// slider
// AdjustmentListener
slider.addAdjustmentListener(new AdjustmentEventHandler());
value = new TextField("0",5);
//
value.setEditable(false);
//
label = new Label("0~100");
// label
label.setBackground(Color.cyan);
//
add(label);
//
add(slider);
//
add(value);
//
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt) {
setVisible(false);
// f
dispose();
//
System.exit(0);
//
}
});
y 217 y
Java
setSize(300, 50);
//
setVisible(true);
//
}
class AdjustmentEventHandler implements AdjustmentListener
{
// adjustmentValueChanged
public void adjustmentValueChanged(AdjustmentEvent eve) {
value.setText(Integer.toString(((Scrollbar)eve.getSource()).getValue()));// value
}
}
}
12.2.4
FocusEvent
FocusListener
void focusGained(FocusEvent e)
void focusLost(FocusEvent e)
FocusEvent
getOppositeComponent()
isTemporary()
paramString() toString()
textfield
textfield.addFocusListener(new FocusListener()
public void focusGained(FocusEvent e)
{
// do somthing
}
public void focusLost(FocusEvent e)
{
// do somthing
}
y 218 y
12
});
//
12.3
FocusEventDemo.java
FocusEvent
//
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FocusEventDemo extends Frame {
// FocusEventDemo
public FocusEventDemo ()
{
super();
//
init();
// init
}
public static void main(String args[ ])
{
new FocusEventDemo();
// FocusEventDemo
}
TextArea textarea;
// TextArea
TextField textfield;
// TextField
public void init( ) {
setLayout(new GridLayout(2, 1));
// GridLayout
textarea = new TextArea();
// TextArea textarea
textarea.addFocusListener(new FocusListener() // textarea FocusListener
{
public void focusGained(FocusEvent eve) {
textarea.setText("textarea"); // textarea
}
public void focusLost(FocusEvent eve) {
textarea.setText("textarea"); // textarea
}
});
textfield = new TextField();
// TextField textfield
textfield.addFocusListener(new FocusListener() // textfield FocusListener
{
public void focusGained(FocusEvent eve) {
textfield.setText("textfield");
// textfield
}
public void focusLost(FocusEvent eve) {
textfield.setText("textfield");
// textfield
}
});
add(textarea); // textarea
add(textfield); // textfield
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt) {
setVisible(false);
dispose();
System.exit(0);
}
// f
//
//
y 219 y
Java
});
setSize(300, 200);
setVisible(true);
}
//
//
javac class
java class
12-5
12.3 textfield textarea
textarea textfield
12-5 FocusEventDemo.java
textarea textfield
12.2.5
ItemEvent CheckBox
ComboBoxList
ItemListener
void itemStateChange(ItemEvent e)
getItem()
getItemSelectable() ItemSelectable
getStateChange() SELECTED DESELECTED
paramString()
jComboBox1
jComboBox1.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
// do somthing
}
});
//
12.4
ItemEventDemo.java
ItemEvent
//
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ItemEventDemo extends Frame implements ItemListener{
// ItemEventDemo
public ItemEventDemo()
y 220 y
12
{
super();
init();
}
public static void main(String args[ ])
{
new ItemEventDemo();
}
//
// init
// ItemEventDemo
List list; //
Checkbox checkbox1,checkbox2,checkbox3; //
Choice colorChooser;
//
TextArea textarea;
//
public void init( ) {
setLayout(new GridLayout(4, 1));
// GridLayout
textarea = new TextArea();
// textarea
add(textarea);
//
list = new List(4,false);
// 4
list.add("");
//
list.add("");
//
list.add("");
//
list.add("");
//
list.add("");
//
list.add("");
//
list.add("");
//
add(list); //
list.addItemListener(this);
// list ItemListener
Panel panel = new Panel();
// panel
CheckboxGroup cbg = new CheckboxGroup();
// cbg
checkbox1 = new Checkbox("one", cbg, true);
// checkbox1
checkbox1.addItemListener(this);
// checkbox1 ItemListener
panel.add(checkbox1);
// checkbox1
checkbox2 = new Checkbox("two", cbg, false); // checkbox2
checkbox2.addItemListener(this);
// checkbox1 ItemListener
panel.add(checkbox2);
// checkbox1
checkbox3 = new Checkbox("three", cbg, true);
// checkbox3
checkbox3.addItemListener(this);
// checkbox1 ItemListener
panel.add(checkbox3);
// checkbox1
add(panel);
//
colorChooser=new Choice();
// colorChooser
colorChooser.add("Green");
//Green colorChooser
colorChooser.add("Red");
//Red colorChooser
colorChooser.add("Blue");
//Blue colorChooser
colorChooser.addItemListener(this); // colorChooser ItemListener
add(colorChooser);
// colorChooser
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt) {
setVisible(false);
// f
dispose();
//
System.exit(0);
//
}
});
setSize(200, 300);
//
y 221 y
Java
setVisible(true);
//
}
public void itemStateChanged(ItemEvent eve) {
if (eve.getSource() == list)
{
textarea.setText(list.getSelectedItem());
}
if (eve.getSource() == checkbox1)
{
textarea.setText(checkbox1.getLabel());
}
if (eve.getSource() == checkbox2)
{
textarea.setText(checkbox2.getLabel()); /
}
if (eve.getSource() == checkbox3)
{
textarea.setText("checkbox3.getLabel()");
}
if (eve.getSource() == colorChooser)
{
textarea.setText(colorChooser.getSelectedItem());
}
}
// textarea
// textarea
/ textarea
// textarea
// textarea
javac class
java class
12-6
12.4 3 ListCheckbox
Choice 3
ItemEvent
12.2.6
12-6 ItemEventDemo.java
KeyEvent
KeyListener 3
void keyPressed(KeyEvent e)
void keyReleased(KeyEvent e)
void keyTyped(KeyEvent e)
y 222 y
getKeyChar()
getKeyCode()
getKeyLocation()
getKeyModifiersText()
getKeyText()
12
isActionKey()
setKeyChar()
setModifiers(int modifiers)
paramString()
textfield
textfield.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
// dosomething
}
public void keyReleased(KeyEvent e)
{
// dosomething
}
public void keyTyped(KeyEvent e)
{
// dosmething
}
});
//
12.5
KeyEventDemo.java
KeyEvent
//
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class KeyEventDemo extends Frame implements KeyListener{
// KeyEventDemo
public KeyEventDemo()
{
super();
//
init();
// init
}
public static void main(String args[ ])
{
new KeyEventDemo();
// KeyEventDemo
}
Button button; // button
TextArea textarea,textarea1;
// textarea,textarea1
public void init( ) {
setLayout(new GridLayout(3, 1));
// GridLayout
textarea = new TextArea();
// textarea
textarea1 = new TextArea();
// textarea1
add(textarea);
//
button = new Button(""); // button
add(button);
//
button.addKeyListener(this);
// button
y 223 y
Java
add(textarea1);
// textarea1
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt) {
setVisible(false);
// f
dispose();
//
System.exit(0);
//
}
});
setSize(200, 300);
//
setVisible(true);
//
}
public void keyPressed(KeyEvent eve) {
textarea.setText("");
// textarea
}
public void keyReleased(KeyEvent eve) {
textarea.setText("");
// textarea
}
public void keyTyped(KeyEvent eve) {
textarea1.setText(String.valueOf(eve.getKeyChar()));
// textarea1
}
}
javac class
java class
12-7
12.5
keyPressed
12-7 KeyEventDemo.java
keyTyped
keyReleased
12.2.7
MouseEvent
MouseListener 5
y 224 y
void mouseClicked(MouseEvent e)
void mouseEntered(MouseEvent e)
void mouseExited(MouseEvent e)
void mousePressed(MouseEvent e)
void mouseReleased(MouseEvent e)
12
getButton()
getClickCount()
getMouseModifiersText()
getPoint()
getX()
getY()
paramString()
panel
panel.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
// dosomething
}
public void mouseEntered(MouseEvent e)
{
// dosomething
}
public void mouseExited(MouseEvent e)
{
// dosomething
}
public void mousePressed(MouseEvent e)
{
// dosomething
}
public void mouseReleased(MouseEvent e)
{
// dosomething
}
});
//
12.6
MouseEventDemo.java
MouseEvent
//
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseEventDemo extends Frame{
// MouseEventDemo
public MouseEventDemo()
{
super();
//
init();
// init
}
public static void main(String args[ ])
{
new MouseEventDemo();
// MouseEventDemo
y 225 y
Java
}
Panel panel;
// panel
TextField textfield1,textfield2;
// textfield1,textfield2
public void init( ) {
setLayout(new GridLayout(3, 1));
// GridLayout
textfield1 = new TextField(20);
// textfield1
textfield2 = new TextField();
// textfield2
add(textfield1);
// textfield1
add(textfield2);
// textfield2
panel = new Panel();
// panel
panel.setBackground(Color.cyan); //
add(panel);
// panel
panel.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent eve) {
textfield2.setText("X="+eve.getX()+";Y="+eve.getY());// textfield2
}
public void mouseEntered(MouseEvent eve) {
textfield1.setText(""); // textfield1
}
public void mouseExited(MouseEvent eve) {
textfield1.setText(""); // textfield1
}
public void mousePressed(MouseEvent eve) {
textfield1.setText("");
// textfield1
}
public void mouseReleased(MouseEvent eve) {
textfield1.setText("");
// textfield1
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt) {
setVisible(false);
// f
dispose();
//
System.exit(0);
//
}
});
setSize(200,200);
//
setVisible(true);
//
}
}
javac class
java class
12-8
12.6 panel
MouseListener mouseClicked
mouseEnteredmouseExitedmousePressedmouseReleased
12-8 MouseEventDemo.java
y 226 y
12
12.2.8
WindowEvent Window
Window
FrameDialog WindowListener
7
void windowActivated(WindowEvent e)
void windowClosed(WindowEvent e)
void windowClosing(WindowEvent e)
void windowDeactivated(WindowEvent e)
void windowDeiconified(WindowEvent e)
void windowIconified(WindowEvent e)
void windowOpened(WindowEvent e)
getNewState()
getOldState()
getOppositeWindow()
getWindow()
paramString()
frame
frame.addWindowListener(new WindowListener()
{
public void windowActivated(WindowEvent e)
{
// dosomething
}
public void windowClosed(WindowEvent e)
{
// dosomething
}
public void windowClosing(WindowEvent e)
{
// dosomething
}
public void windowDeactivated(WindowEvent e)
{
// dosomething
}
public void windowDeiconified(WindowEvent e)
{
// dosomething
}
public void windowIconified(WindowEvent e)
{
// dosomething
}
y 227 y
Java
public void windowOpened(WindowEvent e)
{
// dosomething
}
});
//
12.7
WindowEventDemo.java
WindowEvent
//
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowEventDemo extends Frame{
// WindowEventDemo
public WindowEventDemo()
{
super();
//
init();
// init
}
public static void main(String args[ ])
{
new WindowEventDemo();
// WindowEventDemo
}
public void init( ) {
addWindowListener(new WindowListener(){
public void windowClosing(WindowEvent eve) {
str = "windowClosing";
//
System.out.println(str);
//
setVisible(false);
// f
dispose();
//
System.exit(0);
//
}
public void windowActivated(WindowEvent eve) {
str = "windowActivated"; //
System.out.println(str);
//
repaint();
//
}
public void windowClosed(WindowEvent eve) {
str = "windowClosed";
//
System.out.println(str);
//
repaint();
//
}
public void windowDeactivated(WindowEvent eve) {
str = "windowDeactivated";//
System.out.println(str);
//
repaint();
//
}
public void windowDeiconified(WindowEvent eve) {
str = "windowDeiconified"; //
System.out.println(str);
//
repaint();
//
}
public void windowIconified(WindowEvent eve) {
str = "windowIconified"; //
y 228 y
12
System.out.println(str);
repaint();
//
//
}
public void windowOpened(WindowEvent eve) {
str = "windowOpened";
//
System.out.println(str);
//
repaint();
//
}
});
setSize(200,200);
setVisible(true);
}
public void paint(Graphics g) {
g.drawString(str, 30, 100);
}
String str = null;
//
//
//
//
12-9 WindowEventDemo.java
12.3
y 229 y
Java
12.3.1
EventListener
public interface KeyListener extends EventListener
{
public void keyPressed(KeyEvent ev);
public void keyReleased(KeyEvent ev);
public void keyTyped(KeyEvent ev);
}
KeyListener 3 keyPressed()
keyReleased() keyTyped() 12-1
AWT 10
11
12-1
ActionEvent
ActionListener
actionPerformed(ActionEvent)
ItemEvent
ItemListener
itemStateChanged(ItemEvent)
MouseMotionListener
mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
mousePressed(MouseEvent)
MouseEvent
mouseReleased(MouseEvent)
MouseListener
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mouseClicked(MouseEvent)
keyPressed(KeyEvent)
KeyEvent
KeyListener
keyReleased(KeyEvent)
keyTyped(KeyEvent)
FocusEvent
focusGained(FocusEvent)
FocusListener
focusLost(FocusEvent)
AdjustmentEvent
AdjustmentListener
adjustmentValueChanged(AdjustmentEvent)
componentMoved(ComponentEvent)
ComponentEvent
componentHidden(ComponentEvent)
ComponentListener
componentResized(ComponentEvent)
componentShown(ComponentEvent)
windowClosing(WindowEvent)
WindowEvent
WindowListener
windowOpened(WindowEvent)
windowIconified(WindowEvent)
y 230 y
12
windowDeiconified(WindowEvent)
windowClosed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
ContainerEvent
componentAdded(ContainerEvent)
ContainerListener
componentRemoved(ContainerEvent)
TextEvent
TextListener
textValueChanged(TextEvent)
AWT
public void add<ListenerType> (<ListenerType>listener);
public void remove<ListenerType> (<ListenerType>listener);
//
//
Button
public void addItemListener(ItemListener listener)
listener
null
public void removeItemListener(ItemListener listener)
listener null
12.3.2
ListenerDemo
/
//
12.8
ListenerDemo.java
//
import java.awt.*;
import java.awt.event.*;
public class ListenerDemo implements MouseMotionListener,MouseListener,WindowListener {
public static void main(String[ ] args) {
new ListenerDemo();
// ListenerDemo
}
public ListenerDemo()
{
f = new Frame("");
// f
tf1 = new TextField(20);
// tf1
tf2 = new TextField(20);
// tf1
f.add("South", tf1);
// tf1 South
f.add("North", tf2);
// tf1 North
f.addMouseListener(this);
// MouseListener
f.addMouseMotionListener(this);
// MouseMotionListener
f.addWindowListener(this);
// WindowListener
f.setSize(400, 300);
f.setVisible(true);
//
//
}
y 231 y
Java
//
public void mouseDragged(MouseEvent e) {
String s = "";
// s
tf1.setText(s);
// tf1
}
public void mouseMoved(MouseEvent e) {
String s = "";
// s
tf1.setText(s);
// tf1
tf2.setText(": X="+e.getX()+"Y = "+e.getY());
// tf2
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
tf1.setText("");
// tf1
}
public void mouseExited(MouseEvent e) {
String s = "";
// s
tf1.setText(s);
// tf1
}
public void mousePressed(MouseEvent e) {
// mousePressed
}
public void mouseReleased(MouseEvent e) { // mouseReleased
}
public void windowActivated(WindowEvent e) { // windowActivated
}
public void windowClosed(WindowEvent e) { // windowClosed
}
// windowClosing
public void windowClosing(WindowEvent e) {
System.exit(1);
//
}
public void windowDeactivated(WindowEvent e) {
// windowDeactivated
}
public void windowDeiconified(WindowEvent e) {
// windowDeiconified
}
public void windowIconified(WindowEvent e) { // windowIconified
}
public void windowOpened(WindowEvent e){ // windowOpened
}
private Frame f;
// f
private TextField tf1,tf2;
// tf1,tf2
}
javac class
java class
12-10
12.8 MouseMotionListener
MouseListenerWindowListener 3 3
12-10
y 232 y
ListenerDemo.java
12
12.3.3
f.addMouseMotionListener(this);
f.addMouseListener(this);
f.addWindowListener(this);
//
//
//
f
12.8 f
ThreeListener f ThreeListener
// s
// tf1
// tf2
Java
Java AWT
12.4
Java
EventAdapter
Java
java.awt.event
ComponentAdapter
ContainerAdapter
FocusAdapter
KeyAdapter
MouseAdapter
MouseMotionAdapter
WindowAdapter
y 233 y
Java
//
12.9
AdapterDemo.java
//
import java.awt.*;
import java.awt.event.*;
public class AdapterDemo {
public static void main(String[ ] args) {
new AdapterDemo();
}
// AdapterDemo
public AdapterDemo()
{
f = new Frame("");
y 234 y
// AdapterDemo
// f
// panel
// Center panel
//
//
//
//
//
//
//
// windowClosing
//
//
//
// Frame f
// Point start,end
// Panel panel
12
javac
class java class
12-11
12.9 panel
MouseAdapter mousePressed
mouseReleased mousePressed
mouseReleased
12-11
AdapterDemo.java
12.5
12.5.1
Inner Class
AWT
//
12.10
ListenerInnerClass.java
//
import java.awt.* ;
import java.awt.event.*;
public class ListenerInnerClass{
public static void main(String args[ ]) {
ListenerInnerClass li = new ListenerInnerClass();// ListenerInnerClass li
li.show();
// show
}
// ListenerInnerClass
public ListenerInnerClass(){
f=new Frame(",");
// f
tf=new TextField(30);
// tf
}
public void show(){
f.add(tf,"North");
// tf North
f.addMouseMotionListener(new MyMouseMotionListener());
//
MouseMotionListener
f.setSize(300,240);
//
// f
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt) {// windowClosing
y 235 y
Java
f.setVisible(false);
f.dispose();
System.out.println("");
System.exit(0);
// f
//
//
}
});
f.setVisible(true);
//
}
//
class MyMouseMotionListener extends MouseMotionAdapter{ //
public void mouseMoved(MouseEvent e) {
tf.setText(": x="+e.getX()+"Y="+e.getY()); // tf
}
}
private Frame f;
// Frame f
private TextField tf;
// TextField tf
}
12-12
12.5.2
ListenerDemo.java
//
12.11
ListeneranonymousClass.java
//
import java.awt.* ;
import java.awt.event.*;
public class ListeneranonymousClass extends MouseMotionAdapter{
public static void main(String args[ ]) {
// ListeneranonymousClass la
ListeneranonymousClass la = new ListeneranonymousClass();
la.show();
// show
y 236 y
12
}
public ListeneranonymousClass(){
f=new Frame(","); // f
tf=new TextField(20);
// tf
}
public void show(){
f.add(tf,"North");
// tf North
f.setSize(300,240);
//
//
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt) {
f.setVisible(false);
// f
f.dispose();
//
System.out.println("");
System.exit(0);
//
}
});
f.addMouseMotionListener(new MouseMotionAdapter(){ //
public void mouseMoved(MouseEvent e) {
tf.setText(": x="+e.getX()+"Y="+e.getY());
// tf
}
});
f.setVisible(true);
//
}
private Frame f;
// Frame f
private TextField tf;
// TextField tf
}
javac class
java class
12-13
12.11 f MouseMotionListener
MouseMotionAdapter
12-13 ListenerDemo.java
12.6 AWT
AWT
y 237 y
Java
12.6.1
NoteBook
12
public void windowClosing(WindowEvent e){
System.exit(0);
//
}
} );
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
saveAsItem.addActionListener(this);
exitItem.addActionListener(this);
selectItem.addActionListener(this);
copyItem.addActionListener(this);
cutItem.addActionListener(this);
pasteItem.addActionListener(this);
// newItem
// openItem
// saveItem
// saveAsItem
// exitItem
// selectItem
// copyItem
// cutItem
// pasteItem
12.6.2
if(fileName != null)
writeFile(fileName);
//
}else if(eventSource == selectItem)
//
{
textArea.selectAll();
//
}else if(eventSource == copyItem)
//
{
String text = textArea.getSelectedText();
//
y 239 y
Java
StringSelection selection = new StringSelection(text); // String
Transferable
clipBoard.setContents(selection, null);
//
transferable
}else if(eventSource == cutItem)
//
{
String text = textArea.getSelectedText();
//
StringSelection selection = new StringSelection(text); // String
Transferable
clipBoard.setContents(selection, null);
//
transferable
//
textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == pasteItem)
//
{
Transferable contents = clipBoard.getContents(this);//
transferable
if(contents==null) return;
//
String text;
// text
text = "";
// text
try{
//
text=(String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception exception){
// Exception
}
// text
textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == exitItem){
//
System.exit(0);
//
}
}
ActionListener actionPerformed
actionPerformed ActionEvent e e.getSource()
12.6.3
readFile
writeFile FileReader FileWriter
//
public void readFile(String fileName){
try{
File file = new File(fileName);
// File file
FileReader readIn = new FileReader(file); // file FileReader
int size = (int)file.length();
//
int charsRead = 0;
// charsRead
char[ ] content = new char[size];
//
while(readIn.ready())
//
charsRead += readIn.read(content, charsRead, size-charsRead); //
y 240 y
12
readIn.close();
// readIn
textArea.setText(new String(content,0,charsRead));
//
}catch(IOException e){
System.out.println("Error opening File");
//
}
}
//
public void writeFile(String fileName){
try{
File file = new File(fileName);
FileWriter writeOut = new FileWriter(file);
writeOut.write(textArea.getText());
writeOut.close();
}catch(IOException e){
System.out.println("Error writing file");
}
// File file
// file FileWriter
//
// writeOut
//
//
12.6.4
NoteBook Toolkit.getDefaultToolkit().getScreen
Size()
public static void main(String[ ] args) {
Frame frame = new NoteBook();
// NoteBook
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //
Dimension frameSize = frame.getSize();
//
if(frameSize.height > screenSize.height){
//
frameSize.height = screenSize.height;
//
}
if(frameSize.width > screenSize.width){
//
frameSize.width = screenSize.width;
//
}
//
frame.setLocation((screenSize.width
frameSize.width)/2,
(screenSize.height
frameSize.height)/2);
frame.setVisible(true);
//
}
y 241 y
Java
12-14 NoteBook.java
NoteBook
16
12.7
Java
y 242 y