import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
* 加密解密软件
* 不使用java风格:JFrame.setDefaultLookAndFeelDecorated(true);
* 设置窗口位置:this.setLocation(widthSize / 1024 * 850,heightSize / 768 * 100)
* 窗口位于中央:this.setLocationRelativeTo(null)
* 窗口大小是否可以拖动:this.setResizable(false)
* 关闭风格:this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
* 设置窗体风格:
* UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
* SwingUtilities.updateComponentTreeUI(this);
* 设置窗口图标:
* Image image = Toolkit.getDefaultToolkit().createImage("images/001.png");
this.setIconImage(image);
* 设置窗口可见:this.setVisible(true)
* 屏幕尺寸:Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
* 字体样式大小:nameText.setFont(new Font(null, 0, 12));
* 设置闪烁的颜色:beforeTranText.setCaretColor(Color.white);
* 设置背景颜色:nameText.setBackground(Color.red)
* 设置字体颜色:nameText.setForeground(Color.red)
* 聚焦:pwdText.requestFocus();
* 弹出框:JOptionPane.showMessageDialog(this, "请输入密码!", "提示信息", 2);
* 图片处理:Image image = imageIcon.getImage();
image = image.getScaledInstance(mainWidth, mainHeight,
Image.SCALE_DEFAULT);
imageIcon.setImage(image);
imageLabel.setIcon(imageIcon);
*/
public class ChineseAndUnicode extends MyListener
{
/**
* 定义两个按钮,获取环境变量和获取系统配置
*/
JButton downTranBtn = new JButton("向下转码");
JButton upTranBtn = new JButton("向上还原");
JButton clearBeforeBtn = new JButton("清空▲");
JButton clearAfterBtn = new JButton("清空▼");
/**
* 定义两个文本框
*/
JTextArea beforeTranText = new JTextArea();
JTextArea afterTranText = new JTextArea();
/**
* 定义一个面板,存放各组件
*/
JPanel mainPanel = new JPanel();
/**
* 定义两个可滚动的面板
*/
JScrollPane beforePane = new JScrollPane(beforeTranText);
JScrollPane afterPane = new JScrollPane(afterTranText);
/**
* 获取屏幕尺寸
*/
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int widthSize;
int heightSize;
int mainWidthSize;
int mainHeihgtSize;
{
widthSize = (int)screenSize.getWidth();
heightSize = (int)screenSize.getHeight();
mainWidthSize = widthSize / 1024 * 900;
mainHeihgtSize = heightSize / 768 * 720;
}
public ChineseAndUnicode()
{
this.setTitle("转码工具");
this.setSize(mainWidthSize,mainHeihgtSize);
//设置窗口布局
this.setLayout(new BorderLayout());
//设置窗口是否可以拖动
this.setResizable(false);
//加载各组件
this.loadComp();
setDefaultCloseOperation(3);
//设置窗口位置位于中央
this.setLocationRelativeTo(null);
this.setVisible(true);
}
/**
* 加载各个组件
*/
private void loadComp()
{
mainPanel.setSize(mainWidthSize, mainHeihgtSize);
mainPanel.setLayout(null);
mainPanel.setBackground(Color.gray);
int textWidth = widthSize / 1024 * 890;
int textHeight = this.heightSize / 768 * 350;
//设置背景颜色
beforeTranText.setBackground(Color.black);
afterTranText.setBackground(Color.black);
//设置字体颜色
beforeTranText.setForeground(Color.white);
afterTranText.setForeground(Color.red);
//设置文本框中闪烁的颜色
beforeTranText.setCaretColor(Color.white);
afterTranText.setCaretColor(Color.red);
//设置可以换行
beforeTranText.setLineWrap(true);
afterTranText.setLineWrap(true);
beforePane.setBounds(0, 0, textWidth, textHeight);
afterPane.setBounds(0, heightSize / 768 * 370, textWidth, textHeight);
int btnWidth = widthSize/1024*100;
int btnHeight = heightSize / 768 * 20;
clearBeforeBtn.setBounds(widthSize/1024*100, heightSize / 768 * 350, btnWidth, btnHeight);
downTranBtn.setBounds(widthSize/1024*300, heightSize / 768 * 350, btnWidth, btnHeight);
upTranBtn.setBounds(widthSize/1024*500, heightSize / 768 * 350, btnWidth, btnHeight);
clearAfterBtn.setBounds(widthSize/1024*700, heightSize / 768 * 350, btnWidth, btnHeight);
mainPanel.add(beforePane);
mainPanel.add(afterPane);
mainPanel.add(downTranBtn);
mainPanel.add(upTranBtn);
mainPanel.add(clearBeforeBtn);
mainPanel.add(clearAfterBtn);
clearBeforeBtn.addActionListener(this);
clearAfterBtn.addActionListener(this);
downTranBtn.addActionListener(this);
upTranBtn.addActionListener(this);
this.add(mainPanel);
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
new ChineseAndUnicode();
}
public void actionPerformed(ActionEvent e)
{
//前面文本框的清除按钮
if (e.getSource() == clearBeforeBtn)
{
beforeTranText.setText("");
}
//后面文本框的清除按钮
if (e.getSource() == clearAfterBtn)
{
afterTranText.setText("");
}
//向下转码按钮
if (e.getSource() == downTranBtn)
{
try
{
String strs = beforeTranText.getText();
strs = chinaToUnicode(strs);
afterTranText.setText(strs);
}
catch (Exception ee)
{
JOptionPane.showMessageDialog(this, "含有非法字符不能转化,请检查!", "提示信息", 2);
}
}
//向上还原按钮
if (e.getSource() == upTranBtn)
{
try
{
String strs = afterTranText.getText();
strs = unicodeToChinese(strs);
beforeTranText.setText(strs);
}
catch (Exception ee)
{
JOptionPane.showMessageDialog(this, "含有非法字符不能转化,请检查!", "提示信息", 2);
}
}
}
/**
* 中文转unicode
* @param str
* @return String 反回unicode编码
*/
private String chinaToUnicode(String str)
{
StringBuffer result = new StringBuffer();
for (int i = 0; i < str.length(); i++)
{
int chr1 = (char) str.charAt(i);
//汉字范围 \u4e00-\u9fa5 (中文)
if(chr1 >= 19968 && chr1 <= 171941)
{
result.append("\\u" + Integer.toHexString(chr1));
}
else
{
result.append(str.charAt(i));
}
}
return result.toString();
}
/**
* unicode转中文
* @param str 要转的Unicode字符串
* @return String 转化后的字符串
* @throws IOException
* @throws InterruptedException
*/
private String unicodeToChinese(String strs) throws IOException, InterruptedException
{
File file = new File("C:\\temp");
if (!file.exists())
{
file.mkdir();
}
File fileFrom = new File(file,"from.txt");
File toFrom = new File(file,"to.txt");
if (!fileFrom.exists())
{
fileFrom.createNewFile();
}
if (!toFrom.exists())
{
toFrom.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new FileWriter(toFrom));
bw.write(strs);
bw.close();
//运行系统的命令
Runtime rt = Runtime.getRuntime();
Process pro = rt.exec("cmd /c native2ascii.exe -reverse C:\\temp\\to.txt > C:\\temp\\from.txt");
pro.waitFor();
//toFrom.delete();
BufferedReader br = new BufferedReader(new FileReader(fileFrom));
StringBuffer sb = new StringBuffer();
String readStr = br.readLine();
while (null != readStr)
{
sb.append(readStr);
sb.append("\n");
readStr = br.readLine();
}
br.close();
//System.out.println(sb.toString());
//fileFrom.delete();
return sb.toString();
}
}
package com.jia.sysconfig;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class MyListener extends JFrame implements ActionListener,MouseListener
{
/**
* 序列号
*/
private static final long serialVersionUID = 1L;
@Override
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
}