(JAVA)二进制位查看器

package Main;

import java.awt.EventQueue;

public class Main {
    public static void main(String[] args) {
        /**
         * Launch the application.
         */

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test frame = new test("二进制位查看器");
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

package Main;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

public class test extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 7483545163057953091L;
    private JPanel contentPane;
    private JTextField intField;
    private JTextField binaryField;

    /**
     * Create the frame.
     */
    public test(String title) {
        super(title);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        this.setLocation(700, 380);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 434, 262);
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel labelInt = new JLabel("请输入十进制整数:");
        labelInt.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        labelInt.setBounds(98, 69, 108, 27);
        panel.add(labelInt);

        intField = new JTextField();
        intField.setBounds(216, 72, 130, 21);
        panel.add(intField);
        intField.setColumns(10);

        JLabel labelBinary = new JLabel("该整数的二进制为:");
        labelBinary.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        labelBinary.setBounds(97, 99, 108, 27);
        panel.add(labelBinary);

        binaryField = new JTextField();
        binaryField.setColumns(10);
        binaryField.setBounds(216, 102, 130, 21);
        panel.add(binaryField);

        JButton Sure = new JButton("确定");
        Sure.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        Sure.setBounds(182, 153, 93, 23);
        panel.add(Sure);
        Sure.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String numText = intField.getText();
                try {
                    int num = Integer.parseInt(numText);// String 转 int

                    int[] a = new int[32];
                    int i;
                    for (i = 1; i < 32; i++)
                        a[i] = 0;

                    a[0] = (num < 0) ? 1 : 0;
                    num *= (num < 0) ? -1 : 1;
                    boolean T = false;
                    i = 31;
                    while (!T) {
                        a[i--] = num % 2;
                        num /= 2;
                        T = (num == 0) ? true : false;
                    }
                    int max = 0;
                    for (i = 0; i < 32; i++)
                        if (a[i] != 0) {
                            max = i;
                            break;
                        }

                    String arr = "";
                    for (i = max; i < 32; i++)
                        arr += String.valueOf(a[i]);// int 转 String

                    binaryField.setText(arr);

                } catch (NumberFormatException e1) {
                    EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            try {
                                error frame = new error();
                                frame.setVisible(true);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });

                    intField.setText("");
                }
            }

        });
    }
}


package Main;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;

public class error extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 6013400559790620619L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */

    /**
     * Create the frame.
     */
    public error() {
        super("错误!");

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 269, 114);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        this.setLocation(800, 450);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 253, 146);
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel label = new JLabel("请输入十进制整数!");
        label.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        label.setBounds(71, 30, 108, 15);
        panel.add(label);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值