Script Java
Script Java Script Java
5.1 ActionEvent und ActionListener 5Event Handling ActionEvent ActionListener Das vorliegende Kapitel erläutert das Event Handling. Die Ausführungen beziehen sich auf ein Beispielprogramm, welches das Event Handling am Beispiel von ActionEvents und einem ActionListener aufzeigt. 5.1 ActionEvent und ActionListener Die folgende Application „Game“ implementiert ein Spiel, bei welchem der Benutzer gegen den Computer antritt. Hierbei hat man zu Beginn eine gewisse Anzahl von Elementen, von welchen man nun abwechslungsweise zieht. Man kann bei einem Zug höchstens bis zu drei Elemente aufs Mal nehmen, darf aber nicht passen. Wer das letzte Element wegnimmt, hat verloren. import java.awt.*; import java.awt.event.*; public class UserFrame extends Frame implements ActionListener { private Label message; private TextField text; private Button turn, take1, take2, take3; private void place(Component comp,int x,int y,int width,int height) { comp.setBounds(x, y, width, height); add(comp); } public UserFrame() { setTitle("Game"); setLayout(null); setSize(360,240); setResizable(false); place(message=new Label(),30, 50,300, 20); place(text=new TextField(),30,70,300, 20); 124
5.1 ActionEvent und ActionListener } place(turn=new Button(),140,140,80,20); place(take1=new Button("TAKE ONE"),40,100,80,20); place(take2=new Button("TAKE TWO"),140,100,80,20); place(take3=new Button("TAKE THREE"),240,100,80,20); turn.addActionListener(this); take1.addActionListener(this); take2.addActionListener(this); take3.addActionListener(this); text.setEchoChar('•'); setVisible(true); newGame(); public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals("START")) start(); else if (command.equals("NEW GAME")) newGame(); else if (command.equals("YOUR TURN")) myTurn(); else if (command.equals("TAKE ONE")) userTakes(1); else if (command.equals("TAKE TWO")) userTakes(2); else if (command.equals("TAKE THREE")) userTakes(3); } public int numberOfTokens() {return text.getText().length();} public void decrementTokens(int i) { text.setText(text.getText().substring(i)); } public void setTakeButtonsVisible(boolean yes) { take1.setVisible(yes); take2.setVisible(yes); take3.setVisible(yes); } public void newGame() { message.setText("Enter tokens and START:"); text.setText(""); text.setEditable(true); text.requestFocus(); turn.setLabel("START"); setTakeButtonsVisible(false); } public void start() { switch (numberOfTokens()) { case 0: message.setText("Please try again to enter tokens and START!"); break; case 1: message.setText("Entering only one single token is unfair!"); break; default: text.setEditable(false); myTurn(); } } public void userTakes(int k) { 125
- Seite 73 und 74: 3.1 int, boolean und Programmstrukt
- Seite 75 und 76: 3.1 int, boolean und Programmstrukt
- Seite 77 und 78: 3.1 int, boolean und Programmstrukt
- Seite 79 und 80: 3.1 int, boolean und Programmstrukt
- Seite 81 und 82: 3.1 int, boolean und Programmstrukt
- Seite 83 und 84: 3.1 int, boolean und Programmstrukt
- Seite 85 und 86: 3.1 int, boolean und Programmstrukt
- Seite 87 und 88: 3.1 int, boolean und Programmstrukt
- Seite 89 und 90: 3.1 int, boolean und Programmstrukt
- Seite 91 und 92: 3.1 int, boolean und Programmstrukt
- Seite 93 und 94: 3.1 int, boolean und Programmstrukt
- Seite 95 und 96: 3.1 int, boolean und Programmstrukt
- Seite 97 und 98: 3.1 int, boolean und Programmstrukt
- Seite 99 und 100: 3.1 int, boolean und Programmstrukt
- Seite 101 und 102: 3.1 int, boolean und Programmstrukt
- Seite 103 und 104: 3.1 int, boolean und Programmstrukt
- Seite 105 und 106: 3.2 Unicode und char } } textArea.a
- Seite 107 und 108: 3.2 Unicode und char Farben sind in
- Seite 109 und 110: 3.2 Unicode und char for (int i=0;i
- Seite 111 und 112: 3.3 Zusammenfassung Beschreibung We
- Seite 113 und 114: 4.1 Strings 4Strings Komplexe Daten
- Seite 115 und 116: 4.2 Einfache versus komplexe Datent
- Seite 117 und 118: 4.2 Einfache versus komplexe Datent
- Seite 119 und 120: 4.3 Type Casting Will man aber zwei
- Seite 121 und 122: 4.3 Type Casting 4.3.1.3 Datentyp b
- Seite 123: 4.4 Zusammenfassung 4.4 Zusammenfas
- Seite 127 und 128: 5.1 ActionEvent und ActionListener
- Seite 129 und 130: 5.1 ActionEvent und ActionListener
- Seite 131 und 132: 5.1 ActionEvent und ActionListener
- Seite 133 und 134: 5.1 ActionEvent und ActionListener
- Seite 135 und 136: 5.1 ActionEvent und ActionListener
- Seite 137 und 138: 5.1 ActionEvent und ActionListener
- Seite 139 und 140: 5.1 ActionEvent und ActionListener
- Seite 141 und 142: 5.1 ActionEvent und ActionListener
- Seite 143 und 144: 5.1 ActionEvent und ActionListener
- Seite 145 und 146: 5.2 Zusammenfassung ♦ Damit eintr
- Seite 147 und 148: 6.1 Instanzmethode versus Klassenme
- Seite 149 und 150: 6.1 Instanzmethode versus Klassenme
- Seite 151 und 152: 6.1 Instanzmethode versus Klassenme
- Seite 153 und 154: 6.1 Instanzmethode versus Klassenme
- Seite 155 und 156: 6.2 Visiblity Modifiers ClassMethod
- Seite 157 und 158: 6.2 Visiblity Modifiers } subjectLi
- Seite 159 und 160: 6.2 Visiblity Modifiers Eine Instan
- Seite 161 und 162: 6.2 Visiblity Modifiers 6.2.2 Synta
- Seite 163 und 164: 7.1 Calendar 7Method Overloading Ar
- Seite 165 und 166: 7.1 Calendar } text.requestFocus();
- Seite 167 und 168: 7.1 Calendar In den restlichen Zeil
- Seite 169 und 170: 7.1 Calendar Die Klassenmethode get
- Seite 171 und 172: 7.2 Graphics public void setTime(in
- Seite 173 und 174: 7.2 Graphics in der Methode paint()
5.1 ActionEvent und ActionListener<br />
5Event Handling<br />
ActionEvent<br />
ActionListener<br />
Das vorliegende Kapitel erläutert das Event Handling. Die Ausführungen beziehen sich auf<br />
ein Beispielprogramm, welches das Event Handling am Beispiel von ActionEvents und<br />
einem ActionListener aufzeigt.<br />
5.1 ActionEvent und ActionListener<br />
Die folgende Application „Game“ implementiert ein Spiel, bei welchem der Benutzer<br />
gegen den Computer antritt. Hierbei hat man zu Beginn eine gewisse Anzahl von<br />
Elementen, von welchen man nun abwechslungsweise zieht. Man kann bei einem Zug<br />
höchstens bis zu drei Elemente aufs Mal nehmen, darf aber nicht passen. Wer das letzte<br />
Element wegnimmt, hat verloren.<br />
import java.awt.*;<br />
import java.awt.event.*;<br />
public class UserFrame extends Frame implements ActionListener {<br />
private Label message;<br />
private TextField text;<br />
private Button turn, take1, take2, take3;<br />
private void place(Component comp,int x,int y,int width,int height) {<br />
comp.setBounds(x, y, width, height);<br />
add(comp);<br />
}<br />
public UserFrame() {<br />
setTitle("Game");<br />
setLayout(null);<br />
setSize(360,240);<br />
setResizable(false);<br />
place(message=new Label(),30, 50,300, 20);<br />
place(text=new TextField(),30,70,300, 20);<br />
124