Code:
package Userpackage;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Layouttest extends JFrame {
Scanner s;
JLabel Vorname1;
JLabel Nachname1;
JLabel Email1;
JButton button;
JButton neuerNutzer;
JTextField Vorname;
JTextField Nachname;
JTextField Email;
String Vorname3;
String Nachname3;
String Email3;
JTextArea text;
Layouttest (){
setVisible(true);
setSize(1000,900);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(0,0);
setTitle("Wiedergabe");
Listener ul = new Listener();
FileReadWriteExample lu = new FileReadWriteExample();
Vorname1 = new JLabel ("Vorname:");
Vorname1.setBounds(10, 0, 500, 60);
Font schrift = new Font(null, Font.PLAIN + Font.ITALIC, 20);
Vorname1.setFont(schrift);
add(Vorname1);
Vorname = new JTextField();
Vorname.setBounds(125, 20 ,200,25);
Vorname.setToolTipText("Vorname");
add (Vorname);
Nachname1 = new JLabel ("Nachname:");
Nachname1.setBounds(10, 40, 500, 60);
Font schrift1 = new Font(null, Font.PLAIN + Font.ITALIC,20);
Nachname1.setFont(schrift1);
add(Nachname1);
Nachname = new JTextField();
Nachname.setBounds(125, 60 ,200,25);
Nachname.setToolTipText("Nachname");
add (Nachname);
Email1 = new JLabel ("Email:");
Email1.setBounds(10, 80, 500, 60);
Font schrift2 = new Font(null, Font.PLAIN + Font.ITALIC, 20);
Email1.setFont(schrift2);
add(Email1);
Email = new JTextField();
Email.setBounds(125, 100 ,200,25);
Email.setToolTipText("Email");
add (Email);
button = new JButton ("Datenbank aktualisieren");
button.setBounds(175, 150 ,200,25);
button.addActionListener(lu);
button.setEnabled(true);
button.setToolTipText("Datenbank aktualisieren");
add (button);
neuerNutzer = new JButton ("Neuer Nutzer");
neuerNutzer.setBounds(10, 150 ,150,25);
neuerNutzer.addActionListener(ul);
neuerNutzer.setEnabled(true);
neuerNutzer.setToolTipText("Neuen Nutzer erstellen");
add (neuerNutzer);
text = new JTextArea ();
text.setLineWrap(true);
text.setWrapStyleWord(true);
text.setOpaque(false);
add(text);
}
private class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()== neuerNutzer){
String Vorname2 = Vorname.getText();
String Nachname2 = Nachname.getText();
String Email2 = Email.getText();
File file = new File("C:/Users/Finn/Desktop/Datenbank.txt");{
try {
OutputStreamWriter writer = new FileWriter(file ,true);
writer.write("Vorname: " + Vorname2);
writer.write(System.getProperty("line.separator"));
writer.write("Nachname: " + Nachname2);
writer.write(System.getProperty("line.separator"));
writer.write("Email: "+Email2);
writer.write(System.getProperty("line.separator"));
writer.write(System.getProperty("line.separator"));
Vorname.setText("");
Nachname.setText("");
Email.setText("");
writer.close();
} catch (IOException e1) {
e1.printStackTrace(); }}}}}
public class FileReadWriteExample implements ActionListener {
{
try {
BufferedReader in = new BufferedReader(new FileReader("C:/Users/Finn/Desktop/Datenbank.txt"));
String zeile = null;
while ((zeile = in.readLine()) != null) {
text.setText("" + zeile);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
}