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: