java反编译程序

package com.mdj.test;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

import com.mdj.unmake.Main;
/**
 *
 * @author 武汉软件工程职业学院<br>孟德军
 * @version 1.0
 */
public class Start extends JFrame {
 private JWindow window;
 private String filepath;

 public Start() {
  filepath = System.getProperty("user.dir");
  window = new JWindow(this);
  Image logo = Toolkit.getDefaultToolkit().getImage(
    filepath + "
//start.jpg");
  Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();

  JPanel p = new JPanel(new GridLayout(2,2));
  JLabel a = new JLabel("******** 欢迎使用本软件,/n 版权所有:孟德军 *********",
    JLabel.CENTER);
  a.setFont(new Font("宋体",Font.ITALIC,16));
  JLabel b = new JLabel(new ImageIcon(logo), JLabel.CENTER);

  p.add(b);
  p.add(a);

  window.add(p, "Center");

  window.setLocation(scrSize.width /5, scrSize.height / 4);
  window.setSize(scrSize.width / 3 + 50, scrSize.height / 3);
  window.show();
  window.toFront();
  try {
   Thread.currentThread().sleep(2000);
   // Thread.currentThread();可以获得主线程的一个应用。
   Main main = new Main("java反编译程序");
   main.setVisible(true);

  } catch (Exception e) {
   e.printStackTrace();
  }
  window.dispose();

 }

 public static void main(String args[]) {
  System.out.println("server start...please wait a minute!");
  new Start();
 }
}
///////////////////////////////////////////////////////
package com.mdj.unmake;

import java.awt.Toolkit;

public class Share {
 public static final int SCREENHEIGHT = Toolkit.getDefaultToolkit()
   .getScreenSize().height;
 public static final int SCREENWIDTH = Toolkit.getDefaultToolkit()
   .getScreenSize().width;
}
///////////////////////////////////////////////////////
package com.mdj.unmake;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Main extends JFrame {
 private TextArea textarea;
 private MenuBar mainbar;
 private JScrollPane c;
 private Menu file, edit, view, help;
 private MenuItem load, save, exit, all, copy, fresh, about, back, font,
   system, open;
 private Font f;
 private Toolkit tk;

 public Main(String title) {
  super(title);
  tk = Toolkit.getDefaultToolkit();
  Image image = tk.createImage(System.getProperty("user.dir")
    + "//logo.gif"); /* image.gif是你的图标 */
  this.setIconImage(image);
  init();
  display();
  this.add(textarea);
  // this.add(sb);
  this.setMenuBar(mainbar);
  this.setSize(Share.SCREENWIDTH / 2, Share.SCREENHEIGHT / 2);
  this.setLocation(Share.SCREENWIDTH / 4, Share.SCREENHEIGHT / 4);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  load.addActionListener(new Listener());
  save.addActionListener(new Listener());
  exit.addActionListener(new Listener());
  all.addActionListener(new Listener());
  copy.addActionListener(new Listener());
  fresh.addActionListener(new Listener());
  about.addActionListener(new Listener());
  open.addActionListener(new Listener());
  back.addActionListener(new Listener());
  system.addActionListener(new Listener());
 }

 private void init() {
  Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
  f = new Font("宋体", Font.PLAIN, 13);
  Font fontset = new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 18);
  textarea = new TextArea();
  textarea.setFont(fontset);
  textarea.setCursor(cursor);
  mainbar = new MenuBar();
  file = new Menu("文件");
  edit = new Menu("编辑");
  view = new Menu("视图");
  help = new Menu("帮助");
  open = new MenuItem("打开");
  load = new MenuItem("加载");
  save = new MenuItem("保存");
  exit = new MenuItem("退出");
  all = new MenuItem("全选");
  copy = new MenuItem("复制");
  fresh = new MenuItem("刷新");
  about = new MenuItem("关于");
  back = new MenuItem("背景色");
  font = new MenuItem("字体");
  system = new MenuItem("系统信息");
  c = new JScrollPane();
  file.setFont(f);
  open.setFont(f);
  edit.setFont(f);
  view.setFont(f);
  help.setFont(f);
  load.setFont(f);
  save.setFont(f);
  exit.setFont(f);
  copy.setFont(f);
  all.setFont(f);
  fresh.setFont(f);
  about.setFont(f);
  back.setFont(f);
  font.setFont(f);

  system.setFont(f);
  // textarea.add(c);
 }

 public void display() {
  mainbar.add(file);
  mainbar.add(edit);
  mainbar.add(view);
  mainbar.add(help);
  file.add(load);
  file.add(open);
  file.add(save);
  file.add(exit);
  edit.add(all);
  edit.add(copy);
  edit.add(fresh);
  view.add(back);
  edit.add(font);
  help.add(about);
  help.add(system);
 }

 /*
  * public static void main(String args[]) { Main main = new
  * Main("java反编译程序"); main.setVisible(true); }
  */

 private class Listener implements ActionListener {

  public void actionPerformed(ActionEvent s) {
   while (s.getSource() == load) {
    System.out.println("load");
    loadClick();
    break;
   }
   while (s.getSource() == save) {
    System.out.println("save");
    saveClick();
    break;
   }
   while (s.getSource() == exit) {
    System.out.println("exit");
    System.exit(0);
   }
   while (s.getSource() == about) {
    JOptionPane.showMessageDialog(null,
      "该软件由java编写,开发平台为myeclipse /n 希望大家多多支持!/t 孟德军", "关于",
      JOptionPane.INFORMATION_MESSAGE);
    break;
   }
   while (s.getSource() == back) {
    colorSet();
    break;
   }
   while (s.getSource() == system) {
    systemInformation();
    break;
   }
   while (s.getSource() == font) {
    new FontDialog().setVisible(true);
   }
   while (s.getSource() == open) {
    openClick();
    break;
   }
  }

  /*
   * private void copySet(){ String temp=textarea.getSelectedText();
   * //拖动鼠标选取文本。 String Selection text=new String Selection(temp);
   * clipboard.setContents(text,null); }
   */
  private void colorSet() {
   Color newcolor = JColorChooser.showDialog(null, "颜色设置", null);
   textarea.setBackground(newcolor);
   // textarea.setCursor(new Cursor().getDefaultCursor());

  }

  private void loadClick() {
   Input myinput = new Input();
   myinput.setVisible(true);
  }

  private void openClick() {
   JFileChooser jfile = new JFileChooser();
   jfile.setFileFilter(new javax.swing.filechooser.FileFilter() {
    public boolean accept(File f) { // 设定可用的文件的后缀名
     if (f.getName().endsWith(".java") || f.isDirectory()) {
      return true;
     }
     return false;
    }

    public String getDescription() {
     return "文本文件(*.txt)";
    }
   });
   int result = jfile.showOpenDialog(null);
   if (result == JFileChooser.OPEN_DIALOG) {
    File dir = jfile.getCurrentDirectory();
    File file = jfile.getSelectedFile();
    textarea.setText(readFile(new File(dir, file.getName())));
    // System.out.println(readFile(new File(dir, file.getName())));
   } else if (result == JFileChooser.CANCEL_OPTION) {
    return;
   }

  }

  private String readFile(File file) {
   System.out.println("readfile...");
   try {
    BufferedReader read = new BufferedReader(new InputStreamReader(
      new FileInputStream(file), "GBK"));
    String data = null;
    StringBuffer buffer = new StringBuffer();
    while ((data = read.readLine()) != null) {
     buffer.append(data + "/n");
    }
    read.close();
    return buffer.toString();
   } catch (IOException e) {
    throw new RuntimeException(e);
   }

  }

  private void systemInformation() {
   Runtime rt = Runtime.getRuntime();
   JOptionPane.showConfirmDialog(null, "剩余内存:  " + rt.freeMemory()
     + " KB" + "/n" + "总内存:  " + rt.totalMemory() + " KB",
     "系统信息", JOptionPane.OK_OPTION, JOptionPane.WARNING_MESSAGE);

  }

  private void saveClick() {
   // 该方法用于保存文件。
   JFileChooser jfile = new JFileChooser();
   jfile.setFileFilter(new javax.swing.filechooser.FileFilter() {
    public boolean accept(File f) { // 设定可用的文件的后缀名
     if (f.getName().endsWith(".java") || f.isDirectory()) {
      return true;
     }
     return false;
    }

    public String getDescription() {
     return "文本文件(*.txt)";
    }
   });
   int result = jfile.showSaveDialog(null);
   if (result == JFileChooser.APPROVE_OPTION) {
    File dir = jfile.getCurrentDirectory();
    File file = jfile.getSelectedFile();
    write(new File(dir, file.getName() + ".txt"), textarea
      .getText());

   }
   if (result == JFileChooser.CANCEL_OPTION) {
    return;
   }

  }

  public void write(File file, String str) {
   try {
    PrintWriter write = new PrintWriter(new OutputStreamWriter(
      new FileOutputStream(file), "UTF-8"));
    write.println(str);
    write.close();
   } catch (IOException e) {
    throw new RuntimeException(e);
   }
  }

 }

 private class Input extends JFrame {
  private JLabel classname;
  private JTextField classnametext;
  private JButton okbutton, cancelbutton;
  private Toolkit tcl;

  public Input() {
   init();
   tcl = Toolkit.getDefaultToolkit();
   Image image = tcl.createImage(System.getProperty("user.dir")
     + "//logo.gif");
   this.setIconImage(image);
   this.setSize(250, 100);
   this.setLocation(Share.SCREENWIDTH / 4, Share.SCREENHEIGHT / 4);
   this.setTitle("请输入完整类名");
   this.add(classname);
   this.add(classnametext);
   this.add(okbutton);
   this.add(cancelbutton);
   this.setLayout(new FlowLayout());
   this.setResizable(false);
   cancelbutton.addActionListener(new InputClick());
   okbutton.addActionListener(new InputClick());

  }

  private void init() {
   Font f = new Font("宋体", Font.PLAIN, 14);
   classname = new JLabel("类名称");
   classnametext = new JTextField(20);
   okbutton = new JButton("确定");
   cancelbutton = new JButton("取消");
   okbutton.setMnemonic('y');
   cancelbutton.setMnemonic('n');
   classname.setFont(f);
   okbutton.setFont(f);
   cancelbutton.setFont(f);
   classnametext.setFont(f);
  }

