本帖最后由 田建 于 2012-7-3 22:38 编辑
- package cn.itcast.secret;
- import java.awt.Container;
- import java.awt.FileDialog;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class BuildSecret extends JFrame implements ActionListener {
- private JPanel imagePanel;
- private ImageIcon background;
- // FileDialog fig;
- JTextField jtf1 = new JTextField("传入要加密文件", 8);
- JTextField jtf2 = new JTextField("传入要解密文件", 8);
- JButton jbt11 = new JButton("导入");
- JButton jbt12 = new JButton("加密");
- JButton jbt21 = new JButton("导入");
- JButton jbt22 = new JButton("解密");
- public BuildSecret() {
- // fig=new FileDialog(this,"打开",FileDialog.LOAD);有了JFileChooser,不需要了!
- this.setTitle("加密解密器");
- this.setLayout(new FlowLayout());
- myEvent();
- this.add(jtf1);
- this.add(jbt11);
- this.add(jbt12);
- this.add(jtf2);
- this.add(jbt21);
- this.add(jbt22);
- /*
- * background=new ImageIcon("f:\123.jpg");//背景图片 JLabel label=new
- * JLabel(background);//把背景图片显示在一个标签里面 label.setBounds(0, 0,
- * background.getIconWidth(), background.getIconHeight());
- * imagePanel=(JPanel
- * )this.getContentPane();//把内容窗格转化为JPanel,否则不能用setOpaque()使内容窗格透明
- * imagePanel.setOpaque(false); imagePanel.setLayout(new FlowLayout());
- * this.getLayeredPane().setLayout(null);//把背景图片添加到分层窗格的最底层作为背景
- * this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
- * this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- * this.setResizable(false);
- */
- setBac();
- Container c = this.getContentPane();
- JPanel jp = new JPanel();
- jp.setOpaque(false);
- c.add(jp);
- this.setBounds(400, 250, 500, 360);
- // this.setBounds(300,200,400,300);
- this.setVisible(true);
- }
- private void myEvent() {
- // TODO Auto-generated method stub
- jbt11.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // fig.setVisible(true);
- JFileChooser jfc = new JFileChooser();
- if (jfc.showOpenDialog(BuildSecret.this) == JFileChooser.APPROVE_OPTION) {
- String strPath = jfc.getSelectedFile().getAbsolutePath();
- jtf1.setText(strPath);
- }
- }
- });
- jbt21.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // fig.setVisible(true);
- JFileChooser jfc = new JFileChooser();
- if (jfc.showOpenDialog(BuildSecret.this) == JFileChooser.APPROVE_OPTION) {
- String strPath = jfc.getSelectedFile().getAbsolutePath();
- jtf2.setText(strPath);
- }
- }
- });
- jbt12.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- File file = new File(jtf1.getText());
- String str=file.getName();
- FileInputStream fis = new FileInputStream(file);
- FileOutputStream fos = new FileOutputStream("f:\\str.jpg");
- int len = 0;
- byte[] bs = new byte[100];
- while ((len = fis.read(bs, 10, 80)) != -1) {
- for (int i = 0; i < bs.length; i++) {
- fos.write(bs, 0, len + 20);
- }
- fos.close();
- fis.close();
- }
- } catch (FileNotFoundException e1) {
- System.out.println("操作的文件不存在");
- } catch (IOException e2) {
- System.out.println("发生IO操作异常");
- }
- }
- });
- jbt22.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- try {
- File file=new File(jtf2.getText());
- FileInputStream fis2 = new FileInputStream(file);
- FileOutputStream fos2 = new FileOutputStream("f:\\str1.jpg");
- int len2 = 0;
- byte[] bs2 = new byte[100];
- while ((len2 = fis2.read(bs2)) != -1) {
- fos2.write(bs2, 10, len2 - 20);
- }
- fos2.close();
- fis2.close();
- } catch (FileNotFoundException e1) {
- System.out.println("操作的文件不存在");
- } catch (IOException e1) {
- System.out.println("发生IO操作异常");
- }
- }
- });
- }
- public void setBac() {
- //加的一个背景
- ((JPanel) this.getContentPane()).setOpaque(false);
- ImageIcon img = new ImageIcon("f://123.jpg");
- JLabel background = new JLabel(img);
- this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
- background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
- }
- public static void main(String[] args) {
- new BuildSecret();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- }
- }
复制代码 |