`
836811384
  • 浏览: 546019 次
文章分类
社区版块
存档分类
最新评论

用java语言实现各种进制转换(二进制,八进制,十进制,十六进制互转,主要使用Java Swing技术)

 
阅读更多

用java语言实现各种进制转换(二进制,八进制,十进制,十六进制互转,主要使用Java Swing技术)

1、原题

先从键盘输入一个十六进制数,再将其转化为十进制数,然后输出。若输入的不是一个有效的十六进制数,则抛出异常。

2、分析

原题是java老师前几天布置的一个课后作业,因为现在还正在学习java控制台下的输入输出操作,所以按原题交作业很简单。自己就自作主张,改了一下。改用GUI可视化实现,并且要能实现2进制数,8进制数,10进制数,16进制数之间的互相转换。具体实现下面都有很详细的介绍。

3、UML图

4、具体实现代码(附注释:共6个)

(1)

package ConvertNumber;

/**
 * @Author Kun Sun
 * @Date   2013-10-26
 * @At     XUST
 * @All Copyright by Kun Sun
 * 
 */

public class MainClass { // 用做测试
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        WindowNumber win = new WindowNumber();
        win.setTitle("简单进制转换器");
	}

}


(2)

package ConvertNumber;

/**
 * @Author Kun Sun
 * @Date   2013-10-26
 * @At     XUST
 * @All Copyright by Kun Sun
 * 
 */

import java.awt.FlowLayout;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class WindowNumber extends JFrame {
	Box baseBox,boxV1,boxV2; // boxV1,boxV2为两个列式盒式容器,baseBox盛放前两者。此三者用于布局
	JTextField enterTwo,enterEight,enterTen,enterSixteen; // 4个可编辑文本区域,依次用做输入2进制数,8进制数,10进制数,16进制数
	
	// 监视器,分别用于监听各进制(2,8,10,16)进制
	PoliceListen2 listener2;
	PoliceListen8 listener8;
	PoliceListen10 listener10;
	PoliceListen16 listener16;
	
	public WindowNumber(){ // 构造函数,初始化
		setLayout(new FlowLayout());
		init();
		setBounds(100,100,600,300);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	void init(){
		enterTwo = new JTextField(16);
		enterEight = new JTextField(16);
		enterTen = new JTextField(16);
		enterSixteen = new JTextField(16);
		
		// 布局列式盒式容器boxV1
		boxV1 = Box.createVerticalBox();
		boxV1.add(new JLabel("2进制数:"));
		boxV1.add(Box.createVerticalStrut(15));
		boxV1.add(new JLabel("8进制数:"));
		boxV1.add(Box.createVerticalStrut(15));
		boxV1.add(new JLabel("10进制数:"));
		boxV1.add(Box.createVerticalStrut(15));
		boxV1.add(new JLabel("16进制数:"));
		
		// 布局列式盒式容器boxV2
		boxV2 = Box.createVerticalBox();
		boxV2.add(enterTwo);
		boxV2.add(Box.createVerticalStrut(15));
		boxV2.add(enterEight);
		boxV2.add(Box.createVerticalStrut(15));
		boxV2.add(enterTen);
		boxV2.add(Box.createVerticalStrut(15));
		boxV2.add(enterSixteen);
		
		// 盛放上述两容器
		baseBox = Box.createHorizontalBox();
		baseBox.add(boxV1);
		baseBox.add(Box.createHorizontalStrut(10));
		baseBox.add(boxV2);
		add(baseBox);
		
		// 监视2进制数是否输入
		listener2 = new PoliceListen2();
		listener2.setJTextField(enterEight,enterTen,enterSixteen);
		enterTwo.addActionListener(listener2);
		
		// 监视8进制数是否输入
		listener8 = new PoliceListen8();
		listener8.setJTextField(enterTwo,enterTen,enterSixteen);
		enterEight.addActionListener(listener8);
		
		// 监视10进制数是否输入
		listener10 = new PoliceListen10();
		listener10.setJTextField(enterTwo,enterEight,enterSixteen);
		enterTen.addActionListener(listener10);
		
		// 监视16进制数是否输入
		listener16 = new PoliceListen16();
		listener16.setJTextField(enterTwo,enterEight,enterTen);
		enterSixteen.addActionListener(listener16);

	}
}


(3)

package ConvertNumber;

/**
 * @Author Kun Sun
 * @Date   2013-10-26
 * @At     XUST
 * @All Copyright by Kun Sun
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

public class PoliceListen2 implements ActionListener {

	JTextField enterEight, enterTen, enterSixteen; // 8进制,10进制,16进制
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		long input = 0;
		String num8 = null;
		String num10 = null;
		String num16 = null;
		
		JTextField textSource = (JTextField)e.getSource();
		String str = textSource.getText();
		
		// 判断输入,并进行各进制间的转换
		if(!str.isEmpty()){
			try{ 
				input = Long.parseLong(str,2);
				num8 = Long.toOctalString(input);
				num10 = Long.toString(input);
				num16 = Long.toHexString(input);
				
				enterEight.setText(num8);
				enterTen.setText(num10);
				enterSixteen.setText(num16);
			}catch(Exception ee){
				textSource.setText("请输入格式正确的2进制数");
			}
		}
	}

	public void setJTextField(JTextField enterEight, JTextField enterTen,
			JTextField enterSixteen) { // 初始化
		// TODO Auto-generated method stub
		this.enterEight = enterEight;
		this.enterTen = enterTen;
		this.enterSixteen = enterSixteen;
	}

}


(4)

package ConvertNumber;

/**
 * @Author Kun Sun
 * @Date   2013-10-26
 * @At     XUST
 * @All Copyright by Kun Sun
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

public class PoliceListen8 implements ActionListener {

	JTextField enterTwo, enterTen, enterSixteen; //2进制,10进制,16进制
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		long input = 0;
		String num2 = null;
		String num10 = null;
		String num16 = null;
		
		JTextField textSource = (JTextField)e.getSource();
		String str = textSource.getText();
		
		// 判断输入,并进行各进制间的转换
		if(!str.isEmpty()){
			try{
				input = Long.parseLong(str,8);
				num2 = Long.toBinaryString(input);
				num10 = Long.toString(input);
				num16 = Long.toHexString(input);
				
				enterTwo.setText(num2);
				enterTen.setText(num10);
				enterSixteen.setText(num16);
			}catch(Exception ee){
				textSource.setText("请输入格式正确的8进制数");
			}
		}
	}

	public void setJTextField(JTextField enterTwo, JTextField enterTen,
			JTextField enterSixteen) { // 初始化
		// TODO Auto-generated method stub
		this.enterTwo = enterTwo;
		this.enterTen = enterTen;
		this.enterSixteen = enterSixteen;
	}

}


(5)

package ConvertNumber;

/**
 * @Author Kun Sun
 * @Date   2013-10-26
 * @At     XUST
 * @All Copyright by Kun Sun
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

public class PoliceListen10 implements ActionListener {

	JTextField enterTwo, enterEight, enterSixteen; //2进制,8进制,16进制
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		long input = 0;
		String num2 = null;
		String num8 = null;
		String num16 = null;
		
		JTextField textSource = (JTextField)e.getSource();
		String str = textSource.getText();
		
		// 判断输入,并进行各进制间的转换
		if(!str.isEmpty()){
			try{
				input = Long.parseLong(str,10);
				num2 = Long.toBinaryString(input);
				num8 = Long.toOctalString(input);
				num16 = Long.toHexString(input);
				
				enterTwo.setText(num2);
				enterEight.setText(num8);
				enterSixteen.setText(num16);
			}catch(Exception ee){
				textSource.setText("请输入格式正确的10进制数");
			}
		}
	}

	public void setJTextField(JTextField enterTwo, JTextField enterEight,
			JTextField enterSixteen) { // 初始化
		// TODO Auto-generated method stub
		this.enterTwo = enterTwo;
		this.enterEight = enterEight;
		this.enterSixteen = enterSixteen;
	}

}


(6)

package ConvertNumber;

/**
 * @Author Kun Sun
 * @Date   2013-10-26
 * @At     XUST
 * @All Copyright by Kun Sun
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

public class PoliceListen16 implements ActionListener {

	JTextField enterTwo, enterEight, enterTen; //2进制,8进制,10进制
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		long input = 0;
		String num2 = null;
		String num8 = null;
		String num10 = null;
		
		JTextField textSource = (JTextField)e.getSource();
		String str = textSource.getText();
		
		// 判断输入,并进行各进制间的转换
		if(!str.isEmpty()){
			try{
				input = Long.parseLong(str,16);
				num2 = Long.toBinaryString(input);
				num8 = Long.toOctalString(input);
				num10 = Long.toString(input);
				
				enterTwo.setText(num2);
				enterEight.setText(num8);
				enterTen.setText(num10);
			}catch(Exception ee){
				textSource.setText("请输入格式正确的16进制数");
			}
		}
	}

	public void setJTextField(JTextField enterTwo, JTextField enterEight,
			JTextField enterTen) { // 初始化
		// TODO Auto-generated method stub
		this.enterTwo = enterTwo;
		this.enterEight = enterEight;
		this.enterTen = enterTen;
	}

}


5、测试用例及截图

(1)初始状态

(2)在16进制数处输入1a,并按回车键

(3)在16进制数处输入1w,并按回车键

(4)在16进制数处输入-1a,并按回车键

(5)测试其余进制数结果类似上述

6、把上述java代码打包成exe可执行文件

关于如何打包,读者可以看一下这篇博客《手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件》,链接如下:http://blog.csdn.net/u010043538/article/details/13167099

7、备注

如果您对更好的想法或建议,恳请指出,不胜感激。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics