实现一个字符(包括汉字)的简单互相转换;
package cn.hncu.gui2;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class QueryFrame extends Frame implements ActionListener {
private TextField tfd1,tfd2;
private Button btnChar,btnUni;
public QueryFrame(String str) {
super(str);
this.setBounds(300,240,300,150);
this.setBackground(Color.LIGHT_GRAY);
this.setLayout(new FlowLayout(FlowLayout.RIGHT));
tfd1 = new TextField("汉字",10);
this.add(new Label("请输入要查询的汉字"));
this.add(tfd1);
tfd2 = new TextField(10);
this.add(new Label("Unicode码值"));
this.add(tfd2);
btnUni = new Button("查询Unicode码");
btnChar = new Button("查询字符");
this.add(btnUni);
this.add(btnChar);
btnUni.addActionListener(this);
btnChar.addActionListener(this);
this.addWindowListener(new Win2Close());
this.setVisible(true);
}
public static void main(String[] args) {
new QueryFrame("Unicode字符查询器");
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnUni){
String str = tfd1.getText();
char ch = str.charAt(0);
tfd2.setText(""+(int)ch);
}else if(e.getSource()==btnChar){
String str = tfd2.getText();
try {
int n = Integer.parseInt(str);
tfd1.setText(""+(char)n);
} catch (NumberFormatException e1) {
tfd1.setText(str+ "不能转换");
}
}
}
}
class Win2Close extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
正常转换:
异常处理: