Java做一个进制转换小工具

通过swing和awt实现的一个简单的进制转换工具
可以进行数之间的进制转换
只有两个类
所有代码都放在https://github.com/13337356453/BHD_Converter
可自行下载
因为某些特殊的原因,没有把窗口弄得更漂亮(因为懒)

代码如下
BHD_Converter.java

package main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import dialog.TextDialog;

public class BHD_Converter extends JFrame{
    private final String title="进制转换";
    private final int[] size=new int[]{300,400};
    private final String[] radix=new String[]{"二进制","八进制","十进制","十六进制"};
    private Container c=getContentPane();
    private JLabel label=new JLabel("进制转换");
    private JComboBox<String> fromBox=new JComboBox<>(radix);
    private JComboBox<String> toBox=new JComboBox<>(radix);
    private JLabel inputLabel=new JLabel("Input");
    private JLabel outputLabel=new JLabel("Output");
    private JTextField inputField=new JTextField();
    private JTextField outputField=new JTextField();
    private JButton startButton=new JButton("Start");
    private JPanel panel1=new JPanel();
    private JPanel panel2=new JPanel();
    private JPanel panel3=new JPanel();
    private JPanel panel4=new JPanel();

    public BHD_Converter(){
        show_window();
        button_event();
        setLayout(new GridLayout(4,1));
        setTitle(title);
        setVisible(true);
        setResizable(false);
        setSize(size[0], size[1]);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void show_window(){
        label.setFont(new Font("微软雅黑",Font.BOLD,30));
        panel1.add(label,BorderLayout.CENTER);
        c.add(panel1);

        panel2.setLayout(null);
        fromBox.setBorder(BorderFactory.createTitledBorder("From"));
        fromBox.setFont(new Font("微软雅黑",Font.PLAIN,15));
        fromBox.setBounds(0,0,140,50);
        toBox.setBorder(BorderFactory.createTitledBorder("To"));
        toBox.setFont(new Font("微软雅黑",Font.PLAIN,15));
        toBox.setBounds(140,0,140,50);
        panel2.add(fromBox);
        panel2.add(toBox);
        c.add(panel2);

        panel3.setLayout(null);
        inputLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
        inputLabel.setBounds(10,0,100,40);
        inputField.setFont(new Font("宋体",Font.BOLD,15));
        inputField.setBounds(10,40,120,30);
        outputLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
        outputLabel.setBounds(150,0,100,40);
        outputField.setFont(new Font("宋体",Font.BOLD,15));
        outputField.setEditable(false);
        outputField.setBounds(150,40,120,30);
        panel3.add(inputLabel);
        panel3.add(outputLabel);
        panel3.add(inputField);
        panel3.add(outputField);
        c.add(panel3);

        startButton.setFocusPainted(false);
        startButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
        panel4.add(startButton,BorderLayout.CENTER);
        c.add(panel4);
    }

    private void button_event(){
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String from=(String)fromBox.getSelectedItem();
                String to=(String)toBox.getSelectedItem();
                String input=inputField.getText();
                String result;
                switch (from){
                    case "二进制":
                        try {
                            switch (to){
                                case "八进制":
                                    result=Integer.toOctalString(Integer.parseInt(input,2));
                                    outputField.setText(result);
                                    break;
                                case "十进制":
                                    result=Integer.parseInt(input,2)+"";
                                    outputField.setText(result);
                                    break;
                                case "十六进制":
                                    result=Integer.toHexString(Integer.parseInt(input,2));
                                    outputField.setText(result);
                                    break;
                                default:
                                    outputField.setText(input);
                            }
                        }
                        catch (NumberFormatException e1){
                            new TextDialog("错误","输入的不是二进制数");
                            inputField.setText("");
                        }
                        break;
                    case "八进制":
                        try{
                            switch (to){
                                case "二进制":
                                    result=Integer.toBinaryString(Integer.parseInt(input,8));
                                    outputField.setText(result);
                                    break;
                                case "十进制":
                                    result=Integer.parseInt(input,8)+"";
                                    outputField.setText(result);
                                    break;
                                case "十六进制":
                                    result=Integer.toHexString(Integer.parseInt(input,8));
                                    outputField.setText(result);
                                    break;
                                default:
                                    outputField.setText(input);
                            }
                        }
                        catch (NumberFormatException e1){
                            new TextDialog("错误","输入的不是八进制数");
                            outputField.setText("");
                        }
                        break;
                    case "十进制":
                        switch (to){
                            case "二进制":
                                result=Integer.toBinaryString(Integer.valueOf(input));
                                outputField.setText(result);
                                break;
                            case "八进制":
                                result=Integer.toOctalString(Integer.valueOf(input));
                                outputField.setText(result);
                                break;
                            case "十六进制":
                                result=Integer.toHexString(Integer.valueOf(input));
                                outputField.setText(result);
                                break;
                            default:
                                outputField.setText(input);
                        }
                        break;
                    case "十六进制":
                        try{
                            switch (to){
                                case "二进制":
                                    result=Integer.toBinaryString(Integer.parseInt(input,16));
                                    outputField.setText(result);
                                    break;
                                case "八进制":
                                    result=Integer.toOctalString(Integer.parseInt(input,16));
                                    outputField.setText(result);
                                    break;
                                case "十进制":
                                    result=Integer.parseInt(input,16)+"";
                                    outputField.setText(result);
                                    break;
                                default:
                                    outputField.setText(input);
                            }
                        }
                        catch (NumberFormatException e1){
                            new TextDialog("错误","输入的不是十六进制数");
                            outputField.setText("");
                        }
                }
            }
        });
    }


    public static void main(String[] args) {
        new BHD_Converter();
    }

}

TextDialog.java

package dialog;

import javax.swing.*;
import java.awt.*;

public class TextDialog extends JDialog {
    public TextDialog(String title,String text) {
        // TODO 自动生成的构造函数存根
        Container c=getContentPane();
        c.add(new JLabel(text),SwingConstants.CENTER);
        setTitle(title);
        setSize(100, 100);
        setVisible(true);
    }
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值