import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; import java.io.*; import java.text.*; // NumberFormat public class Aufgabe_Facade extends JFrame implements ActionListener { public Aufgabe_Facade() { setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setGUI(); setVisible(true); } private JButton createButton(String caption, String command) { JButton bn = new JButton(caption); bn.setFont( new Font("Arial", Font.BOLD,16) ); bn.setActionCommand(command); bn.addActionListener(this); return bn; } private void setGUI() { setSize(800, 630); setLocation(20,20); setTitle("Test Adapter, Beispiel 4"); JPanel pnButton = new JPanel(); pnButton.setLayout( new FlowLayout() ); pnButton.add( createButton("write Text","Test1") ); pnButton.add( createButton("read Text","Test2") ); pnButton.add( createButton("writeBin","Test3") ); pnButton.add( createButton("readBin","Test4") ); pnButton.add( createButton("readHexViewer","Test5") ); pnButton.add( createButton("Abbruch","Abbruch") ); this.getContentPane().setLayout( new BorderLayout() ); JTextArea editor = Editor.getInstance(); this.getContentPane().add( new JScrollPane(editor), BorderLayout.CENTER); this.getContentPane().add( pnButton, BorderLayout.SOUTH ); } // setGUI public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("Test1") ) { test1(); } if (cmd.equals("Test2") ) { test2(); } if (cmd.equals("Test3") ) { test3(); } if (cmd.equals("Test4") ) { test4(); } if (cmd.equals("Test5") ) { test5(); } if (cmd.equals("Abbruch") ) { setVisible(false); dispose(); // System.exit(0); } } private void test1() { JTextArea editor = Editor.getInstance(); editor.setText("Ergebnisse:"); // hier fehlt Code } private void test2() { JTextArea editor = Editor.getInstance(); editor.setText("Ergebnisse:"); // hier fehlt Code } private void test3() { JTextArea editor = Editor.getInstance(); editor.setText("Ergebnisse:"); // hier fehlt Code } private void test4() { JTextArea editor = Editor.getInstance(); editor.setText("Ergebnisse:"); // hier fehlt Code } // test4 private void test5() { JTextArea editor = Editor.getInstance(); editor.setText("Ergebnisse:\n"); // hier fehlt Code } public static void main(String[] args) { new Aufgabe_Facade(); } } // Bsp4_IO class Editor { static private JTextArea editor = new MyJTextArea(); // wird nicht automatisch aufgerufen private Editor() { editor.setFont( new Font("Arial", Font.BOLD,28) ); System.out.println("editor"); } public static JTextArea getInstance() { return editor; } } class MyJTextArea extends JTextArea{ public MyJTextArea() { setFont( new Font("Arial", Font.BOLD,28) ); } } class FileSystem { public boolean error=false; public String errorText=""; private String filename=""; private FileInputStream fin; private DataInputStream din; private FileOutputStream fout; private DataOutputStream dout; public void createBinFile(String filename) { try { fout = new FileOutputStream(filename); dout = new DataOutputStream(fout); } catch (IOException e) { fout=null; dout=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // createBinFile public void readBinFile(String filename) { try { fin = new FileInputStream(filename); din = new DataInputStream(fin); } catch (IOException e) { fin=null; din=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // createBinFile public void writeByte(byte b) { try { if (dout!=null) { dout.writeByte(b); } else { errorText = "dout ist null"; error = true; } } catch (IOException e) { fout=null; dout=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // writeByte public void writeShort(short s) { try { if (dout!=null) { dout.writeShort(s); } else { errorText = "dout ist null"; error = true; } } catch (IOException e) { fout=null; dout=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // writeShort public void writeInt(int i) { try { if (dout!=null) { dout.writeInt(i); } else { errorText = "dout ist null"; error = true; } } catch (IOException e) { fout=null; dout=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // writeInt public void writeLong(long l) { try { if (dout!=null) { dout.writeLong(l); } else { errorText = "dout ist null"; error = true; } } catch (IOException e) { fout=null; dout=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // writeLong public void writeFloat(float f) { try { if (dout!=null) { dout.writeFloat(f); } else { errorText = "dout ist null"; error = true; } } catch (IOException e) { fout=null; dout=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // writeFloat public void writeDouble(double d) { try { if (dout!=null) { dout.writeDouble(d); } else { errorText = "dout ist null"; error = true; } } catch (IOException e) { fout=null; dout=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // writeDouble public void writeString(String s) { try { if (dout!=null) { dout.writeUTF(s); // Unicode Transformation Format } else { errorText = "dout ist null"; error = true; } } catch (IOException e) { fout=null; dout=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // writeUTF public byte readByte() { byte b=0; try { if (din!=null) { b=din.readByte(); } else { errorText = "din ist null"; error = true; } } catch (IOException e) { fin=null; din=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } return b; } // readByte public short readShort() { short s=0; try { if (din!=null) { s=din.readShort(); } else { errorText = "din ist null"; error = true; } } catch (IOException e) { fin=null; din=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } return s; } // readShort public int readInt() { int i=0; try { if (din!=null) { i=din.readInt(); } else { errorText = "din ist null"; error = true; } } catch (IOException e) { fin=null; din=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } return i; } // readInt public long readLong() { long l=0; try { if (din!=null) { l=din.readLong(); } else { errorText = "din ist null"; error = true; } } catch (IOException e) { fin=null; din=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } return l; } // readLong public float readFloat() { float f=0.0f; try { if (din!=null) { f=din.readFloat(); } else { errorText = "din ist null"; error = true; } } catch (IOException e) { fin=null; din=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } return f; } // readFloat public double readDouble() { double d=0.0; try { if (din!=null) { d=din.readDouble(); } else { errorText = "din ist null"; error = true; } } catch (IOException e) { fin=null; din=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } return d; } // readDouble public String readString() { String s=""; try { if (din!=null) { s=din.readUTF(); // // Unicode Transformation Format } else { errorText = "din ist null"; error = true; } } catch (IOException e) { fin=null; din=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } return s; } // readString public void close() { try { if (dout!=null) { dout.close(); } else { errorText = "dout war null"; error = true; } } catch (IOException e) { fout=null; dout=null; System.err.println("IOException: " + e); errorText = ""+e; error = true; } } // close public void saveText2File(String filename, String content) { error = false; errorText=""; FileOutputStream fout=null; try { fout = new FileOutputStream(filename); PrintStream p = new PrintStream(fout); p.print(content); } catch (IOException e) { System.err.println("IOException: " + e); errorText = ""+e; error = true; } finally { if ( fout != null ) { try { fout.close(); } catch ( IOException e ) { errorText += e.toString(); error = true; } } } } // saveText2File public String readHexViewFromFile(String filename, boolean hex) { FileInputStream fin=null; StringBuilder sb = new StringBuilder(); error = false; errorText=""; try { fin = new FileInputStream(filename); BufferedInputStream bff = new BufferedInputStream(fin); DataInputStream din = new DataInputStream(bff); int i=0; DecimalFormat df = new DecimalFormat("00"); String s; while ( din.available()>0 ) { int b = din.readByte(); if (b<0) b+=256; if (hex) sb.append( Integer.toHexString(b)); else sb.append( df.format(b) ); sb.append( " " ); i++; if (i>=8) { sb.append( "\n"); i=0; } } } catch (IOException e) { System.err.println("IOException: " + e); errorText = ""+e; error = true; } finally { if ( fin != null ) { try { fin.close(); } catch ( IOException e ) { errorText += ""+e; error = true; } } } return sb.toString(); } public String readTextFromFile(String filename) { FileInputStream fin=null; InputStreamReader iin=null; LineNumberReader din=null; StringBuilder sb = new StringBuilder(); error = false; errorText=""; try { fin = new FileInputStream(filename); BufferedInputStream bff = new BufferedInputStream(fin); iin = new InputStreamReader(bff); din = new LineNumberReader(iin); while ( din.ready() ) { sb.append( din.readLine()); sb.append( "\n"); } } catch (IOException e) { System.err.println("IOException: " + e); errorText = ""+e; error = true; } finally { if ( fin != null ) { try { fin.close(); } catch ( IOException e ) { errorText += ""+e; error = true; } } } return sb.toString(); } // readTextFromFile }