Advertisement
Guest User

arxel

a guest
Dec 20th, 2010
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5.  
  6. public class okienko extends JFrame {
  7.  
  8.     public okienko(){
  9.         super("okno");
  10.         setLayout( new BorderLayout());
  11.        
  12.         String[] lista = { "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12" };
  13.         String[] lista2 = { "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12" };
  14.        
  15.         //Tworzenie okienka
  16.         final JTextField text = new JTextField(20);
  17.         add( text, BorderLayout.NORTH);
  18.        
  19.         JPanel panel = new JPanel();
  20.         panel.setLayout( new GridLayout(1,2));
  21.         panel.add( new JLabel("Zapis"));
  22.         panel.add( new JLabel("Odczyt"));
  23.         add(panel, BorderLayout.CENTER);
  24.        
  25.         JPanel panel2 = new JPanel();
  26.         panel2.setLayout( new GridLayout(1,2));
  27.         final JComboBox zapisBox = new JComboBox(lista);
  28.        
  29.        
  30.        
  31.         //Zapis pliku
  32.         zapisBox.addActionListener( new ActionListener() {
  33.            
  34.             @Override
  35.             public void actionPerformed(ActionEvent e) {
  36.                 JFileChooser chooser = new JFileChooser();
  37.                   if (chooser.showSaveDialog(okienko.this) != JFileChooser.APPROVE_OPTION)
  38.                     return;
  39.                   File file = chooser.getSelectedFile();
  40.                   if (file == null)
  41.                     return;
  42.  
  43.                   FileWriter writer = null;
  44.                   try {
  45.                     writer = new FileWriter(file);
  46.                     text.write(writer);
  47.                   } catch (IOException ex) {
  48.                     JOptionPane.showMessageDialog(okienko.this,
  49.                         "Plik nie zostal zapisany", "ERROR", JOptionPane.ERROR_MESSAGE);
  50.                   } finally {
  51.                     if (writer != null) {
  52.                       try {
  53.                         writer.close();
  54.                       } catch (IOException x) {
  55.                       }
  56.                     }
  57.                   }        
  58.             }
  59.         });
  60.         panel2.add( zapisBox);
  61.         //Zapis pliku
  62.        
  63.        
  64.         //Odczyt pliku
  65.         final JComboBox odczytBox = new JComboBox(lista2);
  66.         odczytBox.addActionListener(new ActionListener() {
  67.            
  68.             @Override
  69.             public void actionPerformed(ActionEvent e) {
  70.                 JFileChooser chooser = new JFileChooser();
  71.                   if (chooser.showOpenDialog(okienko.this) != JFileChooser.APPROVE_OPTION)
  72.                     return;
  73.                   File file = chooser.getSelectedFile();
  74.                   if (file == null)
  75.                     return;
  76.                  
  77.                   FileReader reader = null;
  78.                   try {
  79.                     reader = new FileReader(file);
  80.                     text.read(reader, null);
  81.                   } catch (IOException ex) {
  82.                     JOptionPane.showMessageDialog(okienko.this,
  83.                         "Brak pliku", "ERROR", JOptionPane.ERROR_MESSAGE);
  84.                   } finally {
  85.                     if (reader != null) {
  86.                       try {
  87.                         reader.close();
  88.                       } catch (IOException x) {
  89.                       }
  90.                     }
  91.                   }
  92.                  
  93.                  
  94.             }
  95.         });
  96.         panel2.add( odczytBox);
  97.         //Odczyt pliku
  98.        
  99.    
  100.         add(panel2, BorderLayout.SOUTH);
  101.            
  102.        
  103.        
  104.         pack();
  105.         setVisible(true);
  106.        
  107.        
  108.        
  109.     }
  110.    
  111.     public static void main(String[] args) {
  112.        
  113.         new okienko();
  114.  
  115.     }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement