Sobota prosinec 22, 2007
Semestrálka z Javy - dokončeno!
· rubrika Clanky · kategorie Programování
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
- package ksi.tul.cz.jan.kahoun;
- public class main {
- public static void main(String[] args) {
- //vytvorime nove okno a nastavime mu parametry
- window okno1= new window("Semestralni prace", 800, 500);
- okno1.setVisible(true);
- }
- }
Java - window.java
- package ksi.tul.cz.jan.kahoun;
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JProgressBar;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class window extends JFrame implements ActionListener{
- private JButton btnStart, btnStop, btnExit, btnFile1, btnFile2;
- private JLabel label1,label2,label3;
- private JTextField textfield1,textfield2,textfield3;
- private JPanel panel1,panel2, panel3, panel4, panelMain;
- private JTextArea textarea;
- private JProgressBar bar;
- private File dir1, dir2;
- private String extension;
- private boolean editable = false;
- private thread proc;
- private Thread procThread;
- public window(String title, int width, int height){
- /*
- *
- * NASTAVENI GRAFICKEHO ROZHRANI
- *
- */
- this.setSize(width,height);
- this.setTitle(title);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- panel1= new JPanel(new GridLayout(1,3));
- btnStart = new JButton("Start");
- btnStop = new JButton("Stop");
- btnStop.disable();
- btnStop.addActionListener(this);
- btnExit = new JButton("Exit");
- btnExit.addActionListener(this);
- panel1.add(btnStart);
- panel1.add(btnStop);
- panel1.add(btnExit);
- panel2= new JPanel(new BorderLayout());
- panel3= new JPanel(new BorderLayout());
- panel4= new JPanel(new BorderLayout());
- panelMain= new JPanel (new GridLayout(4,1));
- label1= new JLabel("Adresar pro zpracovani: ");
- label2= new JLabel("Pripona souboru: ");
- label3= new JLabel("Vystupni adresar: ");
- textfield1=new JTextField();
- textfield1.setEditable(editable);
- textfield2=new JTextField();
- textfield3=new JTextField();
- textfield3.setEditable(editable);
- btnFile1=new JButton("...");
- btnFile2=new JButton("...");
- panel2.add(label1, BorderLayout.WEST);
- panel2.add(textfield1, BorderLayout.CENTER);
- panel2.add(btnFile1, BorderLayout.EAST);
- panel3.add(label2, BorderLayout.WEST);
- panel3.add(textfield2,BorderLayout.CENTER);
- panel4.add(label3, BorderLayout.WEST);
- panel4.add(textfield3, BorderLayout.CENTER);
- panel4.add(btnFile2, BorderLayout.EAST);
- //vytvorime progressbar a nastavime ho
- bar= new JProgressBar(0,100);
- bar.setValue(0);
- bar.setStringPainted(true);
- textarea= new JTextArea();
- //taLog.setSize(600, 400);
- JScrollPane scrollPanel= new JScrollPane(textarea);
- //panelDia.add(scrollPanel);
- //vytvorime hlavni panel
- panelMain.add(panel1);
- panelMain.add(panel2);
- panelMain.add(panel3);
- panelMain.add(panel4);
- //rozmistime komponenty a panel
- this.setLayout(new BorderLayout());
- this.getContentPane().add(panelMain,BorderLayout.NORTH );
- this.getContentPane().add(scrollPanel, BorderLayout.CENTER);
- this.getContentPane().add(bar, BorderLayout.SOUTH);
- btnStart.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btn1Start(e);
- }
- });
- btnStop.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btn2Stop(e);
- }
- });
- btnExit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btn3Exit(e);
- }
- });
- btnFile1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btn4Vstup(e);
- }
- });
- btnFile2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btn5Vystup(e);
- }
- });
- /*
- *
- * VYTVORENI VLAKNA
- *
- */
- thread proc = new thread(dir1, dir2, extension, textarea, textfield1, textfield2, textfield3, bar);
- Thread procThread = new Thread(proc);
- procThread.stopped = false;
- }
- /*
- *
- * TLACITKO PRO ZADANI VSTUPNIHO ADRESARE
- *
- */
- protected void btn4Vstup(ActionEvent e) {
- // TODO Auto-generated method stub
- JFileChooser fc1;
- fc1 = new JFileChooser();
- //nastavime, ze chceme zobrazovat pouze adresare
- fc1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int returnVal = fc1.showOpenDialog(this);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- dir1 = fc1.getSelectedFile();
- if (!dir1.getPath().equals("")) {
- textfield1.setText(dir1.getPath());
- }
- }
- }
- /*
- *
- * TLACITKO PRO ZADANI VYSTUPNIHO ADRESARE
- *
- */
- protected void btn5Vystup(ActionEvent e) {
- // TODO Auto-generated method stub
- JFileChooser fc2;
- fc2 = new JFileChooser();
- //nastavime, ze chceme zobrazovat pouze adresare
- fc2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int returnVal = fc2.showOpenDialog(this);
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- dir2 = fc2.getSelectedFile();
- if (!dir2.getPath().equals("")) {
- textfield3.setText(dir2.getPath());
- }
- }
- }
- /*
- *
- * TLACITKO PRO SPUSTENI VLAKNA
- *
- */
- protected void btn1Start(ActionEvent e) {
- //ziskame priponu a prevedeme ji na male znaky
- extension = textfield2.getText().toLowerCase();
- //jsou zadane potrebne veci?
- if (!extension.equals("") && !textfield1.getText().equals("") && !textfield3.getText().equals("")){
- btnStop.setEnabled(true);
- btnStart.enable(false);
- //spustime vlakno
- procThread.stopped = false;
- procThread.start();
- btnStop.enable(false);
- btnStart.enable(true);
- }
- else {
- textarea.setText(textarea.getText()+"Nezadali jste příponu nebo jste nevybrali adresář(e)!\n");
- }
- }
- /*
- *
- * TLACITKO PRO ZASTAVENI VLAKNA
- *
- */
- protected void btn2Stop(ActionEvent e) {
- // TODO Auto-generated method stub
- textarea.setText(textarea.getText()+"Zmacknuto tlacitko stop! \n");
- procThread.stopped = true;
- if (!btnStart.isEnabled()){
- procThread.stop();
- procThread.destroy();
- btnStart.setEnabled(true);
- btnStop.disable();
- }
- }
- /*
- *
- * TLACITKO PRO UKONCENI APLIKACE
- *
- */
- protected void btn3Exit(ActionEvent e) {
- // TODO Auto-generated method stub
- dispose();
- System.exit(0);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- }
- }
Java - thread.java
- package ksi.tul.cz.jan.kahoun;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import javax.swing.JProgressBar;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class thread implements Runnable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private String extension;
- private File dir1, dir2;
- private int af_count, l_count, f_count;
- private JTextArea textarea;
- private JProgressBar bar;
- private JTextField textfield1,textfield2,textfield3;
- public boolean stopped;
- public thread(File dir1, File dir2, String extension, JTextArea textarea, JTextField textfield1, JTextField textfield2, JTextField textfield3, JProgressBar bar) {
- this.dir1 = dir1;
- this.dir2 = dir2;
- this.extension = extension;
- this.textarea = textarea;
- this.bar = bar;
- this.textfield1 = textfield1;
- this.textfield2 = textfield2;
- this.textfield3 = textfield3;
- }
- @Override
- public void run() {
- while (!stopped) {
- //vynulujeme statistiky, textareau a progress bar
- textarea.setText("");
- af_count = f_count = l_count = 0;
- bar.setValue(0);
- textarea.setText(textarea.getText()+"pripona: "+extension+" \n");
- textarea.setText(textarea.getText()+"adresar pro zpracovani: "+textfield1.getText()+" \n");
- textarea.setText(textarea.getText()+"vystupni adresar: "+textfield3.getText()+" \n\n");
- //zacneme prochazet adresare aby jsme spocitali soubory pro progressbar
- listContents(dir1, extension, true);
- textarea.setText(textarea.getText()+"Nalezené soubory: \n");
- //zacneme prochazet adresare a hledat soubory se stejnou priponou
- listContents(dir1, extension, false);
- //ulozime statistiky
- saveStats();
- textarea.setText(textarea.getText()+"\n\nPočet souborů: "+af_count+" \n");
- textarea.setText(textarea.getText()+"počet řádků: "+l_count+" \n");
- }
- }
- //funkce na prochazeni adresaru
- private void listContents(File dir, String ext, boolean progress) {
- String[] files; // pro ulozeni obsahu slozky
- files = dir.list();
- for (int i = 0; i < files.length; i++) {
- File f;
- f = new File(dir, files[i]);
- if ( f.isDirectory() ) {
- //rekurzivni volani funkce listContents
- listContents(f, ext, progress);
- }
- else {
- //ma soubor zadanou priponu?
- if (ext.equals(getExtension(f))) {
- //pocitame vsechny soubory?
- if (!progress){
- f_count ++;
- double barvalue = ((double) f_count /(af_count/100));
- bar.setValue((int) barvalue);
- lineCount(f.getPath());
- textarea.setText(textarea.getText()+f.getPath()+"\n");
- }else{
- af_count ++; //pocitame vsechny soubory pro progressbar
- }
- }
- }
- }//konec cyklu
- }
- //funkce na ziskani pripony souboru
- public static String getExtension(File f)
- {
- String ext = null;
- String s = f.getName(); //ziskame nazev souboru
- int i = s.lastIndexOf('.'); //ziskame posledni vyskyt znaku .
- if (i > 0 && i < s.length() - 1) {
- ext = s.substring(i+1).toLowerCase(); //vytahneme priponu (na pozici i je . proto +1)
- }
- if(ext == null){
- return ""; //nektere pripony maji na konci tecku, proto vracime prazdny retezec!
- }
- return ext;
- }
- //funkce na zjisteni poctu radku
- public void lineCount(String file) {
- FileReader fr = null;
- BufferedReader br = null;
- try {
- //otevreni souboru
- fr = new FileReader(file);
- br = new BufferedReader(fr);
- //spocitame radky
- while((br.readLine()) != null) {
- l_count++;
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- //jestli vse probehlo ok, zavreme soubor
- if (fr != null) {
- try {
- fr.close();
- } catch (IOException ioe) {
- }
- }
- }
- }
- //ulozeni statistik
- public void saveStats() {
- FileWriter fw = null;
- try {
- //otevreni souboru
- fw = new FileWriter(dir2.getPath()+"\\statistika.txt");
- fw.write("Počet souborů s příponou \""+extension+"\" je: "+f_count+"\n");
- fw.write("Celkový počet řádek v souborech s příponou: \""+extension+"\" je: "+l_count+"\n");
- } catch (IOException e1) {
- e1.printStackTrace();
- } finally {
- //jestli vse probehlo ok, zavreme soubor
- if (fw != null) {
- try {
- fw.close();
- } catch (IOException ioe) {
- }
- }
- }
- }
- }
18:49
—
Zobrazeno: 1177x
—
Stálý odkaz
Komentáře
xzzx napsal dne 03/08/11 v 18:22 · #
oolpiee napsal dne 01/20/12 v 14:23 · #