JAVA 簡單進制轉換器


亂搞的..

JAVA作業寫了個進制轉換器...寫出來的東西還蠻怪的

/*
 * Author:graykido
 * Coding:GBK
 * */


package Caculater;

import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;

import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class caculater extends JFrame {
    ImageIcon icon;
    JLabel label;
    //輸入文本框
    JTextField text;
    //數字按鈕
    JPanel numPanl;
    JButton numButton[];

    //輸入和輸出進制的框
    JPanel buttonPanel;

    //輸出進制單選框
    JLabel outbuttonlabel;
    JPanel outbuttonPanel;
    ButtonGroup obtg;

    //輸入進制單選框
    JLabel inbuttonlabel;
    JPanel inbuttonPanel;
    ButtonGroup ibtg;

    //將文本框和進制框放在一個jpanel內
    JPanel thPanel;

    //當前輸入進制
    int IHex;


    caculater(String title) {
        super(title);
        //默認輸入是十進制
        this.IHex = 10;
        Container ctpn = getContentPane();
        //設置布局
        GridLayout layout = new GridLayout(2, 2);
        this.setLayout(layout);

        //設置文本框
        text = new JTextField();
        text.setBorder(BorderFactory.createRaisedBevelBorder());
        text.setFont(new Font("宋體", Font.BOLD, 25));

        //設置標題圖標
        this.icon = new ImageIcon("src\\Caculater\\go.png");
        icon.setDescription("gogogo");
        Image img = this.icon.getImage();
        this.setIconImage(img);
        this.setLocation(100, 200);
        this.setSize(1000, 600);
        //設置菜單
        JMenuBar jmb = new JMenuBar();

        JMenu edit = new JMenu("編輯");
        JMenuItem edit1 = new JMenuItem("施工中");
        edit.add(edit1);
        jmb.add(edit);

        JMenu view = new JMenu("查看");
        JMenuItem view1 = new JMenuItem("施工中");
        view.add(view1);
        jmb.add(view);

        JMenu help = new JMenu("幫助");
        JMenuItem help1 = new JMenuItem("施工中");
        help.add(help1);
        jmb.add(help);

        this.setJMenuBar(jmb);

        //圖標
        ImageIcon icon1 = new ImageIcon("src/Caculater/left.gif");
        ImageIcon icon2 = new ImageIcon("src/Caculater/right.gif");

        buttonPanel = new JPanel();

        //輸出進制單選框
        outbuttonPanel = new JPanel();
        obtg = new ButtonGroup();
        JLabel outL = new JLabel("輸出進制", icon1, JLabel.CENTER);
        outbuttonPanel.add(outL);
        outaddRadioButton("二進制", 2);
        outaddRadioButton("八進制", 8);
        outaddRadioButton("十進制", 10);
        outaddRadioButton("十六進制", 16);

        //輸入進制單選框
        inbuttonPanel = new JPanel();
        ibtg = new ButtonGroup();
        JLabel inL = new JLabel("輸入進制", icon2, JLabel.CENTER);
        inbuttonPanel.add(inL);
        inaddRadioButton("二進制", 2);
        inaddRadioButton("八進制", 8);
        inaddRadioButton("十進制", 10);


        //數字按鈕
        this.numPanl = new JPanel();
        this.numButton = new JButton[20];
        for (int i = 0; i < 10; i++) {
            numButton[i] = addJButton(i);
            numPanl.add(numButton[i]);
        }


        //清空文本框按鈕
        JButton clr = new JButton("clr");
        clr.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                text.setText("");
            }
        });   //對於數字按鈕以及清空按鈕排版
        numPanl.add(clr);
        numPanl.setLayout(new GridLayout(4, 4));


        buttonPanel.setLayout(new GridLayout(2, 1));
        buttonPanel.add(outbuttonPanel);
        buttonPanel.add(inbuttonPanel);
        thPanel = new JPanel(new GridLayout(1, 2));
        thPanel.add(text);
        thPanel.add(buttonPanel);


