Java uses JFrame listener events to create a form program and button shortcut keys that copy text functionality

Source: Internet
Author: User

I. BASIC OBJECTIVES

Design a form program, there are two text boxes, where the second text box is non-editable, there are three buttons, click the Copy button to copy the first text box to the second text box, click the Clear button to clear the contents of two text boxes, you can also use shortcut keys Alt+r and alt+y operation


Click the Close button to close the form program



Second, the basic idea

The layout of this jframe with each component name is as follows, please combine the following code to view:



Third, the production process

Take a look at the following code:

Import Java.awt.*;import Java.awt.event.*;import javax.swing.*;//because this form has a trigger event, you must use the ActionListener interface class textbox Implements actionlistener{//member variable, one jframe, two JLabel, two JTextField, three JButton, three jpaneljframe F; JLabel L1,l2; JTextField T1,t2; JButton b1,b2,b3; JPanel p1,p2,p;public static void Main (String args[]) {textbox a=new textbox (); A.go ();} public void Go () {//Interface title F=new JFrame ("textbox");//create 2 fixed text, one called source one called Targetl1=new JLabel ("source"); L2=new JLabel (" Target ");//create 2 text boxes T1=new JTextField (); t2=new JTextField ();//t2 text box is non-editable t2.setenabled (FALSE);//create 3 buttons, The parameters inside are the text b1=new JButton ("Clear"), B2=new JButton ("Copy"), B3=new JButton ("Close"), and//create Shortcut b1,b2 and alt+r for Alt+y, The general shortcut key will only underline B1.setmnemonic (Keyevent.vk_r) in the Plain English button, B2.setmnemonic (keyevent.vk_y),//b1,b2,b3 triggered event, Corresponding to the clear event Copy event Close event actionperformed below B1.setactioncommand ("clear"); B2.setactioncommand ("Copy"); B3.setactioncommand ("Close"), B1.addactionlistener (This), B2.addactionlistener (This), B3.addactionlistener (this) ;//Create panel p=new JPanel ();p 1=new JPanel ();p 2=new JPanel ();//Set the periphery of the largest panel p layout is borderlayout (), this layout can check the information, not here to repeat P.setlayout (new BorderLayout ());//Put the parts separately P1 , P2p1.add (L1);p 1.add (T1);p 1.add (L2);p 1.add (T2);p 2.add (B1);p 2.add (B2);p 2.add (B3);// Put P1 on the CENTER position of the BorderLayout in P, P2 is the south position p.add (P1,borderlayout.center);p. Add (P2,borderlayout.south);// P1 uses the gridlayout2x2 layout, P2 uses 1x3, just put all the components P1.setlayout (new GridLayout (2,2));p 2.setLayout (new GridLayout (1,3)); F.getcontentpane (). Add (P);//Window size is 320x100, do not allow the user to resize themselves, visible, the default is not visible f.setsize (320,100); f.setvisible (true); F.setresizable (false); f.setdefaultcloseoperation (jframe.exit_on_close);} The button trigger time function specifies to write the public void actionperformed (ActionEvent e) {if (E.getactioncommand () = = "clear") {//If the time triggered is clear, Then empty the t1,t2 text T1.settext (""); T2.settext ("");} if (e.getactioncommand () = = "Copy") {//If the trigger time is copy, then the T2 text is the T1 text T2.settext (T1.gettext ());} if (e.getactioncommand () = = "Close") {//If the trigger time is close, exit the program System.exit (0);}}

Among them, compared to the "Java" JFrame Helloworld (click to open the link) in the article of the program, this article has been the painting interface code in the text class encapsulated in the Go () method, more portability

Java uses JFrame listener events to create a form program and button shortcut keys that copy text functionality

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.