国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java文件選擇對話框JFileChooser使用詳解

Java文件選擇對話框JFileChooser使用詳解

2019-12-25 13:29hebedich JAVA教程

這篇文章主要介紹了Java文件選擇對話框JFileChooser使用詳解的相關資料,需要的朋友可以參考下

文件加密器,操作過程肯定涉及到文件選擇器的使用,所以這里以文件加密器為例。下例為我自己寫的一個文件加密器,沒什么特別的加密算法,只為演示文件選擇器JFileChooser的使用。

加密器界面如圖:

Java文件選擇對話框JFileChooser使用詳解

Java文件選擇對話框JFileChooser使用詳解

項目目錄結構如圖:

Java文件選擇對話框JFileChooser使用詳解

下面貼出各個文件的源代碼:

MainForm.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.lidi;
import javax.swing.*;
import java.awt.*;
public class MainForm extends JFrame {
  /**
   * 構造界面
   *
   * @author 1109030125
   */
  private static final long serialVersionUID = 1L;
  /* 主窗體里面的若干元素 */
  private JFrame mainForm = new JFrame("TXT文件加密"); // 主窗體,標題為“TXT文件加密”
  private JLabel label1 = new JLabel("請選擇待加密或解密的文件:");
  private JLabel label2 = new JLabel("請選擇加密或解密后的文件存放位置:");
  public static JTextField sourcefile = new JTextField(); // 選擇待加密或解密文件路徑的文本域
  public static JTextField targetfile = new JTextField(); // 選擇加密或解密后文件路徑的文本域
  public static JButton buttonBrowseSource = new JButton("瀏覽"); // 瀏覽按鈕
  public static JButton buttonBrowseTarget = new JButton("瀏覽"); // 瀏覽按鈕
  public static JButton buttonEncrypt = new JButton("加密"); // 加密按鈕
  public static JButton buttonDecrypt = new JButton("解密"); // 解密按鈕
  public MainForm() {
    Container container = mainForm.getContentPane();
    /* 設置主窗體屬性 */
    mainForm.setSize(400, 270);// 設置主窗體大小
    mainForm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);// 設置主窗體關閉按鈕樣式
    mainForm.setLocationRelativeTo(null);// 設置居于屏幕中央
    mainForm.setResizable(false);// 設置窗口不可縮放
    mainForm.setLayout(null);
    mainForm.setVisible(true);// 顯示窗口
    /* 設置各元素位置布局 */
    label1.setBounds(30, 10, 300, 30);
    sourcefile.setBounds(50, 50, 200, 30);
    buttonBrowseSource.setBounds(270, 50, 60, 30);
    label2.setBounds(30, 90, 300, 30);
    targetfile.setBounds(50, 130, 200, 30);
    buttonBrowseTarget.setBounds(270, 130, 60, 30);
    buttonEncrypt.setBounds(100, 180, 60, 30);
    buttonDecrypt.setBounds(200, 180, 60, 30);
    /* 為各元素綁定事件監聽器 */
    buttonBrowseSource.addActionListener(new BrowseAction()); // 為源文件瀏覽按鈕綁定監聽器,點擊該按鈕調用文件選擇窗口
    buttonBrowseTarget.addActionListener(new BrowseAction()); // 為目標位置瀏覽按鈕綁定監聽器,點擊該按鈕調用文件選擇窗口
    buttonEncrypt.addActionListener(new EncryptAction()); // 為加密按鈕綁定監聽器,單擊加密按鈕會對源文件進行加密并輸出到目標位置
    buttonDecrypt.addActionListener(new DecryptAction()); // 為解密按鈕綁定監聽器,單擊解密按鈕會對源文件進行解密并輸出到目標位置
    sourcefile.getDocument().addDocumentListener(new TextFieldAction());// 為源文件文本域綁定事件,如果文件是.txt類型,則禁用解密按鈕;如果是.kcd文件,則禁用加密按鈕。
    sourcefile.setEditable(false);// 設置源文件文本域不可手動修改
    targetfile.setEditable(false);// 設置目標位置文本域不可手動修改
    container.add(label1);
    container.add(label2);
    container.add(sourcefile);
    container.add(targetfile);
    container.add(buttonBrowseSource);
    container.add(buttonBrowseTarget);
    container.add(buttonEncrypt);
    container.add(buttonDecrypt);
  }
  public static void main(String args[]) {
    new MainForm();
  }
}

