import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Locale;
public class Main extends JFrame implements ActionListener {
private JPanel mainpanel; //panel
private JScrollPane scrollPane; //Scroll Bars für das TextArea
private JTextArea textReader; //TextArea
private JButton buttonReader;
private JButton buttonSaver;
private JButton resetButton;
private JButton textmanipulationButton;
private String text;
private String filesave;
public static void main(String[] args) {
new Main();
} //Start der Main
Main(){
this.setContentPane(mainpanel);
scrollPane.setViewportView(textReader); //ScrollPane wird mit dem textReader verknüpft
buttonReader.setFocusable(false); //optische Anpassung
buttonReader.addActionListener(this); //ActionLister für Read Button erstellen
textmanipulationButton.setFocusable(false);
textmanipulationButton.addActionListener(this);
buttonSaver.setFocusable(false);
buttonSaver.addActionListener(this);
resetButton.setFocusable(false);
resetButton.addActionListener(this);
this.setBounds(650,200,600,600); //Position und Größe festlegen
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //Kontrolliertes Programmende
this.setVisible(true); //GUI sichtbar machen
}
@Override
public void actionPerformed(ActionEvent e) { //Aktionen der Benutzersteuerung werden festgelegt
if(e.getSource() == buttonReader) { //welcher Button wurde gedrückt
JFileChooser chooser = new JFileChooser(); //Fenster zum Öffnen von Dateien wird implementiert
chooser.showOpenDialog(null); //Fenster wird geöffnet
File file = chooser.getSelectedFile(); //String filename = file.getAbsolutePath();
filesave = file.getAbsolutePath(); //überträgt den Pfad in eine String-Variable
try { //Vorgeschriebenes try...catch
BufferedReader br = new BufferedReader(new FileReader(file)); //Öffnen und Einlesen der Datei
textReader.read(br, null); //schreiben des Dateiinhaltes in den textReader
textReader.append("\n");
br.close(); //Datei muss geschlossen werden
//textReader.requestFocus();
}
catch (Exception exception) {
JOptionPane.showMessageDialog(null, exception);
}
}
if(e.getSource() == textmanipulationButton) {
text = textReader.getText();
text = text.replaceAll("[ö,Ö]","oe");
// textReader.append(text+"\n");
text = text.replaceAll("[ü,Ü]","ue");
//textReader.append(text+"\n");
text = text.replaceAll("[ä,Ä]","ae");
// textReader.append(text+"\n");
text = text.replaceAll("ß","ss");
//textReader.append(text+"\n");
text = text.replaceAll(" ","");
//textReader.append(text+"\n");
// text = text.toUpperCase(Locale.ROOT);
// textReader.append(text+"\n");
// text = text.replaceAll("[^A-Z]","");
text = text.replaceAll("[^A-Z,a-z]","");
// textReader.append(text+"\n");
text = text.toUpperCase(Locale.ROOT);
textReader.append(text+"\n");
char [][] matrix = new char[5][5];
int length = text.length();
for(int y = 0; y < 5; y++) {
for(int x = 0; x < 5; x++)
{
int Index = x + y*5;
matrix[x][y] = text.charAt(Index);
System.out.print(matrix[x][y] + " ");
};
System.out.print("\n");
}
}
//matrix new array
if(e.getSource() == buttonSaver){
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(filesave));
text = textReader.getText();
bw.write(textReader.getText());
bw.close();
JOptionPane.showMessageDialog(null, "File successful saved");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
if(e.getSource() == resetButton){
textReader.setText("");
filesave = "";
}
}
}
//[^0-9] = take everything except from 0 to 9