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; 
    } 
}