Actionevent event on the third bullet _ button in Gui Programming

Source: Internet
Author: User

I learned about text boxes and buttons today. It seems that creating text boxes and buttons is not a problem, but it is very troublesome to handle events. For example, there is a "OK" button, when we click this OK button, what will happen? Not necessarily, We need to rewrite the actionlistener method of the interface. Originally we wanted to make a simple calculator, which was very easy to start, however, it may not be easy to think about it. A calculator requires at least 10 numeric buttons and some arithmetic buttons. When these buttons are triggered, there are many things to write after each button, think about it, I'm drunk .. So I decided to extract a + B first ..

Text Box


The jtextfield subclass of jcomponent is responsible for creating text boxes;


Button


The jbutton subclass of jcomponent is used to create buttons;


Please familiarize yourself with these two classes.


Source code of A + B:

Main class

public class Event {public static void main(String[] args){Wgame w=new Wgame();w.setTitle("super a+b");}}

Rewrite jframe, that is, build the window;

Import Java. AWT. *; import Java. AWT. event. *; import javax. swing. *; public class wgame extends jframe {jtextfield input1, input2; jtextfield output; jbutton result; listenresult ls; Public wgame () {Init (); setbounds (20, 30, 300,300 ); setvisible (true); setdefaclocloseoperation (jframe. exit_on_close);} void Init () {setlayout (New flowlayout (); // sets the component stream layout input1 = new jtextfield (20); input2 = new jtextfield (20 ); output = new jtextfield (20); add (input1); add (input2); Result = new jbutton ("Get the sum of two numbers"); add (result); output. seteditable (false); add (output); LS = new listenresult (); result. addactionlistener (LS); LS. seta (input1); LS. SETB (input2); LS. setoutput (output );}}

Button listener:

Import Java. AWT. event. *; import javax. swing. *; import Java. math. *; public class listenresult implements actionlistener {jtextfield A, B, C; Public void Seta (jtextfield t) {A = T;} public void SETB (jtextfield T) {B = T;} public void setoutput (jtextfield t) {c = T;} public void actionreceivmed (actionevent e) {biginteger Ta, TB; try {TA = new biginteger (. gettext (); TB = new biginteger (B. gettext (); C. settext (TA. add (TB ). tostring ();} catch (exception ee) {C. settext ("enter a number ");}}}

The effect is as follows:


Actionevent event on the third bullet _ button in Gui Programming

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.