前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >java---Unicode-字符转换器

java---Unicode-字符转换器

作者头像
谙忆
发布2021-01-20 15:58:36
发布2021-01-20 15:58:36
1.2K00
代码可运行
举报
文章被收录于专栏:程序编程之旅程序编程之旅
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
复制
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);
    }
}

正常转换:

异常处理:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015/11/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档