package chage.ui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import chage.manager.UnicodeAndChar;
public class UnicodeAndCharToChange extends JFrame{
private JPanel northPanel, centerPanel, southPanel ;
private JTextField sourceTextField, toUnicodeTextField ;
private JTextArea sourceTextArea, toUnicodeTextArea ;
private JLabel sourceFieldLabel, toUnicodeFieldLabel, sourceAreaLabel, toUnicodeAreaLabel ;
private JScrollPane sourceScroll, toUnicodeScroll ;
private JButton topChangeButton, backButton, bottomChangeButton, bottomBackButton,topContraryChange, bottomContraryChange ;
private MyActionListener mal = new MyActionListener() ;
private UnicodeAndChar unicodeAndChar ;
public UnicodeAndCharToChange() {
super("unicode码与十进制码互换") ;
initNorthPanel() ;
initCenterPanel() ;
initSouthPanel() ;
initJFrame() ;
}
public void initNorthPanel() {
northPanel = new JPanel(new BorderLayout()) ;
JPanel nNorthPanel = new JPanel() ;
JPanel nCenterPanel = new JPanel() ;
JPanel nSouthPanel = new JPanel() ;
sourceTextField = new JTextField(50) ;
sourceFieldLabel = new JLabel("源 十 进 制 码:") ;
toUnicodeTextField = new JTextField(50) ;
// toUnicodeTextField.setEditable(false);
toUnicodeFieldLabel = new JLabel("十六进 制 unicode 码:") ;
topChangeButton = new JButton("转换") ;
backButton = new JButton("退出") ;
topContraryChange = new JButton("反转换") ;
topChangeButton.addActionListener(mal) ;
topContraryChange.addActionListener(mal) ;
backButton.addActionListener(mal);;
nSouthPanel.add(topChangeButton) ;
nSouthPanel.add(topContraryChange) ;
nSouthPanel.add(backButton) ;
nNorthPanel.add(sourceFieldLabel, Label.LEFT) ;
nNorthPanel.add(sourceTextField) ;
nCenterPanel.add(toUnicodeFieldLabel, Label.LEFT) ;
nCenterPanel.add(toUnicodeTextField) ;
northPanel.add(nNorthPanel, BorderLayout.NORTH) ;
northPanel.add(nCenterPanel) ;
northPanel.add(nSouthPanel, BorderLayout.SOUTH) ;
}
public void initCenterPanel() {
centerPanel = new JPanel(new BorderLayout()) ;
JPanel cNorthPanel = new JPanel() ;
JPanel cCenterPane = new JPanel() ;
sourceTextArea = new JTextArea(10,50) ;
sourceScroll = new JScrollPane(sourceTextArea) ;
sourceAreaLabel = new JLabel("源 十 进 制 码 域:", Label.LEFT) ;
toUnicodeTextArea = new JTextArea(10,50) ;
// toUnicodeTextArea.setEditable(false);
toUnicodeScroll = new JScrollPane(toUnicodeTextArea) ;
toUnicodeAreaLabel = new JLabel("十六进制unicode码域:", Label.LEFT) ;
cNorthPanel.add(sourceAreaLabel) ;
cNorthPanel.add(sourceScroll) ;
cCenterPane.add(toUnicodeAreaLabel) ;
cCenterPane.add(toUnicodeScroll) ;
centerPanel.add(cNorthPanel, BorderLayout.NORTH) ;
centerPanel.add(cCenterPane) ;
}
public void initSouthPanel() {
southPanel = new JPanel() ;
bottomChangeButton = new JButton("转换") ;
bottomBackButton = new JButton("退出") ;
bottomContraryChange = new JButton("反转换") ;
bottomChangeButton.addActionListener(mal) ;
bottomBackButton.addActionListener(mal) ;
bottomContraryChange.addActionListener(mal);
southPanel.add(bottomChangeButton) ;
southPanel.add(bottomContraryChange) ;
southPanel.add(bottomBackButton) ;
}
public void initJFrame() {
this.add(northPanel, BorderLayout.NORTH) ;
this.add(centerPanel) ;
this.add(southPanel, BorderLayout.SOUTH) ;
this.pack() ;
Dimension size = Toolkit.getDefaultToolkit().getScreenSize() ;
this.setLocation(size.width / 2 - this.getWidth() / 2, size.height / 2 - this.getHeight() / 2) ;
this.setVisible(true) ;
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public String contraryChange(String str) {
FileReader fr = null ;
BufferedReader br = null ;
BufferedWriter bw = null ;
FileWriter fw = null ;
String fileStr = "" ;
try {
fw = new FileWriter("c:/temp.txt") ;
bw = new BufferedWriter(fw) ;
bw.write("\"" + str.toString() + "\"") ;
fr = new FileReader("c:/temp.txt") ;
br = new BufferedReader(fr) ;
String line = "" ;
while((line = br.readLine()) != null) {
fileStr += line ;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br != null) {
try {
br.close() ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fr != null) {
try {
fr.close() ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bw != null) {
try {
bw.flush() ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bw.close() ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if(br != null) {
try {
br.close() ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return fileStr.toString() ;
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
unicodeAndChar = new UnicodeAndChar() ;
if(e.getSource().equals(topChangeButton)) {
String text = unicodeAndChar.charToUnicode(sourceTextField.getText()) ;
toUnicodeTextField.setText(text);
toUnicodeTextField.selectAll() ;
toUnicodeTextField.copy() ;
return ;
}
if(e.getSource().equals(bottomChangeButton)) {
String textArea = unicodeAndChar.charToUnicode(sourceTextArea.getText()) ;
toUnicodeTextArea.setText(textArea) ;
toUnicodeTextArea.selectAll() ;
toUnicodeTextArea.copy() ;
return ;
}
if(e.getSource().equals(topContraryChange)) {
String text = unicodeAndChar.unicodeToChar(toUnicodeTextField.getText());
sourceTextField.setText(text);
return ;
}
if(e.getSource().equals(bottomContraryChange)) {
String textArea =toUnicodeTextArea.getText();
String fileStr = contraryChange(textArea) ;
System.out.println(fileStr);
sourceTextArea.setText(fileStr) ;
return ;
}
if(e.getSource().equals(bottomBackButton) || e.getSource().equals(backButton)){
System.exit(0) ;
return ;
}
}
}
public static void main(String[] args) {
new UnicodeAndCharToChange() ;
}
}