There are serious defects in the input of single-line text and are not suitable for practical use. This section introduces JTextArea that can perform multiple-line input through a non-functional notepad:
JTextArea(): Create a text area with empty content
JTextArea(Document doc): Create a text area with a specified document
JTextArea(Document doc,String text,int rows,int columns) : Create a text area with specified documents, rows, and columns
JTextArea(int rows,int columns): Specify the text area of the number of rows and columns
JTextArea(String text): Specify the text area of the text content
JTextArea(String text,int rows,int columns): Specify the text area of the text content and number of rows and columns
Some common methods of JTextArea:
public void append(String str) : Append the given text to the end of the document.
boolean getLineWrap(): Get the line breaking policy for the text area.
public int getRows() : Returns the number of rows in TextArea.
public boolean getWrapStyleWord(): Get the line break method (if the text area wants to have a line break).
public void setWrapStyleWord(boolean word) : Set the line breaking method (if the text area wants to be broken)
public void insert(String str, int pos) : Insert the specified text into the specified location.
public void setColumns(int columns) : Set the number of columns in this TextArea.
public void setFont(Font f) : Set the current font.
public void setLineWrap(boolean wrap) : Set the line wrap policy for the text area.
public void setRows(int rows) : Set the number of rows for this TextArea.
public void setEditable(boolean b): Sets the editing status of the text area. The parameter is true to indicate editable state, and if false, it means non-editable state
Put JTextArea into JScrollPane, so you can use the scrolling effect to see text that is input exceeding the height of JTextArea.
JScrollPane
JscrollPane(): Create a scroll bar that can be displayed horizontally and vertically
JscrollPane(Component view): Create a scroll bar that displays the content of the specified component. When the content of the component exceeds the view size, horizontal and vertical scroll bars will be displayed.
JscrollPane(Component view,int vsbPolicy,int hsbPolicy): Create a scroll bar that displays the content of the specified component and has a scroll bar that specifies the scroll policy.
JscrollPane(int vsbPolicy,int hsbPolicy) : Create a scroll bar with a specified scrolling policy
The scrollbar strategies used in the constructor of the JscrollPane class are mainly as follows:
public int getHorizontalScrollBarPolicy (): Get the horizontal scrolling policy value
public int getVerticalScrollBarPolicy(): Get the vertical scrolling policy value
public void getHorizontalScrollBarPolicy (): Set the horizontal scrolling policy value
public void getVerticalScrollBarPolicy(): Set the vertical scrolling policy value
public boolean isWheelScrollingEnabled(): Sets whether to scroll in response to the mouse wheel
public void setViewportView(Conponent view) : Set the scrolling component in the scrollbar
public void setWheelScrollingEnabled(boolean handleWheel): start/disable the movement response to mouse wheel scrolling
Code example:
package ch10; import java.awt.event.*; import javax.swing.*; public class NoteTextArea extends JFrame implements ActionListener { private JPanel jp = new JPanel (); JButton jb1 = new JButton("click me to wrap the line automatically"); JButton jb2 = new JButton("click me not wrap the line"); private JTextArea jt = new JTextArea(); private JScrollPane js = new JScrollPane(jt); public NoteTextArea() { jp.setLayout(null); jb1.setBounds(40,40,180,20); jb2.setBounds(280,40,180,20); jp.add(jb1); jp.add(jb2); jb1.addActionListener(this); jb2.addActionListener(this); js.setBounds(40,80,420,100); jp.add(js); jt.setLineWrap(false); for(int i=0;i<30;i++) { jt.append("Automatically wrap, no line break!"); } this.add(jp); this.setBounds(80,80,300,300); this.setVisible(true); this.setTitle("Notepad multiline text area"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent a) { if(a.getSource()==jb1) { jt.setLineWrap(true); } else if(a.getSource()==jb2) { jt.setLineWrap(false); } } public static void main(String args[]) { new NoteTextArea(); } } The above is all about this article, I hope it will be helpful to everyone's learning.