import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Iterator; public class Iter1 extends JFrame implements ActionListener{ JButton bnTest1 = new JButton("1. Test"); JButton bnTest2 = new JButton("2. Test"); JTextArea editor = new JTextArea(); // Konstruktor public Iter1() { this.setSize( 400, 400); setLocation(400,10); setTitle( "Iterator 1. Beispiel"); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setGUI(); } /** * Aufbau der GUI-Elemente. * Einfuegen der "desktopPane" in CENTER. */ public void setGUI() { // BorderLayout setzen this.getContentPane().setLayout( new BorderLayout() ); JPanel panelBn = new JPanel(); panelBn.setLayout( new FlowLayout(FlowLayout.RIGHT)); panelBn.add(bnTest1); panelBn.add(bnTest2); this.getContentPane().add(panelBn,BorderLayout.NORTH); setFonts(getContentPane(), 18); bnTest1.addActionListener(this); bnTest2.addActionListener(this); this.getContentPane().add( new JScrollPane(editor) ,BorderLayout.CENTER); editor.setFont(new Font("Arial", Font.BOLD, 18)); } private void setFonts(Container cont, int size) { for (int i = 0; i < cont.getComponentCount(); i++) { Component c = cont.getComponent(i); if (c instanceof JPanel) setFonts((JPanel) c, size); else c.setFont(new Font("Arial", Font.BOLD, size)); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == bnTest1) { bnTest1_click(); } if (e.getSource() == bnTest2) { bnTest2_click(); } } private void bnTest1_click() { MyIterable myit = new MyIterable(); myit.setElements(new String[] {"1", "2", "3"}); editor.setText("1. Test:\n"); for (String s : myit) { editor.append(" "+s+"\n"); } } private void bnTest2_click() { editor.setText("2. Test"); } public static void main(String[] args) { Iter1 frame = new Iter1(); frame.setVisible(true); } } class MyIterator implements Iterator { private int position = -1; private E[] arr = null; public void setData( E[] elements ) { this.arr = elements; } public boolean hasNext() { return this.position + 1 < this.arr.length; } public E next() { return this.arr[++this.position]; } public void remove() { //nicht implementiert } } // MyIterator class MyIterable implements Iterable { private T[] elements = null; public void setElements(T[] elements) { this.elements = elements; } public Iterator iterator() { MyIterator iter = new MyIterator(); iter.setData(this.elements); return iter; } }