java---Unicode-字符转换器

举报
谙忆 发表于 2021/05/28 06:57:08 2021/05/28
【摘要】 实现一个字符(包括汉字)的简单互相转换; 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.Text...

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

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

正常转换:

异常处理:

文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。

原文链接:chenhx.blog.csdn.net/article/details/49835563

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。