啊吧翻译器
婴语翻译
用Java写的一个非常简单的程序,可以将一句话翻译成婴语,啊吧啊吧啊吧 = ̄ω ̄=
使用了Java Swing,UTF-8编码。
一. UI
UI 使用java 的Swing组件,上下两个JTextArea ,两个button,对应两个事件监听
获取上面文本框的内容加密后输出到下面文本框
获取下面文本框的内容解密后输出到上面文本框。
public class EncryptionDecryptionGUI extends JFrame { private final JTextArea upperTextArea; private final JTextArea lowerTextArea; public EncryptionDecryptionGUI() { setTitle("啊吧翻译器 @jinnanDu"); setSize(800, 1000); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // 设置字体 Font font = new Font("SimSun", Font.PLAIN, 16); // 创建并添加上面的文本 upperTextArea = new JTextArea(5, 20); upperTextArea.setLineWrap(true); upperTextArea.setWrapStyleWord(true); upperTextArea.setPreferredSize(new Dimension(500, 120)); upperTextArea.setFont(font); JPanel upperPanel = new JPanel(); upperPanel.add(new JLabel("输入加密:")); upperPanel.add(upperTextArea); add(upperPanel, BorderLayout.NORTH); // 创建并添加下面的文本 lowerTextArea = new JTextArea(10, 25); lowerTextArea.setLineWrap(true); lowerTextArea.setWrapStyleWord(true); lowerTextArea.setPreferredSize(new Dimension(500, 750)); lowerTextArea.setFont(font); JScrollPane lowerScrollPane = new JScrollPane(lowerTextArea); add(lowerScrollPane, BorderLayout.CENTER); JPanel lowerPanel = new JPanel(); lowerPanel.add(new JLabel("输入解密:")); lowerPanel.add(lowerTextArea); add(lowerPanel, BorderLayout.CENTER); // 创建并添加按钮 JButton encryptButton = new JButton("加密"); JButton decryptButton = new JButton("解密"); abaaba ab = new abaaba(); encryptButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String upInput = upperTextArea.getText(); String lowShow = ab.encryAba(upInput); lowerTextArea.setText(lowShow); } }); decryptButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String lowInput = lowerTextArea.getText(); String upShow = ab.decryAba(lowInput); upperTextArea.setText(upShow); } }); JPanel buttonPanel = new JPanel(); buttonPanel.add(encryptButton); buttonPanel.add(decryptButton); add(buttonPanel, BorderLayout.SOUTH); setVisible(true); } }
二. Encryption&Decryption
程序非常简单,核心就是以下加密解密两个方法。
- 加密:将字符串二进制表示转换为由中文字符'吧'和'啊'组成的字符串。
- 解密:将啊吧转换为人类可读的字符串。
//将字符串转换为二进制 public String encryAba(String chineseString){ if(chineseString.isEmpty()){ throw new NullPointerException("加密的数据不能为空"); } byte[] bytes = chineseString.getBytes(); StringBuilder binaryString = new StringBuilder(); for (byte b : bytes) { // 将每个字节转换为8位二进制字符串 for (int i = 7; i >= 0; i--) { binaryString.append((b & (1 << i))!= 0? '吧' : '啊'); } } return binaryString.toString(); }
public String decryAba(String aba){ if(aba.isEmpty()){ throw new NullPointerException("解密的数据不能为空"); } StringBuilder binaryString = new StringBuilder(); for (char c : aba.toCharArray()) { if (c == '啊') { binaryString.append('0'); } else if (c == '吧') { binaryString.append('1'); } } if (binaryString.length() % 8!= 0) { throw new IllegalArgumentException("二进制字符串长度必须是8的倍数"); } byte[] bytes = new byte[binaryString.length() / 8]; for (int i = 0; i < binaryString.length(); i += 8) { String byteBinary = binaryString.substring(i, i + 8); bytes[i / 8] = (byte) Integer.parseInt(byteBinary, 2); } return new String(bytes); }
三. Package
打包成jar,再使用Launch4j将jar转换为windows 可执行的exe。
双击执行,效果如下:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验