Java JFrame geschriebende dinge in String speichern und weiterverwenden?

... komplette Frage anzeigen

1 Antwort

Habe das Problem nun gelößt für die die es Intressiert hier ist der Code.

package frederik;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Overlay extends JFrame {

private static final long serialVersionUID = 1L;
private JLabel lbVorname, lbNachname, lbAlter, lbWohnort, lbID;
private JTextField tfVorname, tfNachname, tfAlter, tfWohnort, tfID;
private String test1, test2, test3, test4, test5;
private FileWriter fw;
private int i = 0;
Person[] personen = new Person[2];

public static void main(String[] args) {
new Overlay();
}

public Overlay() {
setTitle("Personenverwaltung");
setSize(500, 500);
setLocation(10, 10);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);

createLables();
createButtons1();
createTextfields();
createButtons2();
createButtons3();
setVisible(true);
}

private void createLables() {
lbID = new JLabel("ID");
lbID.setBounds(10, 10, 150, 20);
add(lbID);

lbVorname = new JLabel("Vorname:");
lbVorname.setBounds(10, 40, 150, 20);
add(lbVorname);

lbNachname = new JLabel("Nachname:");
lbNachname.setBounds(10, 70, 150, 20);
add(lbNachname);

lbAlter = new JLabel("Alter:");
lbAlter.setBounds(10, 100, 150, 20);
add(lbAlter);

lbWohnort = new JLabel("Wohnort:");
lbWohnort.setBounds(10, 130, 150, 20);
add(lbWohnort);
}

private void createTextfields() {
tfID = new JTextField();
tfID.setBounds(170, 10, 125, 20);
add(tfID);

tfVorname = new JTextField();
tfVorname.setBounds(170, 40, 250, 20);
add(tfVorname);

tfNachname = new JTextField();
tfNachname.setBounds(170, 70, 250, 20);
add(tfNachname);

tfAlter = new JTextField();
tfAlter.setBounds(170, 100, 250, 20);
add(tfAlter);

tfWohnort = new JTextField();
tfWohnort.setBounds(170, 130, 250, 20);
add(tfWohnort);
}

private void createButtons1() {
JButton btnSpeichern = new JButton("Speichern");
btnSpeichern.setBounds(10, 150, 150, 30);
btnSpeichern.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
test1 = tfID.getText();
test2 = tfVorname.getText();
test3 = tfNachname.getText();
test4 = tfAlter.getText();
test5 = tfWohnort.getText();

try {
fw = new FileWriter("C:\\\\Users\\\\prakit\\\\Desktop\\\\test.txt", true);
fw.write("ID:" + test1 + "\\n");
fw.write("Vorname:" + test2 + "\\n");
fw.write("Nachname:" + test3 + "\\n");
fw.write("Alter:" + test4 + "\\n");
fw.write("Wohnort:" + test5 + "\\n");
fw.write("---------------------------------------" + "\\n");
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

add(btnSpeichern);
}

private void createButtons2() {
JButton btnNextPage = new JButton("<");
btnNextPage.setBounds(295, 10, 62, 20);
add(btnNextPage);
}
private void createButtons3() {
JButton btnNextPage = new JButton(">");
btnNextPage.setBounds(357, 10, 62, 20);
add(btnNextPage);
}

}

Antwort bewerten Vielen Dank für Deine Bewertung

Was möchtest Du wissen?