Java------graphical Interface (ii)

Source: Internet
Author: User

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 window centered this.setresizable (false);// Setting the window size cannot change This.setbackground (Java.awt.Color.lightGray); this.setdefaultcloseoperation (exit_on_close);//Click the Close button The End program runs This.getcontentpane (). setlayout (New FlowLayout (Flowlayout.right)); This.getcontentpane (). Add (New JLabel ("Amount ")); Textmoney=new JTextField (" 12345678.90 ", 22);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 window to which the dialog box is attached) True, the modal window (that is, only the dialog window is closed or finished to operate on the dependent frame window) this.setsize (300, 80);/ This.setlayout (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 number, please re-enter!) ");}} 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 ();}}




Java------graphical Interface (ii)

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.