package gui; import java.awt.*; import java.awt.event.*; // zusätzliche import-Anweisung !! class Farbwahl2a { public Farbwahl2a() { frame1.setLayout(new GridLayout(3,3)); for (int i=0;i<9;i++) { p[i] = new Panel(new GridLayout(5,1)); } // for b[0] = new Button("rot"); b[1] = new Button("blau"); b[2] = new Button("gruen"); b[3] = new Button("gelb"); b[4] = new Button("schwarz"); // Erzeugen und Einpflanzen der Hörerobjekte b[0].addActionListener(new RotHoerer()); b[1].addActionListener(new BlauHoerer()); b[2].addActionListener(new GruenHoerer()); b[3].addActionListener(new GelbHoerer()); b[4].addActionListener(new SchwarzHoerer()); for(int i=0;i<5;i++) { p[4].add(b[i]); } // for for(int i=0;i<9;i++) { frame1.add(p[i]); } // for frame1.setTitle("Farbwahl, Version 2a"); frame1.setSize(300,300); frame1.setVisible(true); // frame1.show(); deprecated } // Farbwahl2a() public static void main(String[] args) { Farbwahl2a f2a = new Farbwahl2a(); } // main private Frame1 frame1 = new Frame1(); private Panel[] p = new Panel[9]; private Button[] b = new Button[5]; // innere Hörerklassen als Interface-Implementierung // das Interface ActionListener enthält nur eine Methode -> actionPerformed class RotHoerer implements ActionListener { // was soll beim Auftreten von e passieren; // Methode setBackground() steht für alle Darstellungsflächen zur Verfügung // Color.red - Klassenvariable der Klasse Color public void actionPerformed(ActionEvent e) { for (int i=0;i<9;i++) p[i].setBackground(Color.red); } // actionPerformed } // RotHoerer class BlauHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0;i<9;i++) p[i].setBackground(Color.blue); } } // BlauHoerer class GruenHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0;i<9;i++) p[i].setBackground(Color.green); } } // GruenHoerer class GelbHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0;i<9;i++) p[i].setBackground(Color.yellow); } } // GelbHoerer class SchwarzHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0;i<9;i++) p[i].setBackground(Color.black); } } // SchwarzHoerer } // Farbwahl2a