First, the purpose and requirements of the experiment
Learn and understand the container, part, Layout manager, and part event handling methods in Java swing.
Master the basic methods of programming Java GUI by writing and debugging the program.
Experimental content:
To design and write a program for converting renminbi into equivalent dollars, the interface requires that the amount of the renminbi be entered and the converted result can be obtained.
Attached: RMB exchange rate reference for program use
Buy RMB per $100 equivalent: 619.72 (as of 2015/5/23)
Second, the experimental code
Package Think;import Java.awt.color;import Java.awt.gridlayout;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.JLabel; Import Javax.swing.jpanel;import Javax.swing.jtextfield;public class JAVA_EXP_01RMB {public static void main (string[] args) {JFrame frame = new JFrame ("RMB to USD"); Frame.setresizable (false);//fixed size JPanel panel = new JPanel (); Panel.setbackground (New Color (250,250,250));p anel.setlayout (New GridLayout (5,1,10,10)); JLabel Text1 = new JLabel ("RMB:"); JLabel Text2 = new JLabel ("USD:"); JTextField rmbinput = new JTextField (""); JTextField dollaroutput = new JTextField (""); JButton BT = new JButton ("Conversion"), Frame.setbounds (0, 0, (), Anel.add);p anel.setbounds (+/--)---------------(Text1); Panel.add (rmbinput);p anel.add (text2);p anel.add (dollaroutput);p Anel.add (BT); Frame.add (panel); Frame.setvisible ( true); Bt.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent E) {//TODO auto-generated method Stubdouble temp; JButton BT = (JButton) e.getsource (); try{temp = double.parsedouble (Rmbinput.gettext ()); Dollaroutput.settext ("" +temp/6.1972);} catch (numberformatexception N) {dollaroutput.settext ("The amount you entered is wrong! "); }}});}}
third, the interface
Iv. Summary
The whole idea:
* Layout--Set up a swing top container, then set a common container jpanel, using the gridlayout layout. The layout uses five components,jpanel and JTextField , and a JButton. Then use a listener to listen for button buttons.
Problems encountered and how to resolve them:
* Initially did not set the layout to GridLayout, directly with the label default layout. Although the setbounds is set, it cannot be output in the desired position. Later Baidu See example + reading, using the GridLayout layout. It takes about an hour and 10 minutes.
* When getting the input content of TextField component, because the return value of GetText is string, it needs to be converted to double, so Baidu took a look at the example.
* The form of the output in the SetText should be noted that it is not possible to directly give a double type of data, but can be concatenated after the string output.
* Optimized the program: the input type is controlled, if the input is not a number will have an abnormal output.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Njupt Java language Integrated graphical interface programming