Java Interface Programming (2)------button, text input box and text area

Source: Internet
Author: User
Tags event listener gettext

This article is your own study notes, welcome reprint, but please indicate the source: http://blog.csdn.net/jesson20121020  

The previous section created the window, which is the container for the other components, and this section creates the button .

To create a button, simply call the JButton constructor where you want it to appear.

JButton is a component that has its own small window that can be automatically redrawn as part of the entire update process. That is, you do not have to show a button or other type of control, as long as you put it on the form, they can automatically draw themselves. Using the example of the previous section, the following changes are made on the basis of:

public class Swingtest extends jframe{private static JLabel label;private static Swingtest st;private JButton b1;private JButton b2;public swingtest () {super ("Hello Swing"); Setdefaultcloseoperation ( Jframe.exit_on_close); SetSize (+); setvisible (true);//Set Form layout setlayout (New FlowLayout ()); label = new JLabel ("A label);//Add button B1 = new JButton ("button 1"), b2 = new JButton ("button 2"); Add (B1); add (B2);} /** * @param args * @throws interruptedexception */public static void Main (string[] args) throws Interruptedexception {Sw Ingutilities.invokelater (New Runnable () {@Overridepublic void Run () {//TODO auto-generated Method Stubst = new Swingtest ( );}});/ *timeunit.seconds.sleep (1); Swingutilities.invokelater (New Runnable () {@Overridepublic void run () {Label.settext ("Hi,this is a Different");}); */}}
This introduces a layout manager that gives a new FlowLayout type of layout manager before adding any components to jframe. A layout manager is a tool that a panel uses to implicitly determine the position of a control on a form. The default is the Boardlayout management layout, where each added control will completely overwrite the other controls, so this does not apply here. FlowLayout allows controls to be distributed from left to right on the form, from top to bottom. The effect is as follows:


With the button, of course, there are corresponding events to be able to, so we have to add a click button below the event.

Before adding a click event, add a text input box JTextField, then create a new Listener button to click on the event's class and finally bind the listener to the button, where the event is to click the button to display the name of the button in the text input box, complete with the following code:

public class Swingtest extends jframe{private static JLabel label;private static swingtest st;private JButton b1;private J Button B2;private JTextField txt;public swingtest () {super ("Hello Swing"); Setdefaultcloseoperation (Jframe.exit_on_ CLOSE); setSize, setvisible (TRUE);//Set Form layout setlayout (New FlowLayout ());//Add Label label = new JLabel ("A label"); Add the text input box txt = new JTextField, add (TXT),//+ button B1 = new JButton ("button 1"), b2 = new JButton ("button 2"); Add (B1); add (B2);//Add Event B1.addactionlistener (new Buttonlistener ()); B2.addactionlistener (new Buttonlistener ());} /** * @param args * @throws interruptedexception */public static void Main (string[] args) throws Interruptedexception {Sw Ingutilities.invokelater (New Runnable () {@Overridepublic void Run () {//TODO auto-generated Method Stubst = new Swingtest ( );}});/ *timeunit.seconds.sleep (1); Swingutilities.invokelater (New Runnable () {@Overridepublic void run () {Label.settext ("Hi,this is a Different");}); */}class Buttonlistener implements Actionlistener{@Overridepublic void actionperformed (ActionEvent event) {//TODO auto-generated method stub// Displays the name of the button in TextField txt.settext (((JButton) Event.getsource ()). GetText ());}}}
Effects such as:


In this way, a click event is added for the button, the button is clicked, the corresponding event is triggered, and its name is displayed in the text input box. Similarly, the event listener can be written in its name's inner class, which is more convenient, which can be written as:

Private ActionListener bl = new ActionListener () {@Overridepublic void actionperformed (ActionEvent event) {//TODO Auto-ge Nerated Method Stubtxt.settext (((JButton) Event.getsource ()). GetText ());};

Here JTextField can only enter a single line of text, while the text area JTextArea can enter multiple lines of text and more functions, and there is a more common method is append (), in the Swingtest () method added:

Add text Area Txa = new JTextArea (5,10); add (TXA);
To create a 5-row, 10-column text area, to reflect the append () method, we add two buttons, click one to add a line of text, click another to clear all the text, add the following code:

Add button B1 = new JButton ("Add data") B2 = new JButton ("Clear Data"); Add (B1); add (B2); B1.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent arg0) {//TODO auto-generated method Stubtxa.append ( "This is Jtext area\n");}); B2.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent arg0) {//TODO Auto-generated method Stubtxa.settext ("");});
The effect is as follows:

As you can see, the text area can enter multiple lines of text, and of course, there are other functions.




Java Interface Programming (2)------button, text input box and text area

Related Article

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.