//        ctpn.add(text);
        ctpn.add(thPanel);
//        ctpn.add(outbuttonPanel);
//        ctpn.add(inbuttonPanel);
        ctpn.add(numPanl);


        //關閉進程
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setVisible(true);
    }

    public JButton addJButton(int num) {
        JButton button = new JButton("" + num);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                try {
                    Integer.parseInt(text.getText());
                } catch (Exception e) {
                    text.setText("");
                }
                text.setText(text.getText() + num);
                validate();
            }
        });
        return button;
    }

    public void outaddRadioButton(String title, int Hex) {
        JRadioButton button = new JRadioButton(title);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                //觸發進制轉換
                try {
                    long num = Long.parseLong(text.getText());
                    //先將框內內容轉為十進制
                    long temp = num;
                    long mut = 1;
                    long res = 0;
                    if (IHex != 10) {
                        while (temp != 0) {
                            res += (temp % 10 * mut);
                            mut *= IHex;
                            temp /= 10;
                        }
                    } else {
                        res = num;
                    }

                    num = res;
                    String ans = "";
                    while (num != 0) {
//                        ans += String.valueOf(num % Hex < 10 ? num % Hex : num % Hex - 10 + 'A');
                        ans += (char) (num % Hex < 10 ? num % Hex + '0' : num % Hex - 10 + 'A');
                        num /= Hex;

                    }
                    text.setText(new StringBuffer(ans).reverse().toString());
                } catch (NumberFormatException e) {
                    text.setText("請輸入數字或檢查輸入數字范圍是否過大");
                } catch (Exception e) {
                    text.setText("" + e);
                }

            }
        });
        obtg.add(button);
        outbuttonPanel.add(button);
    }

    public void inaddRadioButton(String title, int Hex) {
        JRadioButton button = new JRadioButton(title);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                IHex = Hex;
                for (int i = 0; i < 10; i++) {
                    numButton[i].setEnabled(true);
                }
                for (int i = Hex; i < 10; i++) {
                    numButton[i].setEnabled(false);
                }
            }
        });
        inbuttonPanel.add(button);
        ibtg.add(button);
    }


    public static void main(String[] args) {
        caculater fr = new caculater("計算器");
    }
}

一個小小的update

結果因為把IDEA突然弄崩了鼓搗了一晚上導致心態炸裂

/*
 * Author:graykido
 * Coding:GBK
 * */

package Caculater;

import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;

