A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 吴清源 中级黑马   /  2013-10-8 20:33  /  1059 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

将获取到的字符转换为ASCII码
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import java.awt.Font;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;

  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JPanel;
  11. import javax.swing.JTextField;
  12. import javax.swing.UIManager;
  13. import javax.swing.border.EmptyBorder;
  14. import javax.swing.border.EtchedBorder;

  15. public class ASCIIViewer extends JFrame {
  16.    
  17.     /**
  18.      *
  19.      */
  20.     private static final long serialVersionUID = -6067423561196663639L;
  21.     private JPanel contentPane;
  22.     private JTextField asciiTextField;
  23.     private JTextField numberTextField;
  24.     private JLabel label3;
  25.     private JLabel label6;
  26.    
  27.     /**
  28.      * Launch the application.
  29.      */
  30.     public static void main(String[] args) {
  31.         try {
  32.             UIManager
  33.                     .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  34.         } catch (Throwable e) {
  35.             e.printStackTrace();
  36.         }
  37.         EventQueue.invokeLater(new Runnable() {
  38.             public void run() {
  39.                 try {
  40.                     ASCIIViewer frame = new ASCIIViewer();
  41.                     frame.setVisible(true);
  42.                 } catch (Exception e) {
  43.                     e.printStackTrace();
  44.                 }
  45.             }
  46.         });
  47.     }
  48.    
  49.     /**
  50.      * Create the frame.
  51.      */
  52.     public ASCIIViewer() {
  53.         setTitle("ASCII\u7F16\u7801\u67E5\u770B\u5668");
  54.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.         setBounds(100, 100, 450, 150);
  56.         contentPane = new JPanel();
  57.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  58.         contentPane.setLayout(new BorderLayout(0, 0));
  59.         setContentPane(contentPane);
  60.         
  61.         JPanel panel = new JPanel();
  62.         contentPane.add(panel, BorderLayout.CENTER);
  63.         panel.setLayout(new GridLayout(2, 1, 5, 5));
  64.         
  65.         JPanel asciiPanel = new JPanel();
  66.         asciiPanel
  67.                 .setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
  68.         panel.add(asciiPanel);
  69.         asciiPanel.setLayout(new GridLayout(1, 5, 5, 5));
  70.         
  71.         JLabel label1 = new JLabel("\u8F93\u5165\u5B57\u7B26\uFF1A");
  72.         label1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  73.         asciiPanel.add(label1);
  74.         
  75.         asciiTextField = new JTextField();
  76.         asciiTextField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  77.         asciiPanel.add(asciiTextField);
  78.         asciiTextField.setColumns(3);
  79.         
  80.         JLabel label2 = new JLabel("\u8F6C\u6362\u7ED3\u679C\uFF1A");
  81.         label2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  82.         asciiPanel.add(label2);
  83.         
  84.         label3 = new JLabel("");
  85.         label3.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  86.         asciiPanel.add(label3);
  87.         
  88.         JButton toNumberButton = new JButton("\u8F6C\u6362");
  89.         toNumberButton.addActionListener(new ActionListener() {
  90.             public void actionPerformed(ActionEvent e) {
  91.                 do_toNumberButton_actionPerformed(e);
  92.             }
  93.         });
  94.         toNumberButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  95.         asciiPanel.add(toNumberButton);
  96.         
  97.         JPanel numberPanel = new JPanel();
  98.         numberPanel
  99.                 .setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
  100.         panel.add(numberPanel);
  101.         numberPanel.setLayout(new GridLayout(1, 5, 5, 5));
  102.         
  103.         JLabel label4 = new JLabel("\u8F93\u5165\u6570\u5B57\uFF1A");
  104.         label4.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  105.         numberPanel.add(label4);
  106.         
  107.         numberTextField = new JTextField();
  108.         numberTextField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  109.         numberPanel.add(numberTextField);
  110.         numberTextField.setColumns(3);
  111.         
  112.         JLabel label5 = new JLabel("\u8F6C\u6362\u7ED3\u679C\uFF1A");
  113.         label5.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  114.         numberPanel.add(label5);
  115.         
  116.         label6 = new JLabel("");
  117.         label6.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  118.         numberPanel.add(label6);
  119.         
  120.         JButton toASCIIButton = new JButton("\u8F6C\u6362");
  121.         toASCIIButton.addActionListener(new ActionListener() {
  122.             public void actionPerformed(ActionEvent e) {
  123.                 do_toASCIIButton_actionPerformed(e);
  124.             }
  125.         });
  126.         toASCIIButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  127.         numberPanel.add(toASCIIButton);
  128.     }
  129.    
  130.     protected void do_toNumberButton_actionPerformed(ActionEvent e) {
  131.         String ascii = asciiTextField.getText(); // 获取输入字符
  132.         int i = Character.codePointAt(ascii, 0); // 把字符转换为编码
  133.         label3.setText("" + i); // 将编码值显示到窗体标签控件中
  134.     }
  135.    
  136.     protected void do_toASCIIButton_actionPerformed(ActionEvent e) {
  137.         String number = numberTextField.getText(); // 获取输入数字
  138.         char[] a = Character.toChars(Integer.parseInt(number)); // 把指定的编码转换为字符
  139.         label6.setText(new String(a)); // 显示转换后的字符到窗体标签控件中
  140.     }
  141. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
EYE_SEE_YOU + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马