Ereignisse Beispiel mit Schaltknöpfen: import java.awt.*; public class Butt01 extends java.applet.Applet { public void init() { Button bu1 = new Button("Taste1"); Button bu2 = new Button("Taste2"); Button bu3 = new Button("Taste3"); add(bu1); add(bu2); add(bu3); } } Beispiel 2: import java.awt.*; import java.awt.event.*; public class Butt02 extends java.applet.Applet { Button bu1 = new Button("Taste1"); Button bu2 = new Button("Taste2"); Button bu3 = new Button("Taste3"); Label l = new Label(" "); class Bl implements ActionListener {
public void actionPerformed(ActionEvent e)
{
l.setText("Die " + e.getActionCommand() +
" wurde angeklickt!.");
}
}
Bl al = new Bl();
public void init()
{
add(bu1);
bu1.addActionListener(al);
add(bu2);
bu2.addActionListener(al);
add(bu3);
bu3.addActionListener(al);
add(l);
}
}
Beispiel 3:
import java.awt.*;
import java.awt.event.*;
public class Pan001 extends java.applet.Applet
{
TextField tx1 = new TextField("Textfeld1",20);
TextField tx2 = new TextField("Textfeld2",20);
TextField tx3 = new TextField("Textfeld3",20);
Checkbox kr1 = new Checkbox("Markierung1");
Checkbox kr2 = new Checkbox("Markierung2");
Checkbox kr3 = new Checkbox("Markierung3");
Button bu1 = new Button("Taste1");
Button bu2 = new Button("Taste2");
Button bu3 = new Button("Taste3");
Label la1 = new Label("Label1");
Label la2 = new Label("Label2");
Label la3 = new Label("Label3");
Choice aw1 = new Choice();
Choice aw2 = new Choice();
Choice aw3 = new Choice();
class Tl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("\nTextfeld-Ereignis:");
System.out.println(e.getActionCommand());
System.out.println(e.getModifiers());
System.out.println(e.paramString());
}
}
Tl tl = new Tl();
class Il implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
System.out.println("\nAnkreuzfeld-Ereignis:");
System.out.println(e.getItem());
System.out.println(e.getItemSelectable());
System.out.println(e.getStateChange());
System.out.println(e.paramString());
}
}
Il il = new Il();
class Bl implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("\nSchaltaste-Ereignis:");
System.out.println(e.getActionCommand());
System.out.println(e.getModifiers());
System.out.println(e.paramString());
}
}
Bl al = new Bl();
class Cl implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
System.out.println("\nAuswahlfeld-Ereignis:");
System.out.println(e.getItem());
System.out.println(e.getItemSelectable());
System.out.println(e.getStateChange());
System.out.println(e.paramString());
}
}
Cl cl = new Cl();
public void init()
{
add(la1);
add(tx1);
tx1.addActionListener(tl);
add(kr1);
kr1.addItemListener(il);
add(bu1);
bu1.addActionListener(al);
add(la2);
add(tx2);
tx2.addActionListener(tl);
add(kr2);
kr2.addItemListener(il);
add(bu2);
bu2.addActionListener(al);
add(la3);
add(tx3);
tx3.addActionListener(tl);
add(kr3);
kr3.addItemListener(il);
add(bu3);
bu3.addActionListener(al);
aw1.addItem("Möglichkeit 1-1");
aw1.addItem("Möglichkeit 1-2");
aw1.addItem("Möglichkeit 1-3");
aw1.addItem("Möglichkeit 1-4");
add(aw1);
aw1.addItemListener(cl);
aw2.addItem("Möglichkeit 2-1");
aw2.addItem("Möglichkeit 2-2");
aw2.addItem("Möglichkeit 2-3");
aw2.addItem("Möglichkeit 2-4");
add(aw2);
aw2.addItemListener(cl);
aw3.addItem("Möglichkeit 3-1");
aw3.addItem("Möglichkeit 3-2");
aw3.addItem("Möglichkeit 3-3");
aw3.addItem("Möglichkeit 3-4");
add(aw3);
aw3.addItemListener(cl);
}
}
Beispiel 4:
import java.awt.*;
import java.awt.event.*;
public class Sreg01 extends java.applet.Applet
{
// Anlegen der Beschriftungen
Scrollbar sc1 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,15);
Scrollbar sc2 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,15);
Scrollbar sc3 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,15);
class Sl1 implements AdjustmentListener
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
System.out.println("\nScrollfeld-Ereignis 1:");
System.out.println(e.getAdjustable());
System.out.println(e.getAdjustmentType());
System.out.println(e.paramString());
System.out.println(e.getValue());
}
}
Sl1 sl1 = new Sl1();
class Sl2 implements AdjustmentListener
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
System.out.println("\nScrollfeld-Ereignis 2:");
System.out.println(e.getAdjustable());
System.out.println(e.getAdjustmentType());
System.out.println(e.paramString());
System.out.println(e.getValue());
}
}
Sl2 sl2 = new Sl2();
class Sl3 implements AdjustmentListener
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
System.out.println("\nScrollfeld-Ereignis 3:");
System.out.println(e.getAdjustable());
System.out.println(e.getAdjustmentType());
System.out.println(e.paramString());
System.out.println(e.getValue());
}
}
Sl3 sl3 = new Sl3();
public void init()
{
add(sc1);
sc1.addAdjustmentListener(sl1);
add(sc2);
sc2.addAdjustmentListener(sl2);
add(sc3);
sc3.addAdjustmentListener(sl3);
}
}
|