Facebook
From Beefy Sloth, 6 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 256
  1. package javaapplication5;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.filechooser.FileNameExtensionFilter;
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.File;
  9. import java.io.IOException;
  10.  
  11. //import java.io.FileWriter;
  12. //import java.io.IOException;
  13. //import java.io.FileInputStream;
  14. //import java.io.FileOutputStream;
  15. //import javax.swing.filechooser.FileSystemView;
  16.  
  17. /**
  18.  * @author Daxo
  19.  */
  20. public class Gui implements ActionListener {
  21.  
  22.   JFrame frame;
  23.   JTextField text1, text2, text3;
  24.   JLabel label1, label2, label3, label4;
  25.   JButton but1, but2, but3, but4;
  26.   Container mainPane;
  27.   JPanel panel1, panel2, panel3, panel4;
  28.   KopiujDane kopiuj = new KopiujDane();
  29.   File file1 = null;
  30.   File file2 = null;
  31.  
  32.   public Gui() { //W konstruktorze tworzymy GUI
  33.     frame = new JFrame("Kopiuj");
  34.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.     mainPane = frame.getContentPane(); //pobranie referencji głównego panelu ramki
  36.     mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); //dla mainPane użyjemy BoxLayout
  37.     frame.setMinimumSize(new Dimension(450, 250));
  38.  
  39.     panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
  40.  
  41.     JLabel label1 = new JLabel("ścieżka1 ");
  42.     panel1.add(label1);
  43.     text1 = new JTextField(20);
  44.     text1.setEditable(false); //blokowanie wpisania tekstu
  45.     but2 = new JButton("wybierz1"); //ten przycisk wyświetla pierwszą połowę napisu
  46.     but2.addActionListener(this);
  47.     panel1.add(but2);
  48.     panel1.add(text1);
  49.  
  50.     JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
  51.     JLabel label2 = new JLabel("ścieżka2 ");
  52.     panel2.add(label2);
  53.     text2 = new JTextField(20);
  54.     text2.setEditable(false);
  55.     but3 = new JButton("wybierz2"); //ten przycisk wyświetla pierwszą połowę napisu
  56.     but3.addActionListener(this);
  57.     panel2.add(but3);
  58.     panel2.add(text2);
  59.     panel3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
  60.     panel4 = new JPanel(new FlowLayout(FlowLayout.CENTER));
  61.     but1 = new JButton("kopiuj"); //ten przycisk wyświetla pierwszą połowę napisu
  62.     but1.addActionListener(this);
  63.     text3 = new JTextField(7);
  64.     JLabel label4 = new JLabel("ilość wierszy  ");
  65.     panel3.add(label4);
  66.     panel3.add(but1);
  67.     panel3.add(text3);
  68.     label3 = new JLabel("Wybierz pliki do kopiowania");
  69.     panel4.add(label3);
  70.  
  71.     mainPane.add(panel1);
  72.     mainPane.add(panel2);
  73.     mainPane.add(panel3);
  74.     mainPane.add(panel4);
  75.  
  76.     //FileChooser z filtrem na pliki txt - po przecinku można dodawać kolejne rozszerzenia -> "txt", "cfg");
  77.     fc = new JFileChooser();
  78.     FileNameExtensionFilter filter = new FileNameExtensionFilter("Pliki tekstowe", "txt"); //filtr plikow txt
  79.     fc.setFileFilter(filter);
  80.  
  81.     frame.pack();
  82.     frame.setVisible(true);
  83.   }
  84.  
  85.   @Override
  86.  
  87.  
  88.   public void actionPerformed(ActionEvent e) {
  89.     if (e.getActionCommand().equals("wybierz1")) {
  90.       int returnVal1 = fc.showOpenDialog(frame);
  91.  
  92.       if (returnVal1 == JFileChooser.APPROVE_OPTION) {
  93.         file1 = fc.getSelectedFile();
  94.  
  95.         label3.setText("Wybrano plik");
  96.  
  97.         text1.setText(fc.getSelectedFile().getPath());
  98.       } else {
  99.         label3.setText("Nie wybrano pliku");
  100.       }
  101.     }
  102.     if (e.getActionCommand().equals("wybierz2")) {
  103.       int returnVal2 = fc.showOpenDialog(frame);
  104.  
  105.       if (returnVal2 == JFileChooser.APPROVE_OPTION) {
  106.         file2 = fc.getSelectedFile();
  107.  
  108.         label3.setText("Wybrano plik");
  109.  
  110.         text2.setText(fc.getSelectedFile().getPath());
  111.         System.out.println(fc.getSelectedFile());
  112.       } else {
  113.         label3.setText("Niewybrano pliku ");
  114.       }
  115.     }
  116.     if (e.getActionCommand().equals("kopiuj")) {
  117.  
  118.       int lines;
  119.       try {
  120.         lines = Integer.valueOf(text3.getText());
  121.       } catch (IllegalArgumentException ex) {
  122.         label3.setText("Liczba wierszy musi być liczbą.");
  123.         return;
  124.       }
  125.  
  126.       try {
  127.         kopiuj.copy(file1, file2, lines);
  128.         label3.setText("Skopiowano!");
  129.       } catch (IllegalArgumentException ex) {
  130.         label3.setText(ex.getMessage());
  131.       } catch (IOException e1) {
  132.         label3.setText("Wystąpił błąd wejścia/wyjścia.");
  133.       }
  134.     }
  135.  
  136.   }
  137. }
  138.    
  139.