BrowseAction.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.lidi;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class BrowseAction implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(MainForm.buttonBrowseSource)) {
      JFileChooser fcDlg = new JFileChooser();
      fcDlg.setDialogTitle("請選擇待加密或解密的文件...");
      FileNameExtensionFilter filter = new FileNameExtensionFilter(
          "文本文件(*.txt;*.kcd)", "txt", "kcd");
      fcDlg.setFileFilter(filter);
      int returnVal = fcDlg.showOpenDialog(null);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        String filepath = fcDlg.getSelectedFile().getPath();
        MainForm.sourcefile.setText(filepath);
      }
    } else if (e.getSource().equals(MainForm.buttonBrowseTarget)) {
      JFileChooser fcDlg = new JFileChooser();
      fcDlg.setDialogTitle("請選擇加密或解密后的文件存放目錄");
      fcDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
      int returnVal = fcDlg.showOpenDialog(null);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        String filepath = fcDlg.getSelectedFile().getPath();
        MainForm.targetfile.setText(filepath);
      }
    }
  }
}

EncryptAction.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.lidi;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
public class EncryptAction implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (MainForm.sourcefile.getText().isEmpty()) {
      JOptionPane.showMessageDialog(null, "請選擇待加密文件!");
    }
    else if (MainForm.targetfile.getText().isEmpty()) {
      JOptionPane.showMessageDialog(null, "請選擇加密后文件存放目錄!");
    }
    else {
      String sourcepath = MainForm.sourcefile.getText();
      String targetpath = MainForm.targetfile.getText();
      File file = new File(sourcepath);
      String filename = file.getName();
      File dir = new File(targetpath);
      if (file.exists() && dir.isDirectory()) {
        File result = new File(getFinalFile(targetpath, filename));
        if (!result.exists()) {
          try {
            result.createNewFile();
          } catch (IOException e1) {
            JOptionPane.showMessageDialog(null,
                "目標文件創建失敗,請檢查目錄是否為只讀!");
          }
        }
        try {
          FileReader fr = new FileReader(file);
          FileWriter fw = new FileWriter(result);
          int ch = 0;
          while ((ch = fr.read()) != -1) {
            // System.out.print(Encrypt(ch));
            fw.write(Encrypt(ch));
          }
          fw.close();
          fr.close();
          JOptionPane.showMessageDialog(null, "加密成功!");
        } catch (Exception e1) {
          JOptionPane.showMessageDialog(null, "未知錯誤!");
        }
      }
      else if (!file.exists()) {
        JOptionPane.showMessageDialog(null, "待加密文件不存在!");
      } else {
        JOptionPane.showMessageDialog(null, "加密后文件存放目錄不存在!");
      }
    }
  }
  public char Encrypt(int ch) {
    int x = ch + 1;
    return (char) (x);
  }
  public String getFinalFile(String targetpath, String filename) {
    int length = filename.length();
    String finalFileName = filename.substring(0, length - 4);
    String finalFile = targetpath + "\\" + finalFileName + ".kcd";
    return finalFile;
  }
}

DecryptAction.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.lidi;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
public class DecryptAction implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (MainForm.sourcefile.getText().isEmpty()) {
      JOptionPane.showMessageDialog(null, "請選擇待解密文件!");
    }
    else if (MainForm.targetfile.getText().isEmpty()) {
      JOptionPane.showMessageDialog(null, "請選擇解密后文件存放目錄!");
    }
    else {
      String sourcepath = MainForm.sourcefile.getText();
      String targetpath = MainForm.targetfile.getText();
      File file = new File(sourcepath);
      String filename = file.getName();
      File dir = new File(targetpath);
      if (file.exists() && dir.isDirectory()) {
        File result = new File(getFinalFile(targetpath, filename));
        if (!result.exists()) {
          try {
            result.createNewFile();
          } catch (IOException e1) {
            JOptionPane.showMessageDialog(null,
                "目標文件創建失敗,請檢查目錄是否為只讀!");
          }
        }
        try {
          FileReader fr = new FileReader(file);
          FileWriter fw = new FileWriter(result);
          int ch = 0;
          while ((ch = fr.read()) != -1) {
            // System.out.print(Encrypt(ch));
            fw.write(Decrypt(ch));
          }
          fw.close();
          fr.close();
          JOptionPane.showMessageDialog(null, "解密成功!");
        } catch (Exception e1) {
          JOptionPane.showMessageDialog(null, "未知錯誤!");
        }
      }
      else if (!file.exists()) {
        JOptionPane.showMessageDialog(null, "待解密文件不存在!");
      } else {
        JOptionPane.showMessageDialog(null, "解密后文件存放目錄不存在!");
      }
    }
  }
  public char Decrypt(int ch) {
    // double x = 0 - Math.pow(ch, 2);
    int x = ch - 1;
    return (char) (x);
  }
  public String getFinalFile(String targetpath, String filename) {
    int length = filename.length();
    String finalFileName = filename.substring(0, length - 4);
    String finalFile = targetpath + "\\" + finalFileName + ".txt";
    return finalFile;
  }
}

TextFieldAction.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.lidi;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class TextFieldAction implements DocumentListener {
  @Override
  public void insertUpdate(DocumentEvent e) {
    // TODO Auto-generated method stub
    ButtonAjust();
  }
  @Override
  public void removeUpdate(DocumentEvent e) {
    // TODO Auto-generated method stub
    ButtonAjust();
  }
  @Override
  public void changedUpdate(DocumentEvent e) {
    // TODO Auto-generated method stub
    ButtonAjust();
  }
  public void ButtonAjust() {
    String file = MainForm.sourcefile.getText();
    if (file.endsWith("txt")) {
      MainForm.buttonDecrypt.setEnabled(false);
      MainForm.buttonEncrypt.setEnabled(true);
    }
    if (file.endsWith("kcd")) {
      MainForm.buttonEncrypt.setEnabled(false);
      MainForm.buttonDecrypt.setEnabled(true);
    }
  }
}

以上所述就是本文的全部內容了,希望大家能夠喜歡。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 香蕉影院在线观看 | 午夜爱爱毛片xxxx视频免费看 | 日本久久久久久 | 日韩三级黄色片 | 成人久| 欧美日韩精品一区 | 久久久久99 | 日韩国产一区 | 久久综合久色欧美综合狠狠 | 精品国产仑片一区二区三区 | 日韩在线中文字幕 | 亚洲青草 | 免费一级黄色录像 | 伦乱视频 | 黄色片免费看 | 在线视频亚洲 | 8888色大全| 欧美 日韩 国产 一区 | 久久首页| 黄色小视频在线观看 | 黄色影院 | 91在线播放视频 | 亚洲欧洲精品成人久久奇米网 | 精品一区二区久久久久黄大片 | 亚洲色图88 | 91在线视频免费 | 日韩一区二区不卡 | 九九热精品视频在线观看 | 色视频在线免费观看 | 黄色毛片看看 | 色婷婷狠狠 | 亚洲四区 | 久久777| 91免费看大片| 一级欧美日韩 | 久久久久在线 | 中文字幕精品一区 | 国产麻豆91视频 | 亚洲高清一区二区三区 | 欧美一区二区三区久久 | 亚洲专区欧美 |