import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.synth.SynthEditorPaneUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class caculater extends JFrame {
    ImageIcon icon;
    JLabel label;
    //輸入文本框
    JTextField text;
    //數字按鈕
    JPanel numPanl;
    JButton numButton[];

    //進制和數字框
    JPanel buttonPanel;

    //進制
    JLabel inbuttonlabel;
    JPanel inbuttonPanel;
    ButtonGroup ibtg;


    //當前進制
    int IHex;


    caculater(String title) {
        super(title);
        //默認輸入是十進制
        this.IHex = 10;
        Container ctpn = getContentPane();
        //設置布局
        this.setLayout(new BorderLayout());


        //設置文本框
        text = new JTextField();
        text.setBorder(BorderFactory.createRaisedBevelBorder());
        text.setFont(new Font("宋體", Font.BOLD, 25));
        ctpn.add("North", text);

        //設置標題圖標
        this.icon = new ImageIcon("src\\Caculater\\go.png");
        icon.setDescription("gogogo");
        Image img = this.icon.getImage();
        this.setIconImage(img);
        this.setLocation(100, 200);
        this.setSize(400, 500);
        //設置菜單
        JMenuBar jmb = new JMenuBar();

        JMenu edit = new JMenu("編輯");
        JMenuItem edit1 = new JMenuItem("施工中");
        edit.add(edit1);
        jmb.add(edit);

        JMenu view = new JMenu("查看");
        JMenuItem view1 = new JMenuItem("施工中");
        view.add(view1);
        jmb.add(view);

        JMenu help = new JMenu("幫助");
        JMenuItem help1 = new JMenuItem("About Me");
        help1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                JOptionPane.showMessageDialog(null, "graykido");
            }
        });
        help.add(help1);
        jmb.add(help);

        this.setJMenuBar(jmb);

        //圖標
        ImageIcon icon1 = new ImageIcon("src/Caculater/left.gif");
        ImageIcon icon2 = new ImageIcon("src/Caculater/right.gif");


        buttonPanel = new JPanel(new BorderLayout());

        //進制單選框
        inbuttonPanel = new JPanel();
        ibtg = new ButtonGroup();
        JLabel inL = new JLabel("進制", icon2, JLabel.CENTER);
        inbuttonPanel.add(inL);
        inaddRadioButton("二進制", 2);
        inaddRadioButton("八進制", 8);
        inaddRadioButton("十進制", 10);
        inaddRadioButton("十六進制", 16);
        buttonPanel.add("North", inbuttonPanel);

        //輸入進制單選框
        this.numPanl = new JPanel(new GridLayout(5, 5));
        this.numButton = new JButton[20];
        for (int i = 0; i < 16; i++) {
            numButton[i] = addJButton(i);
            numPanl.add(numButton[i]);
        }

        //清空文本框按鈕
        JButton clr = new JButton("clr");
        clr.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                text.setText("");
            }
        });

        JButton numadd = new JButton("+");
        numadd.setEnabled(false);
        JButton numsub = new JButton("-");
        numsub.setEnabled(false);
        JButton nummul = new JButton("*");
        nummul.setEnabled(false);
        JButton numdiv = new JButton("/");
        numdiv.setEnabled(false);
        JButton numeql = new JButton("=");
        numeql.setEnabled(false);
        numPanl.add(clr);
        numPanl.add(numdiv);
        numPanl.add(nummul);
        numPanl.add(numsub);
        numPanl.add(numadd);
        numPanl.add(numeql);

        buttonPanel.setPreferredSize(new Dimension(200, 200));
        buttonPanel.add("Center", this.numPanl);
        ctpn.add("Center", buttonPanel);

        //關閉進程
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setVisible(true);
    }

    public JButton addJButton(int num) {
        JButton button;
        if (num >= 10) {
            button = new JButton("" + (char) ((byte) 'A' + num - 10));
        } else {
            button = new JButton("" + num);
        }
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                text.setText(text.getText() + button.getText());
                validate();
            }
        });
        return button;
    }

    public void inaddRadioButton(String title, int Hex) {
        JRadioButton button = new JRadioButton(title);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    String Txt = text.getText();
                    long temp = 0;
                    long mut = 1;
                    long res = 0;
                    for (int i = Txt.length() - 1; i >= 0; i--) {
                        if (Character.isUpperCase(Txt.charAt(i))) {
                            temp += ((byte) Txt.charAt(i) - (byte) 'A' + 10) * mut;
                        } else {
                            temp += ((byte) (Txt.charAt(i)) - (byte) ('0')) * mut;
                        }
                        mut *= IHex;
                    }
                    res = temp;
                    String ans = "";
                    while (res != 0) {
//                        ans += String.valueOf(res % Hex < 10 ? res % Hex : res % Hex - 10 + 'A');
                        ans += (char) (res % Hex < 10 ? res % Hex + '0' : res % Hex - 10 + 'A');
                        res /= Hex;

                    }
                    text.setText(new StringBuffer(ans).reverse().toString());
                    IHex = Hex;
                    for (int i = 0; i < 16; i++) {
                        numButton[i].setEnabled(true);
                    }
                    for (int i = Hex; i < 16; i++) {
                        numButton[i].setEnabled(false);
                    }
                } catch (NumberFormatException e) {
                    text.setText("請輸入數字或檢查輸入數字范圍是否過大");
                } catch (Exception e) {
                    text.setText("" + e);
                }
            }
        });
        inbuttonPanel.add(button);
        ibtg.add(button);
    }


    public static void main(String[] args) {
        caculater fr = new caculater("計算器");
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM