Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- public class okienko extends JFrame {
- public okienko(){
- super("okno");
- setLayout( new BorderLayout());
- String[] lista = { "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12" };
- String[] lista2 = { "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12" };
- //Tworzenie okienka
- final JTextField text = new JTextField(20);
- add( text, BorderLayout.NORTH);
- JPanel panel = new JPanel();
- panel.setLayout( new GridLayout(1,2));
- panel.add( new JLabel("Zapis"));
- panel.add( new JLabel("Odczyt"));
- add(panel, BorderLayout.CENTER);
- JPanel panel2 = new JPanel();
- panel2.setLayout( new GridLayout(1,2));
- final JComboBox zapisBox = new JComboBox(lista);
- //Zapis pliku
- zapisBox.addActionListener( new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- if (chooser.showSaveDialog(okienko.this) != JFileChooser.APPROVE_OPTION)
- return;
- File file = chooser.getSelectedFile();
- if (file == null)
- return;
- FileWriter writer = null;
- try {
- writer = new FileWriter(file);
- text.write(writer);
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(okienko.this,
- "Plik nie zostal zapisany", "ERROR", JOptionPane.ERROR_MESSAGE);
- } finally {
- if (writer != null) {
- try {
- writer.close();
- } catch (IOException x) {
- }
- }
- }
- }
- });
- panel2.add( zapisBox);
- //Zapis pliku
- //Odczyt pliku
- final JComboBox odczytBox = new JComboBox(lista2);
- odczytBox.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- if (chooser.showOpenDialog(okienko.this) != JFileChooser.APPROVE_OPTION)
- return;
- File file = chooser.getSelectedFile();
- if (file == null)
- return;
- FileReader reader = null;
- try {
- reader = new FileReader(file);
- text.read(reader, null);
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(okienko.this,
- "Brak pliku", "ERROR", JOptionPane.ERROR_MESSAGE);
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException x) {
- }
- }
- }
- }
- });
- panel2.add( odczytBox);
- //Odczyt pliku
- add(panel2, BorderLayout.SOUTH);
- pack();
- setVisible(true);
- }
- public static void main(String[] args) {
- new okienko();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement