SS 2011 Fakultät für Angewandte Informatik Lehrprofessur für Informatik 06.07.2011 Prof. Dr. Robert Lorenz Code zum Betreuten Programmieren vom 06.07.2011, Blatt 10 Serialisierung und GUI 1 Die Klasse Field 1 import java.awt.button; 2 import java.awt.panel; 3 import java.awt.gridlayout; 4 import java.awt.event.actionevent; 5 import java.awt.event.actionlistener; 6 import java.awt.color; 7 import java.io.serializable; 8 import java.util.arraylist; 9 10 public class Field extends Panel implements ActionListener, Serializable 11 { 12 private static final long serialversionuid = 5430739654048761030L; 13 private int clickedbuttons; 14 private ArrayList<String> clicked; 15 private ArrayList<Button> numbers; 16 17 public Field() 18 { 19 super(); 20 numbers = new ArrayList<Button>(49); 21 for(int i=1;i<=49;i++) 22 { 23 numbers.add(new Button(""+i)); 24 numbers.get(i-1).addactionlistener(this); 25 numbers.get(i-1).setbackground(color.white); 26 add(numbers.get(i-1)); 27 } 28 clicked = new ArrayList<String>(6); 29 clickedbuttons=0; 30 setlayout(new GridLayout(7,7)); 31 setbackground(color.red); 32 } 33 34 public void actionperformed(actionevent e) 1
35 { 36 Button b = (Button)e.getSource(); 37 if(b.getbackground()==color.white && clickedbuttons<6) 38 { 39 b.setbackground(color.magenta); 40 clicked.add(b.getlabel()); 41 clickedbuttons++; 42 } 43 else if(b.getbackground()==color.magenta) 44 { 45 b.setbackground(color.white); 46 clicked.remove(b.getlabel()); 47 clickedbuttons--; 48 } 49 } 50 51 public void redo() 52 { 53 for(int i=0;i<numbers.size();i++) 54 { 55 numbers.get(i).setbackground(color.white); 56 clicked.clear(); 57 } 58 clickedbuttons=0; 59 } 60 61 public ArrayList<String> getclicked() 62 { 63 return clicked; 64 } 65 66 public boolean isok() 67 { 68 if(clickedbuttons==0 clickedbuttons==6) 69 return true; 70 else 71 return false; 72 } 73 } 2 Die Klasse LotteryTicker 1 import java.awt.borderlayout; 2 import java.awt.button; 3 import java.awt.color; 4 import java.awt.filedialog; 5 import java.awt.frame; 6 import java.awt.gridlayout; 7 import java.awt.label; 8 import java.awt.panel; 9 import java.awt.event.actionevent; 10 import java.awt.event.actionlistener; 11 import java.awt.event.windowadapter; 2
12 import java.awt.event.windowevent; 13 import java.io.fileinputstream; 14 import java.io.fileoutputstream; 15 import java.io.objectinputstream; 16 import java.io.objectoutputstream; 17 import java.util.arraylist; 18 import java.util.iterator; 19 20 public class LotteryTicket extends Frame implements ActionListener 21 { 22 private Label msg; 23 private ArrayList<Field> lottofields; 24 private Panel fields; 25 private static final int SAVE = 0; 26 private static final int LOAD = 1; 27 private int TYPE; 28 private Warning warning=null; 29 30 public LotteryTicket() 31 { 32 super("lottoschein"); 33 setbackground(color.red); 34 lottofields = new ArrayList<Field>(); 35 fields = new Panel(new GridLayout(2,6,2,2)); 36 fields.setbackground(color.red); 37 for(int i=0;i<12;i++) 38 { 39 lottofields.add(new Field()); 40 fields.add(lottofields.get(i)); 41 } 42 Panel buttons = new Panel(); 43 buttons.setbackground(color.white); 44 45 Button speichern = new Button("Speichern"); 46 speichern.addactionlistener(this); 47 buttons.add(speichern); 48 49 Button laden = new Button("Laden"); 50 laden.addactionlistener(this); 51 buttons.add(laden); 52 53 Button loeschen = new Button("Loeschen"); 54 loeschen.addactionlistener(this); 55 buttons.add(loeschen); 56 57 Label nachricht = new Label("Nachricht: "); 58 buttons.add(nachricht); 59 60 msg = new Label(); 61 buttons.add(msg); 62 63 add(fields,borderlayout.north); 3
64 add(buttons,borderlayout.center); 65 66 pack(); 67 68 addwindowlistener(new WindowAdapter() 69 { 70 public void windowclosing(windowevent e) 71 { 72 dispose(); 73 System.exit(0); 74 } 75 }); 76 setvisible(true); 77 } 78 79 public void setwarning(warning warning) 80 { 81 this.warning=warning; 82 } 83 84 public void actionperformed(actionevent e) 85 { 86 if(e.getactioncommand().equals(("speichern"))) 87 { 88 if(checkticket(lottofields)==false) 89 { 90 TYPE=SAVE; 91 new Warning(this,"Der Lottoschein enthält unvollständige Felder"); 92 } 93 else 94 saveticket(); 95 } 96 else if(e.getactioncommand().equals("loeschen")) 97 redoticket(); 98 else if(e.getactioncommand().equals("laden")) 99 { 100 TYPE=LOAD; 101 loadticket(); 102 } 103 else if(e.getactioncommand().equals("ok")) 104 { 105 warning.dispose(); 106 if(type==save) 107 doit(type); 108 } 109 else if(e.getactioncommand().equals("cancel")) 110 { 111 System.out.println(e.getActionCommand()); 112 warning.setvisible(false); 113 warning.dispose(); 114 if(type==load) 115 TYPE=-1; 4
116 } 117 } 118 119 private void doit(int type) 120 { 121 if(type==save) 122 saveticket(); 123 else if (type==load) 124 loadticket(); 125 } 126 127 private boolean checkticket(arraylist<field> al) 128 { 129 Iterator<Field> it=al.iterator(); 130 boolean isok=true; 131 while(it.hasnext()) 132 { 133 if(it.next().isok()==false) 134 isok = false; 135 } 136 return isok; 137 } 138 139 private void redoticket() 140 { 141 Iterator<Field> it = lottofields.iterator(); 142 while(it.hasnext()) 143 { 144 it.next().redo(); 145 } 146 msg.settext("zurückgesetzt!"); 147 this.pack(); 148 } 149 150 private void saveticket() 151 { 152 Iterator<Field> it = lottofields.iterator(); 153 ObjectOutputStream stream=null; 154 FileOutputStream fo=null; 155 String filename; 156 FileDialog fd = new FileDialog(this,"Speichern",FileDialog.SAVE); 157 fd.setalwaysontop(true); 158 fd.setlocation(50, 50); 159 fd.setvisible(true); 160 filename = fd.getfile(); 161 try 162 { 163 fo = new FileOutputStream(filename); 164 stream = new ObjectOutputStream(fo); 165 166 while(it.hasnext()) 167 { 5
168 Field f=it.next(); 169 if(!f.isok()) 170 { 171 f.redo(); 172 } 173 } 174 175 stream.writeobject(lottofields); 176 msg.settext("schein gespeichert"); 177 stream.close(); 178 for (Field f: lottofields) 179 f.redo(); 180 } 181 catch (Exception e) 182 { 183 184 msg.settext((e.getstacktrace())[0].tostring()+" schmeisst: "+e.tostring()); 185 } 186 this.pack(); 187 } 188 189 private void loadticket() 190 { 191 FileInputStream fi=null; 192 ObjectInputStream stream=null; 193 FileDialog fd = new FileDialog(this,"Speichern",FileDialog.SAVE); 194 fd.setalwaysontop(true); 195 fd.setlocation(50, 50); 196 fd.setvisible(true); 197 String filename = fd.getfile(); 198 try 199 { 200 fi = new FileInputStream(filename); 201 stream = new ObjectInputStream(fi); 202 Object o = stream.readobject(); 203 if(o instanceof ArrayList<?>) 204 { 205 if(checkticket((arraylist<field>)o)==false) 206 { 207 new Warning(this,"Der Lottoschein enthält unvollständige Felder"); 208 } 209 if(type==load) 210 { 211 lottofields.clear(); 212 lottofields=(arraylist<field>)o; 213 msg.settext("feld geladen!"); 214 fields.removeall(); 215 for(int i=0;i<lottofields.size();i++) 216 { 217 fields.add(lottofields.get(i)); 218 } 219 msg.settext("geladen"); 6
220 } 221 } 222 stream.close(); 223 } 224 catch(exception e) 225 { 226 msg.settext(e.getmessage()); 227 } 228 this.pack(); 229 } 230 } 3 Die Klasse Warning 1 import java.awt.borderlayout; 2 import java.awt.button; 3 import java.awt.dialog; 4 import java.awt.frame; 5 import java.awt.label; 6 import java.awt.event.actionevent; 7 import java.awt.event.actionlistener; 8 9 public class Warning extends Dialog implements ActionListener 10 { 11 private Button ok; 12 private Button cancel; 13 public static final int OK=1; 14 public static final int CANCEL=2; 15 private int action=0; 16 17 public Warning(LotteryTicket owner, String msg) 18 { 19 super(owner,true); 20 add(new Label(msg),BorderLayout.NORTH); 21 ok = new Button("OK"); 22 cancel = new Button("Cancel"); 23 ok.addactionlistener(owner); 24 cancel.addactionlistener(owner); 25 add(ok,borderlayout.west); 26 add(cancel,borderlayout.east); 27 pack(); 28 owner.setwarning(this); 29 setvisible(true); 30 } 31 32 public void actionperformed(actionevent e) 33 { 34 if(e.getactioncommand().equals("ok")) 35 action=ok; 36 else if (e.getactioncommand().equals("cancel")) 37 action=cancel; 38 } 39 7
40 public int getresult(int type) 41 { 42 return action; 43 } 44 } 8