  private void loading() {
   StringBuffer buffer = new StringBuffer();
   System.out.println("loading");
   try {
    Class classtype = Class.forName(classnametext.getText());
    Object instance = classtype.newInstance();
    System.out.println(checkClass(classtype.getName()));
    System.out.println(checkEmbelLKish(classtype.getName()));
    buffer.append("public class " + instance.getClass().getName()
      + "{/n/n");
    Field[] field = classtype.getDeclaredFields();
    for (int i = 0; i < field.length; i++) {
     buffer.append(field[i].toString());
     buffer.append(";/n/n");
    }
    Constructor[] con = classtype.getConstructors();
    for (int i = 0; i < con.length; i++) {
     buffer.append(con[i].toString());
     buffer.append("/n{/n");
     buffer.append("// TODO Auto-generated method stub/n");
     buffer.append("}/n");
    }
    Method[] mehtod = classtype.getDeclaredMethods();
    for (int x = 0; x < mehtod.length; x++) {
     buffer.append("/n");
     buffer.append(mehtod[x].toString());
     buffer.append("{/n/n");
     buffer.append("// TODO Auto-generated method stub/n");
     buffer.append("}/n");
    }
    buffer.append("/n/n}");
    buffer
      .append("/n/n/** This program copyright by xiaomeng /n/n
[email protected] **//n");
    buffer.append("/n///tTHE TIME:" + new Date());
    textarea.setText(buffer.toString());
   } catch (ClassNotFoundException e) {
    showMessage(e.getMessage()
      + "/nClassNotFoundException,please check the classname");
   } catch (InstantiationException e) {
    showMessage(e.getMessage()
      + "/nInstantiationException,please check the classname");
   } catch (IllegalAccessException e) {
    showMessage(e.getMessage()
      + "/nIllegalAccessException,please check the classname");
   }
  }

  private String checkClass(String s) {
   int a = 0;
   String sub = null;
   int b = s.lastIndexOf(".");
   if (b != 0) {
    a = s.length();
    sub = s.substring(b + 1, a);
   }
   return sub;
  }

  private String checkEmbelLKish(String s) {
   int a = 0;
   String sub = null;
   int b = s.indexOf(".");
   if (b != 0) {
    a = s.length();
    sub = s.substring(0, b - 1);
   }
   return sub;
  }

  private void showMessage(String s) {
   JOptionPane.showMessageDialog(null, s, "出错啦!",
     JOptionPane.OK_OPTION);
  }

  private class InputClick implements ActionListener {

   public void actionPerformed(ActionEvent e) {
    if (e.getSource() == cancelbutton) {
     dispose();
    } else if (e.getSource() == okbutton) {
     loading();
     dispose();
    }
   }

  }
 }

 private class FontDialog extends JFrame {
  private JLabel fontLabel, fontStyle, fontSize;
  private JComboBox fontName, fontStyleName, fontSizeName;
  private JButton okButton, cancelButton;

  public FontDialog() {
   this.setTitle("字体");
   init();
   addItem();
   this.setLayout(new FlowLayout());
   this.add(fontLabel);
   this.add(fontStyle);
   this.add(fontSize);
   this.add(fontName);
   this.add(fontStyleName);
   this.add(fontSizeName);
   this.add(okButton);
   this.add(cancelButton);
   this.setSize(300, 150);
   this.setLocation(Share.SCREENWIDTH / 4, Share.SCREENHEIGHT / 4);

  }

  private void init() {
   fontLabel = new JLabel("字体");
   fontStyle = new JLabel("字形");
   fontSize = new JLabel("大小");
   okButton = new JButton("OK");
   cancelButton = new JButton("CANCEL");
   fontName = new JComboBox();
   fontStyleName = new JComboBox();
   fontSizeName = new JComboBox();

  }

  private void addItem() {
   // 字体列表
   int[] stylename = { Font.BOLD, Font.CENTER_BASELINE,
     Font.HANGING_BASELINE, Font.ITALIC,
     Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_LEFT_TO_RIGHT,
     Font.LAYOUT_NO_LIMIT_CONTEXT, Font.LAYOUT_NO_START_CONTEXT,
     Font.LAYOUT_RIGHT_TO_LEFT, Font.PLAIN, Font.ROMAN_BASELINE,
     Font.TRUETYPE_FONT, Font.TYPE1_FONT };
   String[] fontname = Toolkit.getDefaultToolkit().getFontList();
   for (int i = 0; i < stylename.length; i++) {
    fontStyleName.addItem(stylename[i]);
   }
   for (int i = 0; i < fontname.length; i++) {
    fontName.addItem(fontname[i]);
   }
   for (int i = 0; i < 72; i++) {
    fontSizeName.addItem(i);
   }
  }

  public String getName() {
   return fontName.getSelectedItem().toString();
  }

  public int getStyleName() {
   return Integer.parseInt(fontStyleName.getSelectedItem().toString());
  }

  public int getFontSize() {
   return Integer.parseInt(fontSizeName.getSelectedItem().toString());
  }
 }
}

你可能感兴趣的:(java反编译程序)