/**
* Sample application using Frame.
*
* @author
* @version 1.00 05/01/28
*/
import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;
import java.io.*;
import java.lang.ClassNotFoundException;
public class VokabelEditorFrame extends Frame
{
String eingabe="";
int eingabe_int=0;
TextField vokabelanzahl;
TextField deutsch[] = new TextField[eingabe_int];
TextField latein[] = new TextField[eingabe_int];
//String lateinHash[];
//String deutschHash[];
String lateinHash2[] = new String[eingabe_int];
String deutschHash2[] = new String[eingabe_int];
Hashtable vokabeln = new Hashtable();
/**
* The constructor.
*/
public VokabelEditorFrame()
{
setLayout( new GridBagLayout() );
final Insets insets = new Insets( 2, 2, 2, 2);
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
MenuItem menuFileSave = new MenuItem();
MenuItem menuFileAbfragen = new MenuItem();
vokabelanzahl = new TextField("1");
add( vokabelanzahl, new GridBagConstraints( 1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, insets, 0, 0) );
vokabelanzahl.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
eingabe = vokabelanzahl.getText();
eingabe_int = Integer.parseInt(eingabe);
System.out.println("making list...");
System.out.println("Anzahl: "+eingabe_int);
latein = new TextField[eingabe_int];
deutsch = new TextField[eingabe_int];
GridBagConstraints constraintsDEUTSCH = new GridBagConstraints( 0, 0, 2, 1, 1.0, 1.0,
GridBagConstraints.EAST, GridBagConstraints.VERTICAL, insets, 0, 0 );
GridBagConstraints constraintsLATEIN = new GridBagConstraints( 0, 0, 2, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.VERTICAL, insets, 0, 0 );
for(int i = 0; i <eingabe_int; i++)
{
latein[i] = new TextField("latein");
deutsch[i]= new TextField("deutsch");
constraintsLATEIN.gridy = i+1;
constraintsDEUTSCH.gridy = i+1;
add(latein[i], constraintsLATEIN);
add(deutsch[i], constraintsDEUTSCH);
repaint();
}
validate();
}
}
});
Label anzahl = new Label("Wieviele Vokabeln möchten Sie eingeben?");
add(anzahl, new GridBagConstraints( 0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, insets, 0, 0));
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
menuFileSave.setLabel("speichern...");
menuFileAbfragen.setLabel("abfragen!");
menuFileSave.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String lateinHash[] = new String[eingabe_int];
String deutschHash[] = new String[eingabe_int];
for(int i=0; i<eingabe_int; i++)
{
lateinHash[i] = latein[i].getText();
deutschHash[i] = deutsch[i].getText();
System.out.println("prepare to save...");
System.out.println("making Hash-Tab...");
vokabeln.put(lateinHash[i], deutschHash[i]);
vokabeln.put(deutschHash[i], lateinHash[i]);
System.out.println("finished!");
}
try
{
FileOutputStream ausgabeDatei = new FileOutputStream("vokabel.tab");
ObjectOutputStream ausgangsStrom = new ObjectOutputStream(ausgabeDatei);
ausgangsStrom.writeObject(vokabeln);
ausgabeDatei.close();
}
catch (IOException fehler)
{
fehler.printStackTrace();
System.out.println("ERROR CREATING HASH TAB!");
}
}
}
);
menuFileAbfragen.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Abfrage();
}
});
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
VokabelEditorFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileAbfragen);
menuFile.add(menuFileSave);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("VokabelEditor");
setMenuBar(menuBar);
setSize(new Dimension(900, 700));
// Add window listener.
this.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
VokabelEditorFrame.this.windowClosed();
}
}
);
}
//Hauptprogramm starten
public void Abfrage()
{
String vokabel_eingabe="";
//String lateinHash2[] = new String[eingabe_int];
//String deutschHash2[] = new String[eingabe_int];
BufferedReader in = new BufferedReader(new InputStreamReader (System.in));
Hashtable vokabelnNeu = null;
// die Vokabeln aus der Datei "vokabel.tab" wieder einlesen
try
{
FileInputStream eingabeDatei = new FileInputStream("vokabel.tab");
ObjectInputStream eingangsStrom = new ObjectInputStream(eingabeDatei);
try
{
vokabelnNeu = (Hashtable) eingangsStrom.readObject();
}
catch (ClassNotFoundException fehler)
{
fehler.printStackTrace();
}
eingabeDatei.close();
}
catch (IOException fehler)
{
fehler.printStackTrace();
}
//Abfrage
for(int j=0; j<eingabe_int; j++)
{
System.out.println(vokabelnNeu.get(deutschHash2[j])+"\n");
try
{
vokabel_eingabe=in.readLine();
if(eingabe.equals(vokabelnNeu.get(lateinHash2[j])))
{
System.out.println("Richtiiig!\n");
}
else
{
System.out.println("Falsch!\n");
}
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
}
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed()
{
// TODO: Check if it is save to close the application
// Exit application.
System.exit(0);
}
}