Hallo,
ich hab ein Problem und krieg es einfach nicht gelöst
Ich habe eine TextArea, welche ich gerne in einer Datei speichern möchte (nicht überschreiben sondern am Ende anhängen!!)... Hat da jemand ein Plan, bei allem was ich versucht habe kamen immer unglaublich viele Fehler
ganz davon abgesehen, dass diese Methoden immer nur eine Zeile eingelesen hätten!
Im Prinzip würde ich gerne die gesamte TextArea in einer Datei (Endung anpassbar) speichern und dabei anhängen (s.o.)! Die Datei muss aber nicht ausgelesen werden und wieder in der TextArea ausgegeben werden! Habe den Quelltext ein bisschen gekürzt, aber nix wichtiges
Wenn jemand den ganzen Quelltext braucht (zum Compilieren oder so): kein Problem 
P.S. Ist übrigens keine Hausaufgabe! Wäre auch viel zu unsauber programmiert :lol:
viele grüße
ich hab ein Problem und krieg es einfach nicht gelöst
Im Prinzip würde ich gerne die gesamte TextArea in einer Datei (Endung anpassbar) speichern und dabei anhängen (s.o.)! Die Datei muss aber nicht ausgelesen werden und wieder in der TextArea ausgegeben werden! Habe den Quelltext ein bisschen gekürzt, aber nix wichtiges
Java:
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;
import java.lang.*;
import javax.swing.event.*;
import java.io.*;
/**
*
* Beschreibung
*
* @version 1.0 vom 05.11.2012
* @author
*/
public class Fenster_Tastureingabe extends Frame implements KeyListener {
// Anfang Attribute
public boolean end = false;
private Label label_timeNow_titel = new Label();
private Label label_timeNow = new Label();
private static Label label_time = new Label();
private static boolean timeBoolean = true;
private Label label_titel = new Label();
private TextArea LABEL = new TextArea("", 1, 1, TextArea.SCROLLBARS_BOTH);
private Button button_start = new Button();
private Button button_clear = new Button();
private TextField textField_reason = new TextField();
public int count = 0;
private Button button_stop = new Button();
public int yyyyS1;
public int MMS1;
public int ddS1;
public int kkS1;
public int mmS1;
public int ssS1;
public int yyyy;
public double MM;
public double dd;
public double kk;
public double mm;
public double ss;
public int S;
public int E;
private Label label_error = new Label();
public int countSek = 0;
public int countSave;
private Button button_total = new Button();
private Button button_preferencesC = new Button();
private Button button_disableClock = new Button();
private Button button_setFileExtension = new Button();
private Button button_save = new Button();
private Button button_preferencesO = new Button();
private Button button_enableClock = new Button();
private TextField textField1 = new TextField();
public String extension;
// Ende Attribute
public Fenster_Tastureingabe(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { dispose(); }
});
int frameWidth = 1008;
int frameHeight = 361;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Panel cp = new Panel(null);
add(cp);
this.addKeyListener(this);
// Anfang Komponenten
label_timeNow_titel.setBounds(8, 8, 307, 72);
label_timeNow_titel.setText("WorkMeter");
label_timeNow_titel.setFont(new Font("Arial Black", Font.BOLD, 48));
label_timeNow.setBounds(328, 8, 75, 25);
label_timeNow.setText("Aktuelle Zeit:");
cp.add(label_timeNow);
label_time.setBounds(408, 8, 331, 25);
label_time.setText("");
cp.add(label_time);
label_titel.setBounds(32, 16, 144, 30);
label_titel.setText("Work Meter");
label_titel.setFont(new Font("Arial Narrow", Font.BOLD, 22));
cp.add(label_titel);
LABEL.setBounds(16, 72, 385, 217);
cp.add(LABEL);
button_start.setBounds(488, 120, 113, 49);
button_start.setLabel("Start");
button_start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_start_ActionPerformed(evt);
}
});
cp.add(button_start);
button_clear.setBounds(424, 184, 113, 49);
button_clear.setLabel("Clear");
button_clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_clear_ActionPerformed(evt);
}
});
cp.add(button_clear);
textField_reason.setBounds(416, 80, 249, 33);
cp.add(textField_reason);
button_stop.setBounds(488, 120, 113, 49);
button_stop.setLabel("Stop");
button_stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_stop_ActionPerformed(evt);
}
});
button_stop.setVisible(false);
cp.add(button_stop);
label_error.setBounds(416, 248, 555, 41);
label_error.setText("");
label_error.setFont(new Font("Arial", Font.PLAIN, 14));
label_error.setForeground(Color.RED);
cp.add(label_error);
button_total.setBounds(552, 184, 113, 49);
button_total.setLabel("Total Time");
button_total.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_total_ActionPerformed(evt);
}
});
cp.add(button_total);
button_preferencesC.setBounds(848, 0, 153, 33);
button_preferencesC.setLabel("Preferences");
button_preferencesC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_preferencesC_ActionPerformed(evt);
}
});
cp.add(button_preferencesC);
button_disableClock.setBounds(864, 40, 137, 33);
button_disableClock.setLabel("Disable Clock");
button_disableClock.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_disableClock_ActionPerformed(evt);
}
});
cp.add(button_disableClock);
button_disableClock.setVisible(false);
button_setFileExtension.setBounds(864, 72, 137, 33);
button_setFileExtension.setLabel("Set File Extension");
button_setFileExtension.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_setFileExtension_ActionPerformed(evt);
}
});
cp.add(button_setFileExtension);
button_setFileExtension.setVisible(false);
button_save.setBounds(848, 160, 153, 33);
button_save.setLabel("Save");
button_save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_save_ActionPerformed(evt);
}
});
cp.add(button_save);
button_preferencesO.setBounds(848, 0, 153, 33);
button_preferencesO.setLabel("Preferences");
button_preferencesO.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_preferencesO_ActionPerformed(evt);
}
});
cp.add(button_preferencesO);
button_enableClock.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_enableClock_ActionPerformed(evt);
}
});
button_enableClock.setBounds(864, 40, 137, 33);
button_enableClock.setLabel("Enable Clock");
button_enableClock.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button_enableClock_ActionPerformed(evt);
}
});
cp.add(button_enableClock);
button_enableClock.setVisible(false);
textField1.setBounds(864, 104, 137, 33);
textField1.setText(".txt");
textField1.setFont(new Font("Arial", Font.PLAIN, 26));
textField1.setVisible(false);
cp.add(textField1);
// Ende Komponenten
setVisible(true);
// end of public Fenster_Tastureingabe
}
// Anfang Methoden
public void button_start_ActionPerformed(ActionEvent evt) {
ausgabe();
} // end of button_start_ActionPerformed
public void button_stop_ActionPerformed(ActionEvent evt) {
stop();
} // end of button_stop_ActionPerformed
public void button_clear_ActionPerformed(ActionEvent evt) {
LABEL.setText("");
} // end of button_clear_ActionPerformed
public void button_total_ActionPerformed(ActionEvent evt) {
LABEL.setText(LABEL.getText()+"\n"+"\n"+"total time: "+countSave);
} // end of button_total_ActionPerformed
public void button_preferencesC_ActionPerformed(ActionEvent evt) {
button_disableClock.setVisible(true);
button_setFileExtension.setVisible(true);
button_enableClock.setVisible(false);
} // end of button_preferencesC_ActionPerformed
public void button_preferencesO_ActionPerformed(ActionEvent evt) {
button_disableClock.setVisible(false);
button_setFileExtension.setVisible(false);
button_enableClock.setVisible(false);
textField1.setVisible(false);
} // end of button_preferencesO_ActionPerformed
public void button_disableClock_ActionPerformed(ActionEvent evt) {
timeBoolean = false;
button_enableClock.setVisible(true);
button_disableClock.setVisible(false);
label_time.setText("disabled");
} // end of button_disableClock_ActionPerformed
public void button_enableClock_ActionPerformed(ActionEvent evt) {
button_enableClock.setVisible(false);
button_disableClock.setVisible(true);
timeBoolean = true;
} // end of button_enableClock_ActionPerformed
public void button_setFileExtension_ActionPerformed(ActionEvent evt) {
extension = textField1.getText();
textField1.setVisible(true);
} // end of button_setFileExtension_ActionPerformed
public void button_save_ActionPerformed(ActionEvent evt) {
label_error.setForeground(Color.GREEN);
label_error.setText("saving in progress...");
} // end of button_save_ActionPerformed
public static void time()
{
while (timeBoolean == true) {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy kk:mm:ss, z");
Timestamp zeit_a = new Timestamp(System.currentTimeMillis());
String time_a = sdf.format(zeit_a);
label_time.setText(""+time_a);
} // end of while
}
public void ausgabe()
{
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy kk:mm:ss, z");
Timestamp zeit = new Timestamp(System.currentTimeMillis());
String time = sdf.format(zeit);
LABEL.setText(LABEL.getText()+"\n"+time+": "+textField_reason.getText());
button_start.setVisible(false);
button_stop.setVisible(true);
end = false;
count = 0;
countSek = 0;
calculate();
}
public void stop()
{
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy kk:mm:ss, z");
Timestamp zeit = new Timestamp(System.currentTimeMillis());
String time = sdf.format(zeit);
button_start.setVisible(true);
button_stop.setVisible(false);
end = true;
calculate();
LABEL.setText(LABEL.getText()+"\n"+"to"+"\n"+time+"\n"+"Duration:"+count+"\n");
label_error.setText("");
countSave = countSave+count;
}
public void calculate()
{
//rechnet die Differenz zwischen dem Start- und Enddatum aus!
}
}
public void keyPressed(KeyEvent evt)
{
if (evt.getKeyCode() == KeyEvent.VK_ENTER && end == false) {
ausgabe();
} // end of while
if (evt.getKeyCode() == KeyEvent.VK_ENTER && end == true) {
stop();
} // end of if
if (evt.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
LABEL.setText("");
} // end of if
}
public void keyTyped(KeyEvent evt)
{
}
public void keyReleased(KeyEvent evt)
{
}
public static void main(String[] args) {
new Fenster_Tastureingabe("Workmeter");
time();
} // end of main
// Ende Methoden
} // end of class Fenster_Tastureingabe
P.S. Ist übrigens keine Hausaufgabe! Wäre auch viel zu unsauber programmiert :lol:
viele grüße