Beuth Hochschule Zwei Grabo-Programme WS15/16, S. 1
Zwei Grabo-Programme
die beide das Gleiche machen (mit absichtlich eingefügten Leerzeilen).
1 // File Grabo30.java
2 /* --- 3 Dieses Grabo-Programm oeffnet ein Fenster mit einem Knopf darin.
4 Bei dem Knopf wird ein Behandler-Objekt namens behandlerOb
5 einer Klasse namens BehandlerK angemeldet (mit der Methode addActionListner).
6 --- */
7 import javax.swing.JFrame;
8 import javax.swing.JButton;
9 import java.awt.event.ActionListener;
10 import java.awt.event.ActionEvent;
11
12 public class Grabo30 extends JFrame {
13 // --- 14 static class BehandlerK implements ActionListener {
15 public void actionPerformed(ActionEvent ae) { 16 printf("actionPerformed wurde aufgerurfen!%n");
17 String txt = ae.toString().replace(',', '\n');
18 printf("%s%n%n", txt);
19 } 20 } 21
22 BehandlerK behandlerOb = new BehandlerK();
23 // --- 24 JButton knopf01 = new JButton("K01");
25 // --- 26 public Grabo30(String titel) {
27 super(titel);
28
29 knopf01.addActionListener(behandlerOb);
30 31 32 33 34 35 36
37 this.getContentPane().add(knopf01);
38 this.setBounds(500, 300, 250, 150);
39 this.setVisible(true);
40 }
41 // --- 42 static public void main(String[] args) {
43 printf("Grabo30: Here we go!%n");
44 printf("---%n");
45 Grabo30 g30 = new Grabo30("g30");
46 printf("---%n");
47 printf("Grabo30: That's all!%n");
48 } // main
49 // --- 50 // A Method with a short name:
51 static void printf(String f, Object... v) {System.out.printf(f, v);}
52 // --- 53 } // class Grabo30
S. 2, WS15/16 Zwei Grabo-Programme Beuth-Hochschule
1 // File Grabo31.java
2 /* --- 3 Dieses Grabo-Programm oeffnet ein Fenster mit einem Knopf darin.
4 Bei dem Knopf wird ein anonymes Behandler-Objekt
5 einer anonymen Klasse angemeldet (mit der Methode addActionListner).
6 --- */
7 import javax.swing.JFrame;
8 import javax.swing.JButton;
9 import java.awt.event.ActionListener;
10 import java.awt.event.ActionEvent;
11
12 public class Grabo31 extends JFrame { 13
14 15 16 17 18 19 20 21 22
23 // --- 24 JButton knopf01 = new JButton("K01");
25 // --- 26 public Grabo31(String titel) {
27 super(titel);
28
29 knopf01.addActionListener(new ActionListener() { 30 public void actionPerformed(ActionEvent ae) { 31 printf("actionPerformed wurde aufgerurfen!%n");
32 String txt = ae.toString().replace(',', '\n');
33 printf("%s%n%n", txt);
34 } 35 });
36
37 this.getContentPane().add(knopf01);
38 this.setBounds(500, 300, 250, 150);
39 this.setVisible(true);
40 }
41 // --- 42 static public void main(String[] args) {
43 printf("Grabo31: Here we go!%n");
44 printf("---%n");
45 Grabo31 g31 = new Grabo31("g31");
46 printf("---%n");
47 printf("Grabo31: That's all!%n");
48 } // main
49 // --- 50 // A Method with a short name:
51 static void printf(String f, Object... v) {System.out.printf(f, v);}
52 // --- 53 } // class Grabo31