c语言sscanf函数的用法是什么
296
2023-01-18
Java可视化之实现文本的加密和解密
一、题目
编写一个java程序,实现一个文本信息的加密。
二、要求
可视化界面,友好的输入和输出,文件的存取。
三、分析
所谓数据加密(Data Encryption)技术是指将一个信息(或称明文,plain text)经过加密钥匙(Encryption key)及加密函数转换,变成无意义的密文(cipher text),而接收方则将此密文经过解密函数、解密钥匙(Decryption key)还原成明文。
四、界面规划
登录验证界面
建立基本框架,基于Swing中Frame
各组件的属性
组件编号/类型
名称/属性
1 /JLabel
lblNewLabel/用户名
2/JPasswordField
passwordField/
3 /JButton
btnNewButton/确定
4 /JTextField
textField/
5 /JPasswordField
passwordField/
6 /JButton
btnNewButton_1/退出
(左边一列从上到下依次为1-2,右边一列从上到下依次为3-5)
五、功能实现
5.1 确定功能实现
当use_name和password都正确时,跳转到下一界面,否则按下正确按钮时,将输入的字符串重置为空。
String use_name=textField.getText();
String password;
password=String.valueOf(passwordField.getPassword());
if(use_name.equals("DJC期待")&&password.equals("1234")) {
SignUp.this.setVisible(false);
Jia_mi d=new Jia_mi();//加密和解密类
d.setVisible(true);
}
else {
String nl="";
textField.setText(nl);
passwordField.setText(nl);
}
5.2 退出功能实现
正常退出,程序正常执行结束退出
System.exit(0);
建立基本框架,基于Swing中Frame
各组件的属性
组件编号/类型
名称/属性
1 /JButton
btnNewButton_2/<<翻译
2/JTextArea
textArea/
3 /JButton
btnNewButton_1/打开密文
4/JTextArea
textArea_1/
5 /JButton
btnNewButton/保存文本
(左边一列从上到下依次为1-2,右边一列从上到下依次为3-5)
多行文本输入框的功能与单行文本输入框的功能相同,只是它能显示更多的文字。因为单行文本输入框只能输入一行的文字,所以需要输入和显示较多的文字时,就要用到多行文本输入框。多行文本输入框是由 JTextArea 类实现的。
5.3 加密文本的产生
在caretUpdate()函数中,先取到用户输入的字符然后依次将这些字符转为Unicode编码加999重新以字符的编码赋值输出,显示在右边Jtextarea中。
String str1=textArea.getText();
String str2="";
char c;
for(int i=0;i c=str1.charAt(i); c=(char)(c+999); str2+=c; } textArea_1.setText(str2); 5.4 密码文件的保存 JFileChooser jfchooser=new JFileChooser(); if(jfchooser.showSaveDialog(null)== JFileChooser.APPROVE_OPTION) { File f=jfchooser.getSelectedFile(); try { FileWriter fw=new FileWriter(f); String str=textArea_1.getText(); fw.write(str); fw.close(); } catch(IOException e1) { e1.printStackTrace(); } } 5.5 密码文件的解密 public void actionPerformed(ActionEvent e) { JFileChooser fchooser=new JFileChooser(); if(fchooser.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) { File f=fchooser.getSelectedFile(); try { FileReader fr=new FileReader(f); try { int n=fr.read(); String str=""; char c; while(n!=-1) { c=(char)n; str+=c; n=fr.read(); } textArea_1.setText(str); fr.close(); } catch(IOException e1) { e1.printStackTrace(); } } catch(FileNotFoundException e1) { e1.printStackTrace(); } } 5.6 <<翻译 加密过程的反过程。 String str2=textArea_1.getText(); String str1=""; for(int i=0;i char c=str2.charAt(i); c=(char)(c-999); str1+=c; } textArea.setText(str1); } 六、功能测试 登录 账号和密码不同时为对时,账号框和密码框重置。 密码和账号同时为对时,进入加密和解密界面。 文本与加密文本的转换。 保存的文件。 打开密文方法与以上相同。 将密文(Unicode编码)转换明文。 将程序导出为一个可执行的Java软件 桌面可执行软件。 步骤:File-Export-选择main函数所在类-选择导出的位置。 七、程序源码 7.1 SignUp.java package 文本信息的加密与解密; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Frame; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; importhttp:// java.awt.event.ActionEvent; import javax.swing.JPasswordField; public class SignUp extends JFrame { private JPanel contentPane; private JTextField textField; private JPasswordField passwordField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SignUp frame = new SignUp(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public SignUp() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D"); lblNewLabel.setBounds(34, 27, 69, 26); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801"); lblNewLabel_1.setBounds(34, 104, 69, 26); contentPane.add(lblNewLabel_1); textField = new JTextField(); textField.setBounds(153, 30, 164, 35); contentPane.add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("\u786E\u5B9A"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String use_name=textField.getText(); String password; password=String.valueOf(passwordField.getPassword()); if(use_name.equals("DJC期待")&&password.equals("1234")) { SignUp.this.setVisible(false); Jia_mi d=new Jia_mi(); d.setVisible(true); } else { String nl=""; textField.setText(nl); passwordField.setText(nl); } } }); btnNewButton.setBounds(53, 194, 93, 23); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u9000\u51FA"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); btnNewButton_1.setBounds(234, 194, 93, 23); contentPane.add(btnNewButton_1); passwordField = new JPasswordField(); passwordField.setBounds(153, 104, 164, 24); contentPane.add(passwordField); } } 7.2 Jia_mi.java package 文本信息的加密与解密; import java.awt.EventQueue; import java.awt.Frame; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.awt.event.ActionEvent; import javax.swing.JTextArea; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.event.CaretListener; import javax.swing.event.CaretEvent; public class Jia_mi extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Jia_mi frame = new Jia_mi(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Jia_mi() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 630, 404); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JTextArea textArea_1 = new JTextArea(); textArea_1.setWrapStyleWord(true); textArea_1.setLineWrap(true); textArea_1.setBounds(356, 97, 187, 164); contentPane.add(textArea_1); JTextArea textArea = new JTextArea(); textArea.setWrapStyleWord(true); textArea.setLineWrap(true); textArea.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent arg0) { String str1=textArea.getText(); String str2=""; char c; for(int i=0;i c=str1.charAt(i); c=(char)(c+999); str2+=c; } textArea_1.setText(str2); } }); textArea.setBounds(35, 97, 187, 164); contentPane.add(textArea); JButton btnNewButton = new JButton("\u4FDD\u5B58\u6587\u672C"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser jfchooser=new JFileChooser(); if(jfchooser.showSaveDialog(null)== JFileChooser.APPROVE_OPTION) { File f=jfchooser.getSelectedFile(); try { FileWriter fw=new FileWriter(f); String str=textArea_1.getText(); fw.write(str); fw.close(); } catch(IOException e1) { e1.printStackTrace(); } } } }); btnNewButton.setBounds(360, 303, 93, 23); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u6253\u5F00\u5BC6\u6587"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fchooser=new JFileChooser(); if(fchooser.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) { File f=fchooser.getSelectedFile(); try { FileReader fr=new FileReader(f); try { int n=fr.read(); String str=""; char c; while(n!=-1) { c=(char)n; str+=c; n=fr.read(); } textArea_1.setText(str); fr.close(); } catch(IOException e1) { e1.printStackTrace(); } } catch(FileNotFoundException e1) { e1.printStackTrace(); } } } }); btnNewButton_1.setBounds(397, 31, 93, 23); contentPane.add(btnNewButton_1); JButton btnNewButton_2 = new JButton("<<\u7FFB\u8BD1"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str2=textArea_1.getText(); String str1=""; for(int i=0;i char c=str2.charAt(i); c=(char)(c-999); str1+=c; } textArea.setText(str1); } }); btnNewButton_2.setBounds(129, 31, 93, 23); contentPane.add(btnNewButton_2); } public Jia_mi(Frame f) { // TODO 自动生成的构造函数存根 } }
c=str1.charAt(i);
c=(char)(c+999);
str2+=c;
}
textArea_1.setText(str2);
5.4 密码文件的保存
JFileChooser jfchooser=new JFileChooser();
if(jfchooser.showSaveDialog(null)==
JFileChooser.APPROVE_OPTION) {
File f=jfchooser.getSelectedFile();
try {
FileWriter fw=new FileWriter(f);
String str=textArea_1.getText();
fw.write(str);
fw.close();
}
catch(IOException e1) {
e1.printStackTrace();
}
}
5.5 密码文件的解密
public void actionPerformed(ActionEvent e) {
JFileChooser fchooser=new JFileChooser();
if(fchooser.showOpenDialog(null)==
JFileChooser.APPROVE_OPTION) {
File f=fchooser.getSelectedFile();
try {
FileReader fr=new FileReader(f);
try {
int n=fr.read();
String str="";
char c;
while(n!=-1) {
c=(char)n;
str+=c;
n=fr.read();
}
textArea_1.setText(str);
fr.close();
}
catch(IOException e1) {
e1.printStackTrace();
}
}
catch(FileNotFoundException e1) {
e1.printStackTrace();
}
}
5.6 <<翻译
加密过程的反过程。
String str2=textArea_1.getText();
String str1="";
for(int i=0;i char c=str2.charAt(i); c=(char)(c-999); str1+=c; } textArea.setText(str1); } 六、功能测试 登录 账号和密码不同时为对时,账号框和密码框重置。 密码和账号同时为对时,进入加密和解密界面。 文本与加密文本的转换。 保存的文件。 打开密文方法与以上相同。 将密文(Unicode编码)转换明文。 将程序导出为一个可执行的Java软件 桌面可执行软件。 步骤:File-Export-选择main函数所在类-选择导出的位置。 七、程序源码 7.1 SignUp.java package 文本信息的加密与解密; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Frame; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; importhttp:// java.awt.event.ActionEvent; import javax.swing.JPasswordField; public class SignUp extends JFrame { private JPanel contentPane; private JTextField textField; private JPasswordField passwordField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SignUp frame = new SignUp(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public SignUp() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D"); lblNewLabel.setBounds(34, 27, 69, 26); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801"); lblNewLabel_1.setBounds(34, 104, 69, 26); contentPane.add(lblNewLabel_1); textField = new JTextField(); textField.setBounds(153, 30, 164, 35); contentPane.add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("\u786E\u5B9A"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String use_name=textField.getText(); String password; password=String.valueOf(passwordField.getPassword()); if(use_name.equals("DJC期待")&&password.equals("1234")) { SignUp.this.setVisible(false); Jia_mi d=new Jia_mi(); d.setVisible(true); } else { String nl=""; textField.setText(nl); passwordField.setText(nl); } } }); btnNewButton.setBounds(53, 194, 93, 23); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u9000\u51FA"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); btnNewButton_1.setBounds(234, 194, 93, 23); contentPane.add(btnNewButton_1); passwordField = new JPasswordField(); passwordField.setBounds(153, 104, 164, 24); contentPane.add(passwordField); } } 7.2 Jia_mi.java package 文本信息的加密与解密; import java.awt.EventQueue; import java.awt.Frame; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.awt.event.ActionEvent; import javax.swing.JTextArea; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.event.CaretListener; import javax.swing.event.CaretEvent; public class Jia_mi extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Jia_mi frame = new Jia_mi(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Jia_mi() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 630, 404); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JTextArea textArea_1 = new JTextArea(); textArea_1.setWrapStyleWord(true); textArea_1.setLineWrap(true); textArea_1.setBounds(356, 97, 187, 164); contentPane.add(textArea_1); JTextArea textArea = new JTextArea(); textArea.setWrapStyleWord(true); textArea.setLineWrap(true); textArea.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent arg0) { String str1=textArea.getText(); String str2=""; char c; for(int i=0;i c=str1.charAt(i); c=(char)(c+999); str2+=c; } textArea_1.setText(str2); } }); textArea.setBounds(35, 97, 187, 164); contentPane.add(textArea); JButton btnNewButton = new JButton("\u4FDD\u5B58\u6587\u672C"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser jfchooser=new JFileChooser(); if(jfchooser.showSaveDialog(null)== JFileChooser.APPROVE_OPTION) { File f=jfchooser.getSelectedFile(); try { FileWriter fw=new FileWriter(f); String str=textArea_1.getText(); fw.write(str); fw.close(); } catch(IOException e1) { e1.printStackTrace(); } } } }); btnNewButton.setBounds(360, 303, 93, 23); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u6253\u5F00\u5BC6\u6587"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fchooser=new JFileChooser(); if(fchooser.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) { File f=fchooser.getSelectedFile(); try { FileReader fr=new FileReader(f); try { int n=fr.read(); String str=""; char c; while(n!=-1) { c=(char)n; str+=c; n=fr.read(); } textArea_1.setText(str); fr.close(); } catch(IOException e1) { e1.printStackTrace(); } } catch(FileNotFoundException e1) { e1.printStackTrace(); } } } }); btnNewButton_1.setBounds(397, 31, 93, 23); contentPane.add(btnNewButton_1); JButton btnNewButton_2 = new JButton("<<\u7FFB\u8BD1"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str2=textArea_1.getText(); String str1=""; for(int i=0;i char c=str2.charAt(i); c=(char)(c-999); str1+=c; } textArea.setText(str1); } }); btnNewButton_2.setBounds(129, 31, 93, 23); contentPane.add(btnNewButton_2); } public Jia_mi(Frame f) { // TODO 自动生成的构造函数存根 } }
char c=str2.charAt(i);
c=(char)(c-999);
str1+=c;
}
textArea.setText(str1);
}
六、功能测试
登录
账号和密码不同时为对时,账号框和密码框重置。
密码和账号同时为对时,进入加密和解密界面。
文本与加密文本的转换。
保存的文件。
打开密文方法与以上相同。
将密文(Unicode编码)转换明文。
将程序导出为一个可执行的Java软件
桌面可执行软件。
步骤:File-Export-选择main函数所在类-选择导出的位置。
七、程序源码
7.1 SignUp.java
package 文本信息的加密与解密;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
importhttp:// java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class SignUp extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SignUp frame = new SignUp();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SignUp() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D");
lblNewLabel.setBounds(34, 27, 69, 26);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801");
lblNewLabel_1.setBounds(34, 104, 69, 26);
contentPane.add(lblNewLabel_1);
textField = new JTextField();
textField.setBounds(153, 30, 164, 35);
contentPane.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("\u786E\u5B9A");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String use_name=textField.getText();
String password;
password=String.valueOf(passwordField.getPassword());
if(use_name.equals("DJC期待")&&password.equals("1234")) {
SignUp.this.setVisible(false);
Jia_mi d=new Jia_mi();
d.setVisible(true);
}
else {
String nl="";
textField.setText(nl);
passwordField.setText(nl);
}
}
});
btnNewButton.setBounds(53, 194, 93, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u9000\u51FA");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnNewButton_1.setBounds(234, 194, 93, 23);
contentPane.add(btnNewButton_1);
passwordField = new JPasswordField();
passwordField.setBounds(153, 104, 164, 24);
contentPane.add(passwordField);
}
}
7.2 Jia_mi.java
package 文本信息的加密与解密;
import java.awt.EventQueue;
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;
public class Jia_mi extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Jia_mi frame = new Jia_mi();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Jia_mi() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 630, 404);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextArea textArea_1 = new JTextArea();
textArea_1.setWrapStyleWord(true);
textArea_1.setLineWrap(true);
textArea_1.setBounds(356, 97, 187, 164);
contentPane.add(textArea_1);
JTextArea textArea = new JTextArea();
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent arg0) {
String str1=textArea.getText();
String str2="";
char c;
for(int i=0;i c=str1.charAt(i); c=(char)(c+999); str2+=c; } textArea_1.setText(str2); } }); textArea.setBounds(35, 97, 187, 164); contentPane.add(textArea); JButton btnNewButton = new JButton("\u4FDD\u5B58\u6587\u672C"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser jfchooser=new JFileChooser(); if(jfchooser.showSaveDialog(null)== JFileChooser.APPROVE_OPTION) { File f=jfchooser.getSelectedFile(); try { FileWriter fw=new FileWriter(f); String str=textArea_1.getText(); fw.write(str); fw.close(); } catch(IOException e1) { e1.printStackTrace(); } } } }); btnNewButton.setBounds(360, 303, 93, 23); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u6253\u5F00\u5BC6\u6587"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fchooser=new JFileChooser(); if(fchooser.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) { File f=fchooser.getSelectedFile(); try { FileReader fr=new FileReader(f); try { int n=fr.read(); String str=""; char c; while(n!=-1) { c=(char)n; str+=c; n=fr.read(); } textArea_1.setText(str); fr.close(); } catch(IOException e1) { e1.printStackTrace(); } } catch(FileNotFoundException e1) { e1.printStackTrace(); } } } }); btnNewButton_1.setBounds(397, 31, 93, 23); contentPane.add(btnNewButton_1); JButton btnNewButton_2 = new JButton("<<\u7FFB\u8BD1"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str2=textArea_1.getText(); String str1=""; for(int i=0;i char c=str2.charAt(i); c=(char)(c-999); str1+=c; } textArea.setText(str1); } }); btnNewButton_2.setBounds(129, 31, 93, 23); contentPane.add(btnNewButton_2); } public Jia_mi(Frame f) { // TODO 自动生成的构造函数存根 } }
c=str1.charAt(i);
c=(char)(c+999);
str2+=c;
}
textArea_1.setText(str2);
}
});
textArea.setBounds(35, 97, 187, 164);
contentPane.add(textArea);
JButton btnNewButton = new JButton("\u4FDD\u5B58\u6587\u672C");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfchooser=new JFileChooser();
if(jfchooser.showSaveDialog(null)==
JFileChooser.APPROVE_OPTION) {
File f=jfchooser.getSelectedFile();
try {
FileWriter fw=new FileWriter(f);
String str=textArea_1.getText();
fw.write(str);
fw.close();
}
catch(IOException e1) {
e1.printStackTrace();
}
}
}
});
btnNewButton.setBounds(360, 303, 93, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u6253\u5F00\u5BC6\u6587");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fchooser=new JFileChooser();
if(fchooser.showOpenDialog(null)==
JFileChooser.APPROVE_OPTION) {
File f=fchooser.getSelectedFile();
try {
FileReader fr=new FileReader(f);
try {
int n=fr.read();
String str="";
char c;
while(n!=-1) {
c=(char)n;
str+=c;
n=fr.read();
}
textArea_1.setText(str);
fr.close();
}
catch(IOException e1) {
e1.printStackTrace();
}
}
catch(FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
});
btnNewButton_1.setBounds(397, 31, 93, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("<<\u7FFB\u8BD1");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str2=textArea_1.getText();
String str1="";
for(int i=0;i char c=str2.charAt(i); c=(char)(c-999); str1+=c; } textArea.setText(str1); } }); btnNewButton_2.setBounds(129, 31, 93, 23); contentPane.add(btnNewButton_2); } public Jia_mi(Frame f) { // TODO 自动生成的构造函数存根 } }
char c=str2.charAt(i);
c=(char)(c-999);
str1+=c;
}
textArea.setText(str1);
}
});
btnNewButton_2.setBounds(129, 31, 93, 23);
contentPane.add(btnNewButton_2);
}
public Jia_mi(Frame f) {
// TODO 自动生成的构造函数存根
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~