Deprecated: Function set_magic_quotes_runtime() is deprecated in /DISK2/WWW/blue-team.org/bcblog/textpattern/lib/txplib_db.php on line 14 Bc. BLOG: Semestrálka z Javy - dokončeno!

Sobota prosinec 22, 2007

Semestrálka z Javy - dokončeno!

· rubrika Clanky · kategorie

Tento týden jsme dostali zadání semestrální práce z Javy. Jak jsem se s tím vypořádal můžete vidět níže. Ještě to není úplně hotové, nemám totiž dořešené ukončení spuštěného vlákna!

EDIT: Již je to plně funkční

Toto je zdroják hlavního souboru "main.java":

Java - main.java

  1. package ksi.tul.cz.jan.kahoun;
  2.  
  3. public class main {
  4.  
  5.   public static void main(String[] args) {
  6.        
  7.     //vytvorime nove okno a nastavime mu parametry
  8.     window okno1= new window("Semestralni prace", 800, 500);
  9.    
  10.     okno1.setVisible(true);
  11.  
  12.   }
  13.  
  14. }
Zde je zdroják souboru "window.java"

Java - window.java

  1. package ksi.tul.cz.jan.kahoun;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.File;
  8. import java.io.IOException;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFileChooser;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.JProgressBar;
  16. import javax.swing.JScrollPane;
  17. import javax.swing.JTextArea;
  18. import javax.swing.JTextField;
  19.  
  20. public class window extends JFrame implements ActionListener{
  21.  
  22.   private JButton btnStart, btnStop, btnExit, btnFile1, btnFile2;
  23.   private JLabel label1,label2,label3;
  24.   private JTextField textfield1,textfield2,textfield3;
  25.   private JPanel panel1,panel2, panel3, panel4, panelMain;
  26.   private JTextArea textarea;
  27.   private JProgressBar bar;
  28.   private File dir1, dir2;
  29.   private String extension;
  30.   private boolean editable = false;
  31.   private thread proc;
  32.   private Thread procThread;
  33.  
  34.   public window(String title, int width, int height){
  35.    
  36.     /*
  37.      *
  38.      *  NASTAVENI GRAFICKEHO ROZHRANI
  39.      *
  40.      */
  41.     this.setSize(width,height);
  42.     this.setTitle(title);
  43.     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44.        
  45.     panel1= new JPanel(new GridLayout(1,3));   
  46.     btnStart = new JButton("Start");
  47.     btnStop = new JButton("Stop");
  48.     btnStop.disable();
  49.     btnStop.addActionListener(this);
  50.     btnExit = new JButton("Exit");
  51.     btnExit.addActionListener(this);
  52.     panel1.add(btnStart);
  53.     panel1.add(btnStop);
  54.     panel1.add(btnExit);
  55.    
  56.     panel2= new JPanel(new BorderLayout());  
  57.     panel3= new JPanel(new BorderLayout());
  58.     panel4= new JPanel(new BorderLayout());
  59.     panelMain= new JPanel (new GridLayout(4,1));
  60.    
  61.     label1= new JLabel("Adresar pro zpracovani: ");
  62.     label2= new JLabel("Pripona souboru:              ");
  63.     label3= new JLabel("Vystupni adresar:             ");
  64.     textfield1=new JTextField();
  65.     textfield1.setEditable(editable);
  66.     textfield2=new JTextField();
  67.     textfield3=new JTextField();
  68.     textfield3.setEditable(editable);
  69.    
  70.     btnFile1=new JButton("...");
  71.     btnFile2=new JButton("...");
  72.    
  73.     panel2.add(label1, BorderLayout.WEST);
  74.     panel2.add(textfield1, BorderLayout.CENTER);
  75.     panel2.add(btnFile1, BorderLayout.EAST);
  76.    
  77.     panel3.add(label2, BorderLayout.WEST);
  78.     panel3.add(textfield2,BorderLayout.CENTER)
  79.    
  80.     panel4.add(label3, BorderLayout.WEST);
  81.     panel4.add(textfield3, BorderLayout.CENTER);
  82.     panel4.add(btnFile2, BorderLayout.EAST);
  83.    
  84.     //vytvorime progressbar a nastavime ho
  85.     bar= new JProgressBar(0,100);
  86.     bar.setValue(0);
  87.     bar.setStringPainted(true);
  88.    
  89.     textarea= new JTextArea();
  90.     //taLog.setSize(600, 400);
  91.     JScrollPane scrollPanel= new JScrollPane(textarea);
  92.     //panelDia.add(scrollPanel);
  93.    
  94.     //vytvorime hlavni panel
  95.     panelMain.add(panel1);
  96.     panelMain.add(panel2);
  97.     panelMain.add(panel3);
  98.     panelMain.add(panel4);
  99.    
  100.     //rozmistime komponenty a panel
  101.     this.setLayout(new BorderLayout());
  102.     this.getContentPane().add(panelMain,BorderLayout.NORTH );
  103.     this.getContentPane().add(scrollPanel, BorderLayout.CENTER);
  104.     this.getContentPane().add(bar, BorderLayout.SOUTH);
  105.    
  106.    
  107.     btnStart.addActionListener(new ActionListener() {
  108.       public void actionPerformed(ActionEvent e) {
  109.         btn1Start(e);
  110.       }
  111.     });
  112.     btnStop.addActionListener(new ActionListener() {
  113.       public void actionPerformed(ActionEvent e) {
  114.         btn2Stop(e);
  115.       }
  116.     });
  117.     btnExit.addActionListener(new ActionListener() {
  118.       public void actionPerformed(ActionEvent e) {
  119.         btn3Exit(e);
  120.       }
  121.     });
  122.     btnFile1.addActionListener(new ActionListener() {
  123.       public void actionPerformed(ActionEvent e) {
  124.         btn4Vstup(e);
  125.       }
  126.     });
  127.     btnFile2.addActionListener(new ActionListener() {
  128.       public void actionPerformed(ActionEvent e) {
  129.         btn5Vystup(e);
  130.       }
  131.     });
  132.    
  133.     /*
  134.      *
  135.      * VYTVORENI VLAKNA
  136.      *
  137.      */
  138.     thread proc = new thread(dir1, dir2, extension, textarea, textfield1, textfield2, textfield3, bar);
  139.     Thread procThread = new Thread(proc);
  140.     procThread.stopped = false;
  141.    
  142.   }
  143.  
  144.   /*
  145.    *
  146.    * TLACITKO PRO ZADANI VSTUPNIHO ADRESARE
  147.    *
  148.    */
  149.   protected void btn4Vstup(ActionEvent e) {
  150.     // TODO Auto-generated method stub
  151.     JFileChooser fc1;
  152.     fc1 = new JFileChooser();
  153.     //nastavime, ze chceme zobrazovat pouze adresare
  154.     fc1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  155.     int returnVal = fc1.showOpenDialog(this);
  156.     if (returnVal == JFileChooser.APPROVE_OPTION) {
  157.       dir1 = fc1.getSelectedFile();
  158.       if (!dir1.getPath().equals("")) {
  159.         textfield1.setText(dir1.getPath());
  160.       }  
  161.     }
  162.   }
  163.  
  164.  
  165.   /*
  166.    *
  167.    * TLACITKO PRO ZADANI VYSTUPNIHO ADRESARE
  168.    *
  169.    */
  170.   protected void btn5Vystup(ActionEvent e) {
  171.     // TODO Auto-generated method stub
  172.     JFileChooser fc2;
  173.     fc2 = new JFileChooser();
  174.     //nastavime, ze chceme zobrazovat pouze adresare
  175.     fc2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  176.     int returnVal = fc2.showOpenDialog(this);
  177.       if (returnVal == JFileChooser.APPROVE_OPTION) {
  178.         dir2 = fc2.getSelectedFile();
  179.        
  180.         if (!dir2.getPath().equals("")) {
  181.           textfield3.setText(dir2.getPath());          
  182.         }
  183.     }
  184.    
  185.   }
  186.  
  187.   /*
  188.    *
  189.    * TLACITKO PRO SPUSTENI VLAKNA
  190.    *
  191.    */
  192.   protected void btn1Start(ActionEvent e) {
  193.    
  194.     //ziskame priponu a prevedeme ji na male znaky
  195.     extension = textfield2.getText().toLowerCase();
  196.    
  197.     //jsou zadane potrebne veci?
  198.     if (!extension.equals("") && !textfield1.getText().equals("") && !textfield3.getText().equals("")){
  199.            
  200.       btnStop.setEnabled(true);
  201.       btnStart.enable(false)
  202.      
  203.       //spustime vlakno
  204.       procThread.stopped = false;
  205.       procThread.start();
  206.      
  207.       btnStop.enable(false);
  208.       btnStart.enable(true);     
  209.     }
  210.     else {
  211.       textarea.setText(textarea.getText()+"Nezadali jste příponu nebo jste nevybrali adresář(e)!\n");
  212.     }
  213.   }
  214.  
  215.   /*
  216.    *
  217.    * TLACITKO PRO ZASTAVENI VLAKNA
  218.    *
  219.    */
  220.   protected void btn2Stop(ActionEvent e) {
  221.     // TODO Auto-generated method stub
  222.     textarea.setText(textarea.getText()+"Zmacknuto tlacitko stop! \n");
  223.  
  224.     procThread.stopped = true;
  225.  
  226.     if (!btnStart.isEnabled()){
  227.       procThread.stop();
  228.       procThread.destroy();
  229.       
  230.       btnStart.setEnabled(true);
  231.       btnStop.disable();
  232.     }
  233.      
  234.   }
  235.  
  236.   /*
  237.    *
  238.    * TLACITKO PRO UKONCENI APLIKACE
  239.    *
  240.    */
  241.   protected void btn3Exit(ActionEvent e) {
  242.     // TODO Auto-generated method stub
  243.     dispose();
  244.         System.exit(0);
  245.   }
  246.  
  247.   @Override
  248.   public void actionPerformed(ActionEvent e) {
  249.     // TODO Auto-generated method stub   
  250.   }    
  251. }
