import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Seminar {
private JLabel jLabelname, jLabelbezeichnung, jLabelWohnort, jLabelTelefon,
jLabelHobbys;
private JTextField jTfVorname, jTfNachname, jTfBezeichnung, jTfWohnort,
jTfTelefon;
private JButton jBSpeichern, jBReset;
private JCheckBoxMenuItem jCBMITanzen, jCBMISport, jCBMIGarten;
private JMenuBar jMenuBar;
private JMenu jMdatei;
private JMenuItem jMISpeichern, jMIReset, jMIExit;
private JFrame jFrame;
private JPanel jPanel;
private RandomAccessFile raf;
public static void main(String[] args) {
new Seminar();
}
public Seminar() {
initFrameAndPanel();
initLabels();
initButtons();
initCheckBox();
initTextFields();
initAndAddMenuBar();
addComponentsToPanel();
jFrame.pack();
jFrame.setVisible(true);
}
private void initAndAddMenuBar() {
jMenuBar = new JMenuBar();
jMdatei = new JMenu("Datei");
jMISpeichern = new JMenuItem("Speichern");
jMISpeichern.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
speichern();
}
});
jMIExit = new JMenuItem("Exit");
jMIExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jMIReset = new JMenuItem("Reset");
jMIReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset();
}
});
jMdatei.add(jMISpeichern);
jMdatei.add(jMIReset);
jMdatei.addSeparator();
jMdatei.add(jMIExit);
jMenuBar.add(jMdatei);
jFrame.setJMenuBar(jMenuBar);
}
private void addComponentsToPanel() {
GridBagConstraints a = new GridBagConstraints();
a.gridx = 0;
a.gridy = 0;
a.insets = new Insets(20, 5, 5, 5);
a.anchor = GridBagConstraints.WEST;
jPanel.add(jLabelname, a);
GridBagConstraints b = new GridBagConstraints();
b.gridx = 1;
b.gridy = 0;
b.insets = new Insets(5, 5, 5, 15);
jPanel.add(jTfVorname, b);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 0;
c.insets = new Insets(5, 5, 5, 15);
c.anchor = GridBagConstraints.EAST;
jPanel.add(jTfNachname, c);
GridBagConstraints d = new GridBagConstraints();
d.gridx = 0;
d.gridy = 1;
d.insets = new Insets(5, 5, 5, 15);
jPanel.add(jLabelbezeichnung, d);
GridBagConstraints e = new GridBagConstraints();
e.gridx = 0;
e.gridy = 1;
e.insets = new Insets(5, 5, 5, 15);
jPanel.add(jLabelbezeichnung, e);
GridBagConstraints f = new GridBagConstraints();
f.gridx = 1;
f.gridwidth = 2;
f.gridy = 1;
f.insets = new Insets(5, 5, 5, 15);
jPanel.add(jTfBezeichnung, f);
GridBagConstraints g = new GridBagConstraints();
g.gridx = 0;
g.gridy = 2;
g.insets = new Insets(5, 5, 5, 15);
g.anchor = GridBagConstraints.WEST;
jPanel.add(jLabelWohnort, g);
GridBagConstraints h = new GridBagConstraints();
h.gridx = 1;
h.gridwidth = 2;
h.gridy = 2;
h.insets = new Insets(5, 5, 5, 15);
jPanel.add(jTfWohnort, h);
GridBagConstraints i = new GridBagConstraints();
i.gridx = 0;
i.gridy = 3;
i.insets = new Insets(5, 5, 5, 15);
i.anchor = GridBagConstraints.WEST;
jPanel.add(jLabelTelefon, i);
GridBagConstraints j = new GridBagConstraints();
j.gridx = 1;
j.gridwidth = 2;
j.gridy = 3;
j.insets = new Insets(5, 5, 5, 15);
jPanel.add(jTfTelefon, j);
GridBagConstraints k = new GridBagConstraints();
k.gridx = 0;
k.gridy = 4;
k.insets = new Insets(5, 5, 5, 15);
k.anchor = GridBagConstraints.WEST;
jPanel.add(jLabelHobbys, k);
GridBagConstraints l = new GridBagConstraints();
l.gridx = 2;
l.gridy = 4;
l.insets = new Insets(5, 15, 5, 15);
l.anchor = GridBagConstraints.WEST;
jPanel.add(jCBMIGarten, l);
GridBagConstraints m = new GridBagConstraints();
m.gridx = 2;
m.gridy = 5;
m.insets = new Insets(5, 15, 5, 15);
m.anchor = GridBagConstraints.WEST;
jPanel.add(jCBMISport, m);
GridBagConstraints n = new GridBagConstraints();
n.gridx = 2;
n.gridy = 6;
n.insets = new Insets(5, 15, 5, 15);
n.anchor = GridBagConstraints.WEST;
jPanel.add(jCBMITanzen, n);
GridBagConstraints o = new GridBagConstraints();
o.gridx = 1;
o.gridy = 7;
o.insets = new Insets(5, 5, 5, 15);
o.anchor = GridBagConstraints.CENTER;
jPanel.add(jBSpeichern, o);
GridBagConstraints p = new GridBagConstraints();
p.gridx = 2;
p.gridy = 7;
p.insets = new Insets(5, 15, 5, 50);
p.anchor = GridBagConstraints.WEST;
p.fill = GridBagConstraints.BOTH;
jPanel.add(jBReset, p);
}
private void initCheckBox() {
jCBMIGarten = new JCheckBoxMenuItem("Garten");
jCBMISport = new JCheckBoxMenuItem("Sport");
jCBMITanzen = new JCheckBoxMenuItem("Tanzen");
}
private void initTextFields() {
jTfBezeichnung = new JTextField(25);
jTfNachname = new JTextField(10);
jTfVorname = new JTextField(10);
jTfWohnort = new JTextField(25);
jTfTelefon = new JTextField(25);
reset();
}
private void initLabels() {
jLabelname = new JLabel("Name");
jLabelbezeichnung = new JLabel("Bezeichnung");
jLabelWohnort = new JLabel("Wohnort");
jLabelTelefon = new JLabel("Telefon");
jLabelHobbys = new JLabel("Hobbys");
}
private void initFrameAndPanel() {
jFrame = new JFrame("Seminar");
jFrame.setSize(new Dimension(500, 500));
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jFrame.getContentPane().add(jPanel);
}
private void initButtons() {
jBSpeichern = new JButton("Speichern");
jBReset = new JButton("Reset");
jBSpeichern.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
speichern();
}
});
jBReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset();
}
});
}
private void speichern() {
String[] daten = new String[8];
daten = copyDaten(daten);
try {
raf = new RandomAccessFile(
"C://Dokumente und Einstellungen//Janes//Desktop//daten.txt",
"rw");
raf.seek(raf.length());
for (int i = 0; i < daten.length; i++) {
raf.writeUTF(daten[i]);
}
JOptionPane.showMessageDialog(jFrame, "Der Teilnehmer " + daten[0]
+ " " + daten[1] + " wurde erfolgreich gespeichert",
"Speichern erfolgreich", JOptionPane.INFORMATION_MESSAGE);
reset();
raf.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(jFrame, e.getMessage());
e.printStackTrace();
} catch (IOException e) {
JOptionPane.showMessageDialog(jFrame, e.getMessage());
e.printStackTrace();
}
}
private String[] copyDaten(String[] daten) {
daten[0] = jTfVorname.getText();
daten[1] = jTfNachname.getText();
daten[2] = jTfBezeichnung.getText();
daten[3] = jTfWohnort.getText();
daten[4] = jTfTelefon.getText() + ",";
daten[5] = "garten=" + jCBMIGarten.isSelected();
daten[6] = "sport=" + jCBMISport.isSelected();
daten[7] = "tanzen=" + jCBMITanzen.isSelected() + "\r\n";
return daten;
}
private void reset() {
jTfVorname.setText("Vorname");
jTfNachname.setText("Nachname");
jTfBezeichnung.setText("Berufsbezeichnung");
jTfWohnort.setText("PLZ, Ort, Straße, Hausnummer");
jTfTelefon.setText("Telefonnummer");
jCBMIGarten.setSelected(false);
jCBMISport.setSelected(false);
jCBMITanzen.setSelected(false);
}
}