• java---Unicode-字符转换器


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

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

    正常转换:

    异常处理:

  • 相关阅读:
    Orleans 2 实例
    Linux基础1 目录和文件系统
    C#中的异步多线程补充1
    委托的小例子(基本委托,匿名方法,lambda)
    Orleans 1 基本概念
    WPF10 Binding-2
    WPF9 Binding-1
    WPF8 UI布局
    WPF7 布局控件
    软工总结
  • 原文地址:https://www.cnblogs.com/webmen/p/5739549.html
Copyright © 2020-2023  润新知