a poslednim souborem je "thread.java":

Java - thread.java

  1. package ksi.tul.cz.jan.kahoun;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8.  
  9. import javax.swing.JProgressBar;
  10. import javax.swing.JTextArea;
  11. import javax.swing.JTextField;
  12.  
  13. public class thread implements Runnable {
  14.  
  15.   /**
  16.    *
  17.    */
  18.   private static final long serialVersionUID = 1L;
  19.   private String extension;
  20.   private File dir1, dir2;
  21.   private int af_count, l_count, f_count;
  22.   private JTextArea textarea;
  23.   private JProgressBar bar;
  24.   private JTextField textfield1,textfield2,textfield3;
  25.   public boolean stopped;
  26.  
  27.   public thread(File dir1, File dir2, String extension, JTextArea textarea, JTextField textfield1, JTextField textfield2, JTextField textfield3, JProgressBar bar) {
  28.    
  29.     this.dir1 = dir1;
  30.     this.dir2 = dir2;
  31.     this.extension = extension;
  32.     this.textarea = textarea;
  33.     this.bar = bar;
  34.     this.textfield1 = textfield1;
  35.     this.textfield2 = textfield2;
  36.     this.textfield3 = textfield3;
  37.    
  38.   }
  39.  
  40.   @Override
  41.   public void run() {
  42.     while (!stopped) {
  43.     //vynulujeme statistiky, textareau a progress bar
  44.     textarea.setText("");
  45.     af_count = f_count = l_count = 0;
  46.     bar.setValue(0);
  47.    
  48.     textarea.setText(textarea.getText()+"pripona: "+extension+" \n");
  49.     textarea.setText(textarea.getText()+"adresar pro zpracovani: "+textfield1.getText()+" \n");
  50.     textarea.setText(textarea.getText()+"vystupni adresar: "+textfield3.getText()+" \n\n");
  51.    
  52.     //zacneme prochazet adresare aby jsme spocitali soubory pro progressbar
  53.     listContents(dir1, extension, true);
  54.    
  55.     textarea.setText(textarea.getText()+"Nalezené soubory: \n");
  56.    
  57.     //zacneme prochazet adresare a hledat soubory se stejnou priponou
  58.     listContents(dir1, extension, false);
  59.  
  60.     //ulozime statistiky
  61.     saveStats();
  62.  
  63.     textarea.setText(textarea.getText()+"\n\nPočet souborů: "+af_count+" \n");
  64.     textarea.setText(textarea.getText()+"počet řádků: "+l_count+" \n");
  65.     }
  66.   }
  67.  
  68.   //funkce na prochazeni adresaru
  69.   private void listContents(File dir, String ext, boolean progress) {
  70.  
  71.     String[] files;  // pro ulozeni obsahu slozky
  72.     files = dir.list();
  73.      
  74.     for (int i = 0; i < files.length; i++) {
  75.       File f;
  76.       f = new File(dir, files[i]);
  77.      
  78.       if ( f.isDirectory() ) {
  79.         //rekurzivni volani funkce listContents
  80.         listContents(f, ext, progress);
  81.       }
  82.       else { 
  83.         //ma soubor zadanou priponu?
  84.         if (ext.equals(getExtension(f))) {
  85.           //pocitame vsechny soubory?
  86.           if (!progress){          
  87.             f_count ++;
  88.             double barvalue = ((double) f_count /(af_count/100));
  89.             bar.setValue((int) barvalue);
  90.             lineCount(f.getPath());
  91.             textarea.setText(textarea.getText()+f.getPath()+"\n");
  92.           }else{         
  93.             af_count ++; //pocitame vsechny soubory pro progressbar
  94.           }
  95.         }                        
  96.       }    
  97.     }//konec cyklu
  98.   }
  99.  
  100.   //funkce na ziskani pripony souboru
  101.     public static String getExtension(File f)
  102.     {
  103.       String ext = null;
  104.       String s = f.getName(); //ziskame nazev souboru
  105.       int i = s.lastIndexOf('.'); //ziskame posledni vyskyt znaku .
  106.  
  107.       if (i > 0 && i < s.length() - 1) {
  108.         ext = s.substring(i+1).toLowerCase(); //vytahneme priponu (na pozici i je . proto +1)
  109.       }
  110.       if(ext == null){
  111.         return ""; //nektere pripony maji na konci tecku, proto vracime prazdny retezec!
  112.       }
  113.       return ext;
  114.     }
  115.    
  116.     //funkce na zjisteni poctu radku
  117.     public void lineCount(String file) {
  118.  
  119.     FileReader fr = null;
  120.     BufferedReader br = null;
  121.         try {
  122.           //otevreni souboru
  123.           fr = new FileReader(file);
  124.             br = new BufferedReader(fr);
  125.           
  126.             //spocitame radky
  127.           while((br.readLine()) != null) {
  128.           l_count++;
  129.           }
  130.         
  131.         } catch (IOException e) {
  132.           e.printStackTrace();
  133.         } finally {
  134.            //jestli vse probehlo ok, zavreme soubor
  135.            if (fr != null) {
  136.              try {
  137.                  fr.close();
  138.              } catch (IOException ioe) {
  139.              }
  140.            }
  141.         }
  142.      }
  143.    
  144.     //ulozeni statistik
  145.     public void saveStats() {
  146.      
  147.       FileWriter fw = null;
  148.         try {
  149.           //otevreni souboru
  150.           fw = new FileWriter(dir2.getPath()+"\\statistika.txt");
  151.     
  152.             fw.write("Počet souborů s příponou \""+extension+"\" je: "+f_count+"\n");
  153.             fw.write("Celkový počet řádek v souborech s příponou: \""+extension+"\" je: "+l_count+"\n");
  154.  
  155.         } catch (IOException e1) {
  156.           e1.printStackTrace();
  157.         } finally {
  158.            //jestli vse probehlo ok, zavreme soubor
  159.            if (fw != null) {
  160.              try {
  161.                  fw.close();
  162.              } catch (IOException ioe) {
  163.              }
  164.            }
  165.         }
  166.      
  167.     }
  168. }

18:49 — Zobrazeno: 1177x — Stálý odkaz



Komentáře

  1. xzzx napsal dne 03/08/11 v 18:22 · #

    alert(„Ahoj“ ;

  2. oolpiee napsal dne 01/20/12 v 14:23 · #

    vM8H1e bwhuqncsxnfh, [url=http://rvev­zycasnmz.com/]rvev­zycasnmz[/url], [link=http://a­vycfqbiwqfi.com/]a­vycfqbiwqfi[/lin­k], http://nzupoessexmc.com/

Přidání komentáře
Formát Texy

Váš příspěvek by měl být psán srozumitelně a k danému tématu! Nepoužívejte hrubé nadávky, vulgarismy, atd. a před samotným odesláním příspěvku si ho prosím ještě zkontrolujte!

Tučně zvýrazněné položky je nutno vyplnit!

Další komentáře:

XHTML 1.0 Strict - CSS