Unicode字符编码查询器。

这是一个使用Java AWT库创建的GUI应用程序,名为QueryFrame,实现了Unicode字符编码查询功能。用户可以输入字符查询其Unicode编码,或者输入Unicode编码查询对应的字符。程序包含两个文本字段和两个按钮,分别用于输入和查询。此外,程序还注册了窗口事件监听器,当窗口关闭时会结束程序运行。
摘要由CSDN通过智能技术生成

QueryFrame类

// Unicode字符编码查询器。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//字符编码查询器框架类,继承框架;声明ActionListener接口表示要响应动作事件
public class QueryFrame extends Frame implements ActionListener {
  private TextField text_char,text_uni;//显示字符和编码文本行
  private Button button_char,button_uni;//查询编码和字符按钮
  //上述对象在构造方法中创建引用实例,在实践处理方法中使用,所以声明为成员变量
  public QueryFrame(){//构造方法
      super("字符编码查询器");//以下设置框架的标题等属性
      this.setBounds(300,240,400,90);
      this.setBackground(Color.cyan);//设置背景颜色
      this.setLayout(new GridLayout(2,3));//框架网络布局,2行3列
      //以下在框架窗口上添加两行组件,每一行各有一个标签,文本行,按钮组件
      this.add(new Label("字符",Label.RIGHT));//添加标签组件(右对齐)
      this.text_char=new TextField("汉字",10);//文本行组件
      this.add(this.text_char);
      this.button_char=new Button("查询Unicode码");//按钮组件
      this.add(this.button_char);
      this.button_char.addActionListener(this);//按钮注册动作事件监听器,委托this对象处理事件
      //以下再添加一行标签,文本行,按钮组件
      this.add(new Label("Unicode编码",Label.RIGHT));
      this.add(this.text_uni = new TextField(10));
      this.add(this.button_uni = new Button("查询字符"));
      this.button_uni.addActionListener(this);//按钮注册动作事件监听器,委托this对象处理事件
      this.setVisible(true);//显示框架窗口
      this.addWindowListener(new WinClose());//框架注册窗口事件监听器,委托WinClose对象处理事件
  }
   //动作事件处理方法,实现ActionListener接口。
   //单击按钮和文本行时触发执行,even.getSource() 返回单击的那个按钮(事件源组件)
   public void actionPerformed(ActionEvent event){
      if(event.getSource() == this.button_char) {//单击”查询Unicode码“按钮,比较引用
          String str = this.text_char.getText();//获得文本行字符串
          if(!str.isEmpty()){//输入非空串执行操作,功能同 !str.equals(""),不能用str !=""
            char ch = str.charAt(0);//获得首字符
            this.text_char.setText(""+ch);//重新设置文本,显示字符
            this.text_uni.setText(""+(int)ch);//显示ch的Unicode码
          }
      }
      else if(event.getSource() == this.button_uni){//单击”查询字符”按钮
          String str = this.text_uni.getText();//获得文本行字符串
          if(!str.isEmpty()){
              //将str字符串转换成整数,未处理异常;再显示为该整数的Unicode 编码的字符
              this.text_char.setText(""+(char)Integer.parseInt(str));
          }
      }
   }

    public static void main(String[] args) {
        new QueryFrame();

    }
}

WinClose类(框架注册窗口事件监听器,委托WinClose对象处理事件)

import 第六章.事件处理.事件类和事件监听器接口.WindowListener;
import java.awt.event.*;
import java.awt.event.WindowEvent;
//WinClose.java,实现窗口事件监听器接口的类,关闭窗口时,结束程序运行
public class WinClose implements WindowListener, java.awt.event.WindowListener {
    public void windowClosing(WindowEvent event) {//关闭窗口时执行的事件处理方法
        System.exit(0);//结束程序运行
    }
    public void windowOpened(WindowEvent event){}//打开窗口后执行
    public void windowActivated(WindowEvent event){}//激活窗口执行
    public void windowDeactivated(WindowEvent event){}//变为不活动窗口后执行
    public void windowIconifed(WindowEvent event){}//窗口最小化后执行
    public void windowDeiconified(WindowEvent event){}//窗口恢复后执行
    public void windowClosed(WindowEvent event){}//关闭窗口后执行

    @Override
    public void windowIconified(WindowEvent e) {

    }
}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汇潮学堂

你的鼓励才是我的最大收获

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值