600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Java 图形化界面 实现ASCII码的转换和查看

Java 图形化界面 实现ASCII码的转换和查看

时间:2023-04-03 15:42:45

相关推荐

Java 图形化界面 实现ASCII码的转换和查看

实现效果如下图:

直接上代码:

package com.wk;import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.UIManager;import javax.swing.border.EmptyBorder;import javax.swing.border.EtchedBorder;public class ASCIIViewer extends JFrame{private static final long serialVersionUID = -6067423561196663639L;private JPanel contentPane;private JTextField asciiTextField;private JTextField numberTextField;private JLabel label3;private JLabel label6;public static void main(String[] args){try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Throwable e){e.printStackTrace();}EventQueue.invokeLater(new Runnable(){public void run(){try{ASCIIViewer frame = new ASCIIViewer();frame.setVisible(true);}catch(Exception e){e.printStackTrace();}}});}public ASCIIViewer(){setTitle("ASCII编码查看器");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,450,150);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5,5,5,5));contentPane.setLayout(new BorderLayout(0,0));setContentPane(contentPane);JPanel panel = new JPanel();contentPane.add(panel,BorderLayout.CENTER);panel.setLayout(new GridLayout(2,1,5,5));//the first lineJPanel asciiPanel = new JPanel();asciiPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED,null,null));panel.add(asciiPanel);asciiPanel.setLayout(new GridLayout(1,5,5,5));JLabel label1 = new JLabel("输入字符");label1.setFont(new Font("宋体",Font.ITALIC,16));asciiPanel.add(label1);asciiTextField = new JTextField();asciiPanel.add(asciiTextField);asciiTextField.setColumns(3);JLabel label2 = new JLabel("转换结果");asciiPanel.add(label2);label3 = new JLabel("");asciiPanel.add(label3);JButton toNumberButton = new JButton("转换");toNumberButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){do_toNumberButton_actionPerformed(e);}});asciiPanel.add(toNumberButton);//the second lineJPanel numberPanel = new JPanel();numberPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED,null,null));panel.add(numberPanel);numberPanel.setLayout(new GridLayout(1,5,5,5));JLabel label4 = new JLabel("输入数字");label4.setFont(new Font("宋体",Font.BOLD,16));numberPanel.add(label4);numberTextField = new JTextField();numberPanel.add(numberTextField);numberTextField.setColumns(3);JLabel label5 = new JLabel("转换结果");numberPanel.add(label5);label6 = new JLabel("");numberPanel.add(label6);JButton toAsciiButton = new JButton("转换");toAsciiButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){do_toAsciiButton_actionPerformed(e);}});numberPanel.add(toAsciiButton);}protected void do_toNumberButton_actionPerformed(ActionEvent e){String ascii = asciiTextField.getText();int i = Character.codePointAt(ascii,0);label3.setText(""+i);}protected void do_toAsciiButton_actionPerformed(ActionEvent e){String number = numberTextField.getText();char[] a = Character.toChars(Integer.parseInt(number));label6.setText(new String(a));}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。