There are serious flaws in the input of single-line text, and it is not suitable for practical use, this section introduces a jtextare that can make multiple lines of input through a non-functional notepad.
JTextArea
JTextArea (): Create a text area with empty content
JTextArea (document DOC): Creates a text area with the specified document
JTextArea (document doc,string text,int rows,int columns): Create a text area with the specified document, number of rows, number of columns
JTextArea (int rows,int columns): Specifies the number of rows, the text area of the number of columns
JTextArea (String text): Specifies the text area of the text content
JTextArea (String text,int rows,int columns): Text area specifying text content and number of rows, columns
Some common methods of JTextArea:
public void append (String str): Appends the given text to the end of the document.
Boolean getlinewrap (): Gets the line wrapping policy for the text area.
public int getRows (): Returns the number of rows in the TextArea.
public boolean Getwrapstyleword (): Gets the line-wrapping method if the text area is to be wrapped.
public void Setwrapstyleword (Boolean word): Sets the line wrapping method (if the text area is to be wrapped)
public void Insert (String str, int pos): Inserts the specified text into the specified position.
public void setcolumns (int columns): Sets the number of columns in this TextArea.
public void Setlinewrap (Boolean wrap): Sets the line wrapping policy for the text area.
public void setrows (int rows): Sets the number of rows for this TextArea.
public void Seteditable (Boolean B): Sets the editing state of the text area. A parameter of TRUE indicates an editable state, or false indicates a non-editable state
JScrollPane
JScrollPane (): Creates a scroll bar that can be displayed both horizontally and vertically
JScrollPane (Component view): Creates a scrollbar that displays the contents of the specified component, displaying horizontal and vertical scroll bars when the contents of the component exceed the view size
JScrollPane (Component view,int vsbpolicy,int hsbpolicy) : Creates a scroll bar that displays the contents of the specified component with a scroll bar with the specified scroll policy
JScrollPane (int vsbpolicy,int hsbpolicy) : Creates a scroll bar with the specified scroll policy
There are 6 main scrollbar strategies used in the construction method of the JScrollPane class:
Constant constant meaning |
Horizontal_scrollbar_as_needed Show horizontal scroll bar when neededhorizontal_scrollbar_always always show horizontal scroll barhorizontal_scrollbar_never The horizontal scroll bar has not been displayed Vertical_scrollbar_as_needed Displays the vertical scrollbar Vertical_ when needed scrollbar_always Always show vertical scroll bar vertical_scrollbar_never No vertical scroll bar is displayed |
VERTICAL
public int gethorizontalscrollbarpolicy (): Gets the horizontal scrolling policy value
public int getverticalscrollbarpolicy (): Gets the vertical scrolling policy value
public void Gethorizontalscrollbarpolicy (): Set horizontal scrolling policy value
public void Getverticalscrollbarpolicy (): Set vertical scrolling policy value
public boolean iswheelscrollingenabled (): Sets whether to scroll in response to the mouse wheel
public void Setviewportview (conponent view): Sets the scroll component in the scroll bar
public void Setwheelscrollingenabled (Boolean Handlewheel): Start/Disable move response to mouse wheel scrolling
code example:
Package Ch10;import Java.awt.event.*;import javax.swing.*;p ublic class Notetextarea extends JFrame implements actionlistener{private JPanel JP = new JPanel (); JButton jb1 = new JButton ("Dot I wrap"); JButton jb2 = new JButton ("Dot I don't Break"); 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 ("wrap line, no wrap! "); } this.add (JP); This.setbounds (80,80,300,300); This.setvisible (TRUE); This.settitle ("Notepad multi-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 (); }}
Multi-line area of Notepad (text area and scroll bar)