📄 frmmain.java
字号:
import java.applet.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;public class FrmMain extends Applet implements Runnable, ActionListener{ private TextArea msg; private JButton b1; private JButton b2; private JButton b3; private JButton b4; private JPanel p; private FileInputStream fin=null; private FileOutputStream fout=null; private String pathname=null; private String filename=null; private String pathname_to=null; private byte buf[]; Thread th; public void init() { msg=new TextArea(50,20); b1=new JButton("选择"); b2=new JButton("加密"); b3=new JButton("保存"); b4=new JButton("解密"); b1.setFont(new Font("DialogInput", 0, 12)); b2.setFont(new Font("DialogInput", 0, 12)); b3.setFont(new Font("DialogInput", 0, 12)); b4.setFont(new Font("DialogInput", 0, 12)); p=new JPanel(); this.setLayout(new BorderLayout()); this.add("Center",msg); this.add("South",p); p.add(b1); p.add(b3); p.add(b2); p.add(b4); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); } public void run() { try { fin=new FileInputStream(pathname); fout=new FileOutputStream(pathname_to); buf=new byte[1024]; msg.append("正在加密文件"+filename+"\n"); int i=fin.read(buf); while(i!=-1) { for(int j=0;j<i;j++) { if(buf[j]!=127) buf[j]++; else buf[j]=-128; } fout.write(buf,0,i); i=fin.read(buf); } msg.append("文件加密成功!\n"); } catch(Exception e) { msg.append(e.getMessage()+"\n"); } } public static void main(String[] args) { FrmMain app=new FrmMain(); Frame frm=new Frame("文件加密工具"); app.init(); app.start(); frm.add(app); frm.setSize(500,300); frm.setResizable(false); Dimension d=Toolkit.getDefaultToolkit().getScreenSize(); Dimension f=frm.getSize(); frm.setLocation(d.width/2-f.width/2,d.height/2-f.height/2); frm.show(); frm.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("选择")) { FileDialog fdlg=new FileDialog(new Frame(),"选择要加密的文件",FileDialog.LOAD); fdlg.setVisible(true); msg.append("要加密的文件是: "+fdlg.getDirectory()+fdlg.getFile()+"\n"); filename=fdlg.getFile(); pathname=fdlg.getDirectory()+fdlg.getFile(); return; } if(e.getActionCommand().equals("保存")) { FileDialog fdlg=new FileDialog(new Frame(),"选择文件的存放位置",FileDialog.SAVE); fdlg.setVisible(true); msg.append("加密并保存到: "+fdlg.getDirectory()+fdlg.getFile()+"\n"); pathname_to=fdlg.getDirectory()+fdlg.getFile(); return; } if(e.getActionCommand().equals("加密")) { if(pathname.equals("")||pathname.equals("nullnull")) { JOptionPane.showMessageDialog(null,"没有选择要加密的文件","错误",JOptionPane.ERROR_MESSAGE); return; } th=new Thread(this); th.start(); return; } if(e.getActionCommand().equals("解密")) { //if(pathname.equals("")||pathname.equals("nullnull")) //{ JOptionPane.showMessageDialog(null,"没有选择要加密的文件","错误",JOptionPane.ERROR_MESSAGE); return; //} // th=new Thread(this); //th.start(); //return; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -