十六进制到十二进制和二进制的转换程序

时间:2013-12-19 05:46:22

标签: java user-interface jframe converter

鉴于我的代码如下,我对我所缺少的内容感到困惑,以便让它能够正常运行,以便当用户输入最多五位数的十六进制值时,它们将得到它们的二进制等效值十进制当量。我认为底部的代码会自动转换为给定的十六进制数。还是难以将二进制值存储在数组中吗?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ConvertPanel {


    public static void main(String[] args) {
    JFrame frame = new JFrame ("Hexadecimal to Binary and Decimal.");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    frame.getContentPane().add(new NumberConverter());
    frame.pack();
    frame.setVisible(true);

    }
}

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class NumberConverter extends JPanel {
    private JLabel binaryLabel = new JLabel();
    private JLabel totalone = new JLabel();
    private JLabel totaltwo = new JLabel();
    private JLabel decimalLabel = new JLabel();
    private JTextField  hexdecString = new JTextField();
    private JButton convert;


    public NumberConverter() {

        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(400, 300));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JLabel converterName = new JLabel("Hexadecimal Input");


        convert = new JButton ("Convert");

        convert.addActionListener (new ButtonListener());


        JPanel panelName = new JPanel(new GridLayout(2,2));
        panelName.add(converterName);
        panelName.add(hexdecString);
        add(panelName, BorderLayout.NORTH);
        add (convert);


        JPanel totalPanel = new JPanel(new GridLayout(1,3));
        totalPanel.add(new JLabel("Binary:"));
        totalone = new JLabel("---- ---- ---- ---- ----");
        totalPanel.add(totalone);
        totalPanel.add(binaryLabel);

        JPanel totalPanel2 = new JPanel(new GridLayout(2,3));
        totalPanel2.add(new JLabel("Decimal:"));
        totaltwo = new JLabel("------");
        totalPanel2.add(totaltwo);
        totalPanel2.add(decimalLabel);

        JPanel south = new JPanel(new GridLayout(2,1));
        south.add(totalPanel);
        south.add(totalPanel2);
        add(south, BorderLayout.SOUTH);

    }        

    private class ButtonListener implements ActionListener
    {
    public void actionPerformed (ActionEvent event){


            Integer n = Integer.valueOf(hexdecString.getText(), 16);
            decimalLabel.setText(String.valueOf(n));
            binaryLabel.setText(Integer.toBinaryString(n));
        }  {

        }
    }
}

0 个答案:

没有答案