package GUIAnwendungen.Klausur;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GuiDemo2 extends Object {

	public static void main(String[] args) {
		JFrame jf = new JFrame("Toller Titel");
		
		jf.add(new JLabel("Hallo Welt!"), BorderLayout.NORTH);
		jf.add(new JTextField(10), BorderLayout.CENTER);
		
		JButton jb1 = new JButton("Cancel");
		JButton jb2 = new JButton("OK");
		
		JPanel jp = new JPanel();
		jp.setLayout(new GridLayout());
		jp.add(jb1);
		jp.add(jb2);
	
		jf.add(jp, BorderLayout.SOUTH);

		jf.pack();
		jf.setVisible(true);
		
		ActionListener obj = new GuiDemo2().new MyActionListener();  // beachte den "seltsamen" Konstruktor Aufruf
		jb1.addActionListener(obj);
		jb2.addActionListener(obj);
		
		//  obj.printSomething();	// geht nicht, da Methode bei ActionListener nicht sichtbar
		
		GuiDemo2.MyActionListener gd = (GuiDemo2.MyActionListener) obj;		// Downcast
		gd.printSomething();	// nach Downcast geht es dann
		
		System.out.println("Ende Gelände...");
	}
	
	// ActionListener-Implementierung als non-static member class
	class MyActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			this.printSomething();
			JButton src = (JButton) e.getSource();
			System.out.println("Button geklickt: " + src.getText());
		}
		
		public void printSomething() {
			System.out.println("Something!");
			MyActionListener mal = new MyActionListener();
		}
	}
	
} 