1: JTextField (text box) usage:
JTextField is a lightweight component that allows editing of a single line of text.
1. Common construction methods of JTextField:
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. Common methods of JTextField:
SetText(string) sets the text value in the text field
GetText() returns the input text value in the text field
getColumns() returns the number of columns in the text field
setEditable(Boolean) sets whether the text field is read-only
3.Usage examples of JTextField:
Copy the 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("Specified content + specified length (read-only state)",30);
jtf3.setEnabled(false);
jtf4.setFont(new Font("harmony",Font.BOLD|Font.ITALIC,16));
//Set the horizontal alignment of the 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();
}
}
Rendering:
2: Use of JTextArea (text area):
1. Common construction methods of JTextArea:
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 text, number of rows, and number of columns.
Usage example:
Copy the 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. Common methods of JTextArea:
Usage example:
Copy the code code as follows:
t1.setText("JTextArea1"); // setText() sets the content of the text display
t2.append("JTextArea2"); // The append() method will append the given text to the end of the document.
t4.setLineWrap(true);//Set the line wrapping strategy of the text area.
t4.setFont(new Font("standard style", Font.BOLD, 16)); //Set the current font.
t4.setTabSize(2);//Use the setTabSize() method to set the jump distance of the [Tab] key
Put JTextArea into JScrollPane, so that you can use the scrolling effect to see text entered that exceeds the height of JTextArea.
3.JTextArea usage cases:
Copy the 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("biaicai", Font.BOLD, 16));
jta.setLineWrap(true);//Activate automatic line wrapping function
jta.setWrapStyleWord(true);//Activate line breaking and word breaking 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);
}
});
}
// Override the method actionPerformed of the interface ActionListener
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();
}
}
Running result demonstration: