10进制转换二进制

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

public class Transform extends JFrame implements ActionListener{
	JLabel jlb=new JLabel("输入数字:");
	JLabel jlb1=new JLabel("二进制:");
	JLabel jlb2=new JLabel("八进制:");
	JLabel jlb3=new JLabel("十六进制:");
	JTextField jtNum=new JTextField();
	JTextField jt2=new JTextField(10);   //显示二进制数
	JTextField jt8=new JTextField(10);   //显示八进制数
	JTextField jt16=new JTextField(10);  //显示十六进制数
	JButton b1=new JButton("转化");
	public Transform(String s){
		super(s);
		this.setLayout(null);
		jlb.setBounds(new Rectangle(20,30,80,20));
		jlb1.setBounds(new Rectangle(20,70,80,20));
		jlb2.setBounds(new Rectangle(20,110,80,20));
		jlb3.setBounds(new Rectangle(20,150,80,20));
		jtNum.setBounds(new Rectangle(110,30,80,20)); //输入框属性
		jt2.setBounds(new Rectangle(110,70,150,20));  //二进制属性
		jt8.setBounds(new Rectangle(110,110,150,20)); //八进制属性
		jt16.setBounds(new Rectangle(110,150,150,20));//十六进制属性
		b1.setBounds(new Rectangle(200,30,80,20));    //按钮属性
		this.add(jlb);
		this.add(jlb1);
		this.add(jlb2);
		this.add(jlb3);
		this.add(jtNum);
		this.add(jt2);
		this.add(jt8);
		this.add(jt16);
		this.add(b1);
		b1.addActionListener((ActionListener) this);
		this.setSize(320,254);
		this.setVisible(true);
	}
	
	public static void main(String[] args){
		Transform Tf=new Transform("进制转化器");
		Tf.addWindowListener(new WindowAdapter(){
			public void windowsClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	public void actionPerformed(ActionEvent e){
		int x=10;
		String output="输入错误,请输入十进制整数";
		try{
		String XX=jtNum.getText();
		x=Integer.parseInt(XX);
		}
		catch(Exception e1)
		{
			JOptionPane.showMessageDialog(null, output);
		}
		jt2.setText(sl(2,x));
		jt8.setText(sl(8,x));
		jt16.setText(sl(16,x));
	}
	/************转化************/
	public String sl(int k,int d) 
    { 
        String x; 
        if (d < k) 
        {
        	if(k==16)
        		x = chang(d);
        	else x=new Integer(d).toString();
        } 
        else 
        { 
            int c; 
            int s = 0; 
            int n = d; 
            while (n >= k) 
            { 
                s++; 
                n = n / k; 
            } 
            String [] m = new String[s]; 
            int i = 0; 
            do 
            { 
                c = d / k; 
                if(k==16)  m[i++] = chang(d % 16);
                else m[i++] = new Integer(d%k).toString(); 
                d = c; 
            } while (c >= k); 
            x = chang(d); 
            for (int j = m.length - 1; j >= 0; j--) 
            { 
                x += m[j]; 
            } 
        }
        return x; 
    } 
	
	public String chang(int d) //change函数
    { 
        String x; 
        switch (d) 
        { 
            case 10: 
                x = "A"; 
                break; 
            case 11: 
                x = "B"; 
                break; 
            case 12: 
                x = "C"; 
                break; 
            case 13: 
                x = "D"; 
                break; 
            case 14: 
                x = "E"; 
                break; 
            case 15: 
                x = "F"; 
                break; 
            default: 
                x = new Integer(d).toString(); 
               break; 
        } 
        return x; 
    } 
}




 

你可能感兴趣的:(进制转换)