Java小小的程序1之MD5计算器

 用Java写计算字符串的MD5值的计算器。用了两个.java文件,共有四个类,其中一个计算并返回MD5值,一个是界面功能的类,一个是内部类,实现按钮的功能的,另一个是实现用浏览器打开指定网页的,采用了网上的一段代码。把代码贴上,望指点。

这是计算功能的代码。

import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5 { private MessageDigest md5; private String inString; MD5(String input) { inString = input; try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } // calculate md5 value, and return as Hex value string String calculate() { byte[] md5Value = this.md5.digest(inString.getBytes()); StringBuffer valueHex = new StringBuffer(); int tmp; for (int i = 0; i < md5Value.length; i++) { tmp = md5Value[i] & 0xff; // 当tmp<16时,转换为16进制值将缺少一个0 if (tmp < 16) { valueHex.append(0); } valueHex.append(Integer.toHexString(tmp)); } return valueHex.toString(); } public static void main(String[] args) { } }
这是界面功能的代码。
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class MainPanel extends JFrame{ private String inString; private String compareString; private String outputString; static JTextField inputText = new JTextField(); static JTextField outputText = new JTextField(); static JTextField compareText = new JTextField(); static JButton compute = new JButton("计算"); static JButton compare = new JButton("比较"); static JButton empty = new JButton("清空"); static JButton freeback = new JButton("意见反馈"); // create main user interface MainPanel() { super("MD5计算器 V0.1.0"); setIconImage(new ImageIcon("./res/icon.gif").getImage()); setSize(320, 160); setResizable(false); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); jp.setLayout(new BorderLayout()); JPanel labels = new JPanel(); labels.setLayout(new GridLayout(3, 1)); labels.add(new JLabel(" 原信息 ")); labels.add(new JLabel(" MD5值 ")); labels.add(new JLabel(" 参考值 ")); JPanel texts = new JPanel(); GridLayout textLayout = new GridLayout(3, 1); textLayout.setVgap(5); texts.setLayout(textLayout); outputText.setEditable(false); texts.add(inputText); texts.add(outputText); texts.add(compareText); JPanel buttons = new JPanel(); FlowLayout buttonsLay = new FlowLayout(); buttonsLay.setHgap(10); buttons.setLayout(buttonsLay); NewAction newAction = new NewAction(); compute.addActionListener(newAction); compare.addActionListener(newAction); empty.addActionListener(newAction); freeback.addActionListener(newAction); buttons.add(compute); buttons.add(compare); buttons.add(empty); buttons.add(freeback); jp.add(labels, BorderLayout.WEST); jp.add(texts, BorderLayout.CENTER); jp.add(buttons, BorderLayout.SOUTH); setContentPane(jp); } public static void main(String[] args) { MainPanel panel = new MainPanel(); panel.setVisible(true); } class NewAction implements ActionListener { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JButton button = (JButton) e.getSource(); if (button == compute) { MD5 md5 = new MD5(inputText.getText()); outputString = md5.calculate(); outputText.setText(outputString); } else if (button == compare) { if (outputText.getText().equals("")) { JOptionPane.showMessageDialog(null, "请先计算"); } else { if (outputText.getText().equals(compareText.getText())) { JOptionPane.showMessageDialog(null, "MD5值相同"); } else { JOptionPane.showMessageDialog(null, "MD5值不同"); } } } else if (button == empty) { inputText.setText(null); outputText.setText(null); compareText.setText(null); inString = null; outputString = null; compareString = null; } else { try { BareBonesBrowserLaunch.browse("http://blog.sina.com.cn/s/blog_03f9d3" "6e0100qihm.html"); } catch (IllegalArgumentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InvocationTargetException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (NoSuchMethodException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } } // 这段代码取自网上,现一时未能找到,望原作见谅。 class BareBonesBrowserLaunch { public static void openURL(String url) { try { browse(url); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:/n" + e.getLocalizedMessage()); } } static void browse(String url) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InterruptedException, InvocationTargetException, IOException, NoSuchMethodException { String osName = System.getProperty("os.name", ""); if (osName.startsWith("Mac OS")) { Class fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class }); openURL.invoke(null, new Object[] { url }); } else if (osName.startsWith("Windows")) { Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler " + url); } else { // assume Unix or Linux String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String browser = null; for (int count = 0; count < browsers.length && browser == null; count++) if (Runtime.getRuntime() .exec(new String[] { "which", browsers[count] }) .waitFor() == 0) browser = browsers[count]; if (browser == null) throw new NoSuchMethodException("Could not find web browser"); else Runtime.getRuntime().exec(new String[] { browser, url }); } } }
上一篇:Spring Cloud构建微服务架构(四)分布式配置中心


下一篇:deferred 延迟加载js