package gui; import java.awt.*; import java.awt.event.*; class Farbwahl2b { public Farbwahl2b() { 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"); // Einplanzen der Hörerobjekte for (int i=0;i<5;i++) { b[i].addActionListener(new FarbHoerer()); p[4].add(b[i]); } // for for (int i=0;i<9;i++) { frame1.add(p[i]); } // for frame1.setTitle("Farbwahl, Version 2b"); frame1.setSize(300,300); frame1.setVisible(true); // frame1.show(); deprecated } // Farbwahl2b() public static void main(String[] args) { Farbwahl2b f2b = new Farbwahl2b(); } // main private Frame1 frame1 = new Frame1(); private Panel[] p = new Panel[9]; private Button[] b = new Button[5]; // EINE Klasse für alle Hörerobjekte class FarbHoerer implements ActionListener { public void actionPerformed(ActionEvent e) { // Bestimmen der angesprochenen Schaltfläche // mit einer Methode der Klasse ActionEvent String s = e.getActionCommand(); if (s.equals("rot")) { for(int i=0;i<9;i++) p[i].setBackground(Color.red); } else if (s.equals("blau")) { for(int i=0;i<9;i++) p[i].setBackground(Color.blue); } else if (s.equals("gruen")) { for(int i=0;i<9;i++) p[i].setBackground(Color.green); } else if (s.equals("gelb")) { for(int i=0;i<9;i++) p[i].setBackground(Color.yellow); } else if (s.equals("schwarz")) { for(int i=0;i<9;i++) p[i].setBackground(Color.black); } // if } // actionPerformed } // Farbhoerer } // Farbwahl2b