java—Unicode-字符转换器

原创 陈 浩翔  2015-11-14 02:23  阅读 48 次

实现一个字符(包括汉字)的简单互相转换;

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);
    }
}

正常转换:

异常处理:

anyShare分享到:
本文地址:http://chenhaoxiang.cn/2015/11/14/1114/
关注我们:请关注一下我们的微信公众号:扫描二维码会Java的公众号,公众号:程序编程之旅
版权声明:本文为原创文章,版权归 陈 浩翔 所有,欢迎分享本文,转载请保留出处!

发表评论


表情