import java.awt.flowlayout;import java.awt.gridlayout;import Javax.swing.jbutton;import Javax.swing.jdialog;import Javax.swing.jframe;import Javax.swing.jlabel;import Javax.swing.JTextField;import Javax.swing.event.caretevent;import Javax.swing.event.caretlistener;public class Moneyjframe extends JFrame Implements Caretlistener{private JTextField textmoney,textstr;private messagejdialog jdialog;public MoneyJFrame () { Super ("Chinese capitalization of Amount"); This.setsize (This.setlocationrelativeto);//Set Form center this.setresizable (false);// Setting the form size cannot be changed this.setbackground (Java.awt.Color.lightGray); this.setdefaultcloseoperation (exit_on_close);// When you click Close button. End program Execution This.getcontentpane (). setlayout (New FlowLayout (Flowlayout.right)); This.getcontentpane (). Add ("Amount "); Textmoney=new JTextField (" 12345678.90 ",This.getcontentpane (). Add (Textmoney); Textmoney.addcaretlistener (this);//Register text edit event. That is, the text editor responds with a change in response to This.getcontentpane (). Add (New JLabel ("Chinese capitalization")); textstr=new JTextField (22); Textstr.sethorizontalalignment (jtextfield.right); textstr.seteditable (false); This.getcontentpane (). Add (TEXTSTR) ; caretupdate (null);//Perform text-editing events equivalent to initialize this.setvisible (true); jdialog=new Messagejdialog ();//Create Dialog Object}private class Messagejdialog extends jdialog{//message dialog box, private instance inner class, object nested private JLabel jlabel;public Messagejdialog () {//Inner class construction method super ( Moneyjframe.this, "hint", true);//moneyjframe.this refers to the current object of the external class (that is, the frame form to which the dialog box is attached) true. Modal forms (that is, only the dialog form is closed or the ability to manipulate the attached frame form) this.setsize (//this.setlayout), the new GridLayout (2,1), Jlabel=new JLabel ("" , jlabel.center); This.getcontentpane (). Add (JLabel); this.setdefaultcloseoperation (hide_on_close);//this.add (new JButton ("OK"));} private void Show (String message) {//dialog box display jlabel.settext (message); This.setlocation (MoneyJFrame.this.getX () +100, MoneyJFrame.this.getY () +100);//dialog box position under Frame this.setvisible (true);}}public void Caretupdate (Caretevent e) {//Text Edit event handling method try {Double x=double.parsedouble (Textmoney.gettext ()); Textstr.settext (rmbtostring (x));} catch (NumberFormatException E1) {jdialog.show ("\" "+textmoney.gettext () +" \ "cannot be converted to floating point numbers, please enter again!) ");}} private string rmbtostring (double x) {//Converts the amount represented by X to the Chinese uppercase form String yuan= "million thousand hundred million pick yuan angle points"; String digit= "0 One and three Woolu qi Ba Jiu"; String result= ""; int y= (int) (x*100);//floating-point number is expanded 100 times times after rounding (two valid digits reserved) for (int i=yuan.length () -1;y>0&&i>0;i--, y/=10) {result= "" +digit.charat (y%10) +yuan.charat (i) +result;} return result;} public static void Main (string[] args) {new Moneyjframe ();}}
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Java It------graphical interface (two)