One: JTextField (text box) use:
JTextField is a lightweight component that allows you to edit single-line text.
1.JTextField Common methods of construction:
JTextField () constructs a new TextField.
JTextField (int columns) constructs a new empty TextField with the specified number of columns.
JTextField (String text) constructs a new TextField initialized with the specified text.
JTextField (String text, int columns) constructs a new TextField initialized with the specified text and columns.
2.JTextField Common methods:
SetText (String) to set text values in a text field
GetText () returns the input text value in the text field
GetColumns () returns the number of columns in a text field
Seteditable (Boolean) sets whether the text field is read-only
Examples of 3.JTextField use:
Copy Code code as follows:
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
public class JTextFieldDemo1 {
JFrame JF;
JPanel JP;
JTextField Jtf1,jtf2,jtf3,jtf4;
Public JTextFieldDemo1 () {
JF = new JFrame ("TextField case");
Container ContentPane = Jf.getcontentpane ();
Contentpane.setlayout (New BorderLayout ());
JP = new JPanel ();
JTF1 = new JTextField ();
JTF2 = new JTextField (10);
JTF3 = new JTextField ("Specify text content");
JTF4 = new JTextField ("Specify content + Specify length (read-only)", 30);
Jtf3.setenabled (FALSE);
Jtf4.setfont (New Font ("Harmonic body", font.bold| font.italic,16));
To set the horizontal alignment of text
Jtf4.sethorizontalalignment (Jtextfield.center);
Jp.add (JTF1);
Jp.add (JTF2);
Jp.add (JTF3);
Jp.add (JTF4);
Contentpane.add (JP);
Jf.pack ();
Jf.setlocation (400, 200);
Jf.setvisible (TRUE);
Jf.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
}
public static void Main (string[] args) {
New JTextFieldDemo1 ();
}
}
Effect Chart:
II: The use of JTextArea (text area):
1.JTextArea Common methods of construction:
JTextArea () constructs a new TextArea.
JTextArea (String text) constructs a new TextArea that displays the specified text.
JTextArea (int rows, int columns) constructs a new empty TextArea with the specified number of rows and columns.
JTextArea (String text, int rows, int columns) constructs a new TextArea with the specified number of text, rows, and columns.
Use examples:
Copy Code code as follows:
JTextArea T1 = new JTextArea ();
JTextArea t2 = new JTextArea (2, 8);
jtextarea t3 = new JTextArea ("JTextArea3");
jtextarea T4 = new JTextArea ("JTextArea4", 5, 10);
2.JTextArea Common methods:
Use examples:
Copy Code code as follows:
T1.settext ("JTextArea1");//SetText () set the contents of the text display
T2.append ("JTextArea2");//The Append () method appends the given text to the end of the document.
T4.setlinewrap (TRUE);//sets the line-wrapping policy for the text area.
T4.setfont (New Font ("superscript italics", Font.Bold, 16)); Sets the current font.
T4.settabsize (2);//Use the Settabsize () method to set the skip distance of the [Tab] key
Put the JTextArea in the JScrollPane so that you can see the text entered more than the JTextArea height using the scrolling effect.
3.JTextArea use of the case:
Copy Code code as follows:
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Implement Interface ActionListener
public class JTextAreaDemo3 implements ActionListener {
JFrame JF;
JPanel JPanel;
JButton jb1, JB2, jb3;
JTextArea JTA = null;
JScrollPane JScrollPane;
Public JTextAreaDemo3 () {
JF = new JFrame ("JTextArea case 3");
Container ContentPane = Jf.getcontentpane ();
Contentpane.setlayout (New BorderLayout ());
JTA = new JTextArea (10, 15);
Jta.settabsize (4);
Jta.setfont (New Font ("superscript italics", Font.Bold, 16));
Jta.setlinewrap (TRUE);//Activate the wrapping function
Jta.setwrapstyleword (TRUE);//Activate break word function
Jta.setbackground (Color.pink);
JScrollPane = new JScrollPane (JTA);
JPanel = new JPanel ();
Jpanel.setlayout (New GridLayout (1, 3));
JB1 = new JButton ("copy");
Jb1.addactionlistener (this);
JB2 = new JButton ("paste");
Jb2.addactionlistener (this);
JB3 = new JButton ("cut");
Jb3.addactionlistener (this);
Jpanel.add (JB1);
Jpanel.add (JB2);
Jpanel.add (JB3);
Contentpane.add (JScrollPane, Borderlayout.center);
Contentpane.add (JPanel, Borderlayout.south);
Jf.setsize (400, 300);
Jf.setlocation (400, 200);
Jf.setvisible (TRUE);
Jf.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
}
The method of covering interface ActionListener actionperformed
public void actionperformed (ActionEvent e) {
if (e.getsource () = = JB1) {
Jta.copy ();
else if (e.getsource () = = JB2) {
Jta.paste ();
else if (e.getsource () = = Jb3) {
Jta.cut ();
}
}
public static void Main (string[] args) {
New JTextAreaDemo3 ();
}
}
Run Results Demo: