Java Swing Interface Programming (20) --- multi-line text input component: JTextArea

Source: Internet
Author: User

To enter multiple lines of text, you can use JTextArea to input multiple lines of text.

Package com. beyole. util; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JTextArea; public class test19 {public static void main (String [] args) {JFrame frame = new JFrame ("Crystal "); // define the form object JTextArea area = new JTextArea (3, 10); // construct a text field JLabel label = new JLabel ("construct a text field:"); label. setBounds (10, 10,120, 20); area. setBounds (130, 10,150,100); frame. setLayout (null); frame. add (label); frame. add (area); frame. setSize (300,150); frame. setLocation (300,200); frame. setVisible (true );}}

Program:

The above is just a simple construction of a text field, but if a text field is too large, it will certainly be displayed using a scroll bar, in this case, you need to set the text field in the panel with a scroll bar and use JScrollPane.

Add a scroll bar to the text field:

Package com. beyole. util; import java. awt. gridLayout; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JScrollPane; import javax. swing. JTextArea; public class test20 {public static void main (String [] args) {JFrame frame = new JFrame ("Crystal "); // instantiate the form object JTextArea area = new JTextArea (3, 20); // construct a text field area. setLineWrap (true); // if the content is too long, wrap the line automatically and add a scroll bar to the text field. The horizontal and vertical scroll bars always appear. JScrollPane pane = new JScrollPane (area, JScrollPane. VERTICAL_SCROLLBAR_ALWAYS, JScrollPane. HORIZONTAL_SCROLLBAR_ALWAYS); JLabel jLabel = new JLabel ("multiline text field:"); frame. setLayout (new GridLayout (2, 1); frame. add (jLabel); frame. add (pane); frame. setSize (300,150); frame. setLocation (300,200); frame. setVisible (true );}}

Program:

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.