Witam,
Mam aplikację, w której zrobiłem dwa obszary: Etykieta, Aktor. Jest zapis i odczyt do jednego plik. Pisze tekst w oddzielnych obszarach, ale później odczyt jest tylko w 1 obszarze - Etykieta. Czy jest jakaś możliwość poprawy tego tzn. żeby poprawnie wczytywało tekst.
Z góry dziękuję za pomoc.
Kod:
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Aplikacja extends JFrame implements ActionListener {
private JTextArea textArea, textArea2;
JLabel label, label2;
public Aplikacja(){
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setTitle("Edytor tekstu");
setLayout(null);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Plik");
JMenuItem miOtworz = new JMenuItem("Otworz");
miOtworz.addActionListener(this);
miOtworz.setActionCommand("Open");
JMenuItem miZapisz = new JMenuItem("Zapisz jako...");
miZapisz.addActionListener(this);
miZapisz.setActionCommand("Save");
JMenuItem miZamknij = new JMenuItem("Zamknij");
miZamknij.addActionListener(this);
miZamknij.setActionCommand("Close");
menu.add(miOtworz);
menu.add(miZapisz);
menu.add(new JSeparator());
menu.add(miZamknij);
label = new JLabel("Etykieta");
label.setBounds(10, 10, 50, 20);
this.add(label);
textArea = new JTextArea();
textArea.setBounds(90, 10, 200, 100);
add(textArea);
label2 = new JLabel("Aktor");
label2.setBounds(10, 120, 50, 20);
this.add(label2);
textArea2 = new JTextArea();
textArea2.setBounds(90, 120, 200, 100);
add(textArea2);
menuBar.add(menu);
setJMenuBar(menuBar);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
if (command.equals("Close")){
dispose();
}
else if(command.equals("Open")){
JFileChooser fc = new JFileChooser();
if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
open(fc.getSelectedFile());
}
}
else if(command.equals("Save")){
JFileChooser fc = new JFileChooser();
if(fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
save(fc.getSelectedFile());
}
}
}
public void open(File file){
FileInputStream fin = null;
try{
fin = new FileInputStream(file);
}
catch(FileNotFoundException e){
JOptionPane.showMessageDialog(this, "Błąd przy otwieraniu pliku.", "Blad", JOptionPane.ERROR_MESSAGE);
return;
}
BufferedReader inbr = new BufferedReader(new InputStreamReader(fin));
textArea.setText("");
textArea2.setText("");
String line = "", line2 = "";
try{
while ((line = inbr.readLine()) != null){
textArea.append(line + '\n');
}
while ((line2 = inbr.readLine()) != null){
textArea2.append(line2 + '\n');
}
}
catch(IOException e){
JOptionPane.showMessageDialog(this, "Błąd wejścia-wyjścia.", "Blad", JOptionPane.ERROR_MESSAGE);
}
}
public void save(File file){
FileOutputStream fout = null;
try{
fout = new FileOutputStream(file);
}
catch(FileNotFoundException e){
JOptionPane.showMessageDialog(this, "Błąd przy zapisie pliku.", "Blad", JOptionPane.ERROR_MESSAGE);
return;
}
DataOutputStream out = new DataOutputStream(fout);
try{
String line = textArea.getText();
out.writeBytes(line + '\n');
String line2 = textArea2.getText();
out.writeBytes(line2 + '\n');
}
catch(IOException e){
JOptionPane.showMessageDialog(this, "Błąd wejścia-wyjścia.", "Blad", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Aplikacja();
}
});
}
}