"Automation __gui Automation" __java__ Case __java Application Calculator

Source: Internet
Author: User
Tags gettext

One, the code is as follows
Package Www.woniu.gui.one;import Java.awt.color;import Java.awt.dimension;import java.awt.font;import Java.awt.point;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.JButton; Import Javax.swing.jcombobox;import javax.swing.jframe;import Javax.swing.jlabel;import Javax.swing.JPanel;import Javax.swing.jtextfield;public class Mycalc__swing extends JFrame {//define interface elements, complete initialization operation private JPanel JPanel = new JPanel ();p Rivate JLabel jlable1 = new JLabel ("operand one");p rivate JLabel jlable2 = new JLabel ("operand two");p rivate JLabel jlable3 = new JLabel ("Operation type");p rivate JLabel jlable4 = new JLabel ("calculation result");p rivate JButton btncalc = new JButton ("calculation");p rivate JButton btnclose = new JButton ("Off");//define operand one, operand two private jtextfield Txtnumberx = new JTextField ("1");p rivate jtextfield txtnumbery = new JTextField ("2");//define drop-down box: Add, subtract, multiply, except private jcombobox<string> Calctype = new jcombobox<string> ();p rivate JLabel Lblresult = new JLabel ("The results are shown here ....) S ");p ublic static void main(string[] args) {//TODO auto-generated method stubmycalc__swing calc = new mycalc__swing (); Calc.windowinit (); Calc.buttoninvoke ();} Window initialization operation, setting element size, position, initial value private void Windowinit () {//TODO auto-generated method stub// Window Basic Properties This.setdefaultcloseoperation (Jframe.exit_on_close); this.setvisible (true); This.setsize (420, 280); This.settitle ("Java demo program");//control Properties, window position jlable1.setsize (new Dimension), Jlable1.setfont (New Font ("Dialog", Font.Bold), jlable1.setlocation (new Point), Jlable2.setsize (new Dimension); Jlable2.setlocation (n. New Point (Jlable3.setsize); (new Dimension); Jlable3.setlocation (new Point (108)); Jlable4.setsize ( New Dimension), Jlable4.setlocation (new Point (148)), Txtnumberx.setlocation (new Point (162, 41)); Txtnumberx.setname ("Txtnumberx"); Txtnumberx.setsize (new Dimension); Txtnumbery.setlocation (new Point (162), Txtnumbery.setname ("Txtnumbery"), Txtnumbery.setsize (New Dimension (160, 21));//Add 4 plus operator Calctype.additem to the drop-down box (" Addition ");Calctype.additem ("subtraction"), Calctype.additem ("multiplication"), Calctype.additem ("division"), Calctype.setlocation (new Point (162, 108)); Calctype.setname ("Calctype"); Calctype.setsize (new Dimension); Lblresult.setsize (new Dimension (150, 20)); Lblresult.setname ("Lblresult"); Lblresult.setbackground (Color.White); Lblresult.setlocation (new Point (162, 148)); Btncalc.setlocation (new Point), Btncalc.setname ("Btncalc"); Btncalc.setsize (New Dimension (90, 21)); Btnclose.setlocation (new Point), Btnclose.setname ("Btnclose"), Btnclose.setsize (New Dimension (80, 21));// Place the control in the container jpanel.setlayout (null); Jpanel.add (jlable1); Jpanel.add (jlable2); Jpanel.add (Jlable3); Jpanel.add (JLABLE4) ; Jpanel.add (Txtnumberx); Jpanel.add (txtnumbery); Jpanel.add (Lblresult); Jpanel.add (Btncalc); Jpanel.add (BtnClose); Jpanel.add (Calctype); This.add (JPanel);} Perform operations and close applications private void Buttoninvoke () {//TODO auto-generated method Stubbtnclose.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODOAuto-generated method Stubdoclose ();}}); Btncalc.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO Auto-generated method Stubdouble Numberx = double.parsedouble (Txtnumberx.gettext ());d ouble numbery = Double.parsedouble (Txtnumbery.gettext ());d ouble result = Docalc (Numberx, numbery); Lblresult.settext (String.valueOf (result));});} Close application private void Doclose () {//TODO auto-generated method Stubsystem.exit (1);} Calculation operation public double Docalc (double x, double y) {//TODO auto-generated method Stubdouble result = 0;if (calctype.getselect Editem (). Equals ("addition")) {result= x + y;} else if (Calctype.getselecteditem () equals ("subtraction")) {result= x-y;} else if (Calctype.getselecteditem (). Equals ("multiplication")) {result= x * y;} else if (Calctype.getselecteditem (). Equals ("division")) {result= x/y;} return result;}}

"Automation __gui Automation" __java__ Case __java Application Calculator

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.