java 对话框 选择_Java文件选择对话框JFileChooser使用详解

MainForm extends JFrame

{

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

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

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

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

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);

}

}

}

文章来源:http://www.chinatjetc.com/article.php?CID=26&ID=673

了解更多内容可登录中软国际官方网站:www.chinatjetc.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值