1. 程式人生 > >Java 圖形化介面 實現ASCII碼的轉換和檢視

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 line

		JPanel 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 line

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

}