popis:triedy funguju aj samostatne ----------------------------------------------------------------------------------------------------------------------- import javax.swing.*; import java.io.*; public class FileOutput_1 extends JFrame //implements ActionListener { private JTextArea textArea; private JButton saveButton; private JTextField nameField; private JLabel nameLabel; private PrintWriter outFile; /** * Konštruktor - Okno s titulkom * @param title nadpis v titulkovej lište */ public FileOutput_1(String title) { super(title); } public static void main(String [] args) { FileOutput_1 frame = new FileOutput_1("Príklad na výstup do súboru"); frame.setSize(600, 400); frame.vytvorGUI(); frame.setVisible(true); } private void vytvorGUI() { } } -------------------------------------------------------------------------------------------------------------------- import javax.swing.*; import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; public class FileOutput extends JFrame implements ActionListener { private JTextArea textArea; private JButton saveButton; private JTextField nameField; private JLabel nameLabel; private PrintWriter outFile; // /** * Konštruktor - Okno s titulkom * @param title nadpis v titulkovej lište */ public FileOutput(String title) { super(title); } public static void main(String [] args) { FileOutput frame = new FileOutput("Príklad na výstup do súboru"); frame.setSize(600, 400); frame.vytvorGUI(); frame.setVisible(true); } private void vytvorGUI() { setDefaultCloseOperation(EXIT_ON_CLOSE); // pre ukončenie aplikácie uzavretím okna Container okno = getContentPane(); // okno aplikácie okno.setLayout(new FlowLayout()); // Layout manager= Flow //---- Vytvorenie Label návestia pre meno súboru --- nameLabel = new JLabel("Meno súboru: "); okno.add(nameLabel); //---- Vytvorenie okienka návestia pre meno súboru --- nameField = new JTextField(20); okno.add(nameField); //---- Vytvorenie textového okna --- textArea = new JTextArea(18, 50); JScrollPane scrollPane = new JScrollPane(textArea); okno.add(scrollPane); //---- Vytvorenie tlačítka pre akciu --- saveButton = new JButton("Ulož"); okno.add(saveButton); saveButton.addActionListener(this); } public void actionPerformed(ActionEvent event) { if (event.getSource() == saveButton) { try { //-- Otvorí súbor menom v textovom poli nameField: outFile = new PrintWriter(new FileWriter(nameField.getText()), true); //-- Celý text okna textArea je odoslaný do otvoreného súboru: outFile.print(textArea.getText()); outFile.close(); //-- súbor sa uzatvorí (finalizuje) JOptionPane.showMessageDialog(null, "Zapísané do súboru : " + nameField.getText()); } catch (IOException e) { JOptionPane.showMessageDialog(null, "Chyba súboru: " + e.toString()); } } } } ------------------------------------------------------------------------------------------------------------------- import javax.swing.*; import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; public class FileInput extends JFrame implements ActionListener { private JTextArea textArea; private JButton openButton; private JTextField nameField; private JLabel nameLabel; private BufferedReader inFile; // /** * Konštruktor - Okno s titulkom * @param title nadpis v titulkovej lište */ public FileInput(String title) { super(title); } public static void main(String [] args) { FileInput frame = new FileInput("Príklad na vstup zo súboru"); frame.setSize(600, 400); frame.vytvorGUI(); frame.setVisible(true); } private void vytvorGUI() { setDefaultCloseOperation(EXIT_ON_CLOSE); // pre ukončenie aplikácie uzavretím okna Container okno = getContentPane(); // okno aplikácie okno.setLayout(new FlowLayout()); // Layout manager= Flow //---- Vytvorenie Label návestia pre meno súboru --- nameLabel = new JLabel("Meno súboru: "); okno.add(nameLabel); //---- Vytvorenie okienka návestia pre meno súboru --- nameField = new JTextField(20); okno.add(nameField); nameField.addActionListener(this); //---- Vytvorenie textového okna --- textArea = new JTextArea("", 18, 50); JScrollPane scrollPane = new JScrollPane(textArea); okno.add(scrollPane); //---- Vytvorenie tlačítka pre akciu --- openButton = new JButton("Čítaj"); okno.add(openButton); openButton.addActionListener(this); } public void actionPerformed(ActionEvent event) { if (event.getSource() == openButton) { try { //-- Otvorí súbor menom v textovom poli nameField: inFile = new BufferedReader(new FileReader(nameField.getText())); textArea.setText(""); // vymaže vstupné pole String line; while ( (line = inFile.readLine()) != null ) { // číta všetky riadky textArea.append(line + "\n"); // ukladá ich do textového pola } inFile.close(); } catch (IOException e) { JOptionPane.showMessageDialog(null, "Chyba súboru: " + e.toString()); } } } }
Autor: Marek Tomčík www: http:// Kategória: Dátové abstrakcie Jazyk: Java Dátum:4/26/2010 11:26:51 AM
Pre vloženie komentáru sa musíte najprv prihlásiť.