实现十进制二进制八进制十六进制的相互转换 (Java经典编程案例)

计算机内部使用了二进制数据,为了节约空间,又定义了八进制和十六进制来表示二进制数据。对于普通人来说,十进制更加容易阅读,本例子将实现一个进制转换器。

package mytest;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.UIManager;
import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class Example extends JFrame {
    
    private JPanel contentPane;
    private JTextField textField;
    private String number;
    
    public static void main(String[] args) {
      
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Example frame = new Example();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    public Example() {
        setTitle("\u8FDB\u5236\u8F6C\u6362\u5DE5\u5177");//进制转换工具
        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));

        JPanel panel1 = new JPanel();
        panel1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        panel.add(panel1);
        panel1.setLayout(new GridLayout(1, 2, 5, 5));

        JLabel label = new JLabel("\u8BF7\u8F93\u5165\u8981\u8F6C\u6362\u7684\u6570\u636E\uFF1A");
        label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        panel1.add(label);

        textField = new JTextField();
        textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                do_textField_focusLost(e);
            }
        });
        textField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        panel1.add(textField);
        textField.setColumns(10);

        JPanel panel2 = new JPanel();
        panel2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        panel.add(panel2);

        JRadioButton binaryRadioButton = new JRadioButton("\u4E8C\u8FDB\u5236");
        binaryRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_binaryRadioButton_actionPerformed(e);
            }
        });
        binaryRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        panel2.add(binaryRadioButton);

        JRadioButton octalRadioButton = new JRadioButton("\u516B\u8FDB\u5236");
        octalRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_octalRadioButton_actionPerformed(e);
            }
        });
        octalRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        panel2.add(octalRadioButton);

        JRadioButton decimalRadioButton = new JRadioButton("\u5341\u8FDB\u5236");
        decimalRadioButton.setSelected(true);
        decimalRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_decimalRadioButton_actionPerformed(e);
            }
        });
        decimalRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        panel2.add(decimalRadioButton);

        JRadioButton hexRadioButton = new JRadioButton("\u5341\u516D\u8FDB\u5236");
        hexRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_hexRadioButton_actionPerformed(e);
            }
        });
        hexRadioButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        panel2.add(hexRadioButton);

        ButtonGroup group = new ButtonGroup();
        group.add(binaryRadioButton);
        group.add(octalRadioButton);
        group.add(decimalRadioButton);
        group.add(hexRadioButton);
    }

    protected void do_binaryRadioButton_actionPerformed(ActionEvent e) {
        textField.setText(Integer.toBinaryString(Integer.parseInt(number)));
    }

    protected void do_octalRadioButton_actionPerformed(ActionEvent e) {
        textField.setText(Integer.toOctalString(Integer.parseInt(number)));
    }

    protected void do_decimalRadioButton_actionPerformed(ActionEvent e) {
        textField.setText(number);
    }

    protected void do_hexRadioButton_actionPerformed(ActionEvent e) {
        textField.setText(Integer.toHexString(Integer.parseInt(number)));
    }

    protected void do_textField_focusLost(FocusEvent e) {
        number = textField.getText();
    }
}

执行结果如下图所示:
实现十进制二进制八进制十六进制的相互转换 (Java经典编程案例)_第1张图片

你可能感兴趣的:(Java经典编程案例)