Implement various transformations in the Java language (binary, octal, decimal, hexadecimal, mainly using the Java Swing technology) __java

Source: Internet
Author: User
Tags gettext stub

implement various transformations in the Java language (binary, octal, decimal, hexadecimal, mainly using Java Swing technology)

1, the original question

Enter a hexadecimal number from the keyboard, convert it to a decimal number, and then output. If the input is not a valid hexadecimal number, an exception is thrown.

2, Analysis

The original title is a few days before the Java teacher assigned a class homework, because now is also learning the Java console input and output operations, so according to the original problem to hand the job is very simple. I took the liberty to change it. Use GUI visualization instead, and to achieve the conversion between 2, 8, 10, and 16 in the system. The concrete implementation below has very detailed introduction. 3, UML diagram

4, the specific implementation code (with comments: a total of 6)

(1)

Package convertnumber;

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

class MainClass {//used to test public
	
	static void Main (string[] args) {/
		/TODO auto-generated method Stu b
        windownumber win = new WindowNumber ();
        Win.settitle ("Simple Conversion converter");
	}



(2)

Package convertnumber; /** * @Author Kun Sun * @Date 2013-10-26 * @At xust * @All Copyright by Kun Sun * */import Java.awt.FlowLay

Out
Import Javax.swing.Box;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;

Import Javax.swing.JTextField; The public class WindowNumber extends JFrame {box basebox,boxv1,boxv2;//Boxv1,boxv2 is a two-column box container, Basebox the two before the bloom. These three were used for layout JTextField entertwo,entereight,enterten,entersixteen;
	4 editable text areas, which are used as input 2, 8, 10, 16, and/or monitor, respectively, for monitoring the incoming system (2,8,10,16) into the system of POLICELISTEN2 Listener2;
	PoliceListen8 Listener8;
	PoliceListen10 Listener10;
	
	PoliceListen16 listener16;
		Public WindowNumber () {//constructor, initializing 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); Layout-column box container 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:"));
		Layout-Type box container 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);
		The above two containers are basebox = Box.createhorizontalbox ();
		Basebox.add (boxV1);
		Basebox.add (Box.createhorizontalstrut (10));
		Basebox.add (boxV2);
		
		Add (Basebox);
		Monitor whether the 2-in-number input listener2 = new PoliceListen2 ();
		Listener2.setjtextfield (Entereight,enterten,entersixteen);
		
		Entertwo.addactionlistener (LISTENER2);
		Monitor whether the 8-in-number input listener8 = new PoliceListen8 ();
		Listener8.setjtextfield (Entertwo,enterten,entersixteen); Entereight.addactIonlistener (Listener8);
		Monitor whether the 10-in-number input listener10 = new PoliceListen10 ();
		Listener10.setjtextfield (Entertwo,entereight,entersixteen);
		
		Enterten.addactionlistener (LISTENER10);
		Monitor whether the 16-in-number input 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.A
Ctionevent;

Import Java.awt.event.ActionListener;

Import Javax.swing.JTextField;
	
	public class PoliceListen2 implements ActionListener {JTextField entereight, enterten, entersixteen;//8 in, 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 ();
				The input is judged and the conversion between each of the inputs is (!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 ("Please enter a well-formed number of 2");

	}
		}
	}public void Setjtextfield (JTextField entereight, JTextField Enterten, JTextField entersixteen) {//initialization//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.A
Ctionevent;

Import Java.awt.event.ActionListener;

Import Javax.swing.JTextField; public class PoliceListen8 implements ActionListener {JTextField entertwo, Enterten, Entersixteen;//2, 10-in-system, 16-in-system @
		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 ();
				The input is judged and the conversion between each of the inputs is (!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 ("Please enter a well-formed number of 8"); }}} Public void Setjtextfield (JTextField entertwo, JTextField Enterten, JTextField entersixteen) {//initialization//TODO Auto-generat
		ed 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.A
Ctionevent;

Import Java.awt.event.ActionListener;

Import Javax.swing.JTextField;
	
	public class PoliceListen10 implements ActionListener {JTextField entertwo, entereight, Entersixteen;//2, 8-in-system, 16-in
		@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 ();
				The input is judged and the conversion between each of the inputs is (!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 ("Please enter a well-formed number of 10"); }
		}
	}

	public void Setjtextfield (JTextField entertwo, JTextField entereight, JTextField entersixteen) {//Initialize//TODO Au
		to-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.A
Ctionevent;

Import Java.awt.event.ActionListener;

Import Javax.swing.JTextField; public class PoliceListen16 implements ActionListener {JTextField entertwo, entereight, Enterten;//2, 8-in, 10-in-system @Ov
		Erride 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 ();
				The input is judged and the conversion between each of the inputs is (!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 ("Please enter a well-formed number of 16"); }} public VOID Setjtextfield (JTextField entertwo, JTextField entereight, JTextField enterten) {//initialization//TODO auto-generated Me
		Thod stub this.entertwo = Entertwo;
		This.entereight = Entereight;
	This.enterten = Enterten;
 }

}


5. Test Cases and Screenshots

(1) Initial state

(2) Enter 1a in the 16 decimal place and press ENTER

(3) Enter 1w in the 16 decimal place and press ENTER

(4) Enter the -1A in the 16 feed number and press ENTER

(5) test the remainder of the results similar to the above

6, the above Java code packaged into EXE executable file

on how to pack, readers can look at this blog "hands-on teach you how to put the Java code, packaged into a jar file and converted to exe executable file," link is as follows: http://blog.csdn.net/u010043538/article/details/13167099

7, Notes

If you have a better idea or suggestion, please kindly point out that it is appreciated.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.