1. Use of JTextField (text box)
JTextField is a lightweight component that can edit single-line text, realize cutting, copying, pasting, shortcut keys, etc. If the length of the text exceeds the display range, the text will be automatically scrolled. The constructor of the JTextField class
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. Example of using JTextField:
package ch10; import java.awt.event.*; import javax.swing.*; public class LoginTest extends JFrame implements ActionListener { private JPanel jp = new JPanel(); JLabel name = new JLabel("Please enter username"); JLabel password = new JLabel("Please enter password"); JLabel show = new JLabel(""); private JLabel[] jl = new JLabel[]{name,password,show}; JButton login = new JButton("Login"); JButton reset = new JButton("Reset"); private JButton[] jb = new JButton[]{login,reset}; private JTextField jname= new JTextField(); private JPasswordField jpass = new JPasswordField(); public LoginTest() { jp.setLayout(null); for(int i=0;i<2;i++) { jl[i].setBounds(30,20+40*i,180,20); jb[i].setBounds(30+110*i,100,80,20); jb[i].addActionListener(this); jp.add(jl[i]); jp.add(jb[i]); } jname.setBounds(130,15,100,20); jp.add(jname); jname.addActionListener(this); jpass.setBounds(130,60,100,20); jp.add(jpass); jpass.addActionListener(this); jpass.setEchoChar('*'); jl[2].setBounds(10,180,270,20); jp.add(jl[2]); this.add(jp); this.setBounds(200,200,300,300); this.setVisible(true); this.setTitle("Login Window"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent a) { if(a.getSource()==jname) { jpass.requestFocus(); } else if(a.getSource()==jb[1]) { jl[2].setText(""); jname.setText(""); jpass.setText(""); jname.requestFocus(); } else { if(jname.getText().equals("lixiang")&&String.valueOf(jpass.getPassword()).equals("201407239")) { jl[2].setText("Login successfully, welcome to your arrival!"); } else { jl[2].setText("Sorry, your password or username is wrong! "); } } } public static void main(String args[]) { new LoginTest(); } }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, rows, and columns.
Example of usage:
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:
Example of usage:
t1.setText("JTextArea1");// setText() sets the content of the text display
t2.append("JTextArea2");// The append() method appends the given text to the end of the document.
t4.setLineWrap(true);// Set the line breaking policy for the text area.
t4.setFont(new Font("林正", 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 you can use the scrolling effect to see text that is input exceeding the height of JTextArea.
3. JTextArea use case:
import java.awt.*; import java.awt.event.*; import javax.swing.*; //Implement the 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("Share", Font.BOLD, 16)); jta.setLineWrap(true);// Activate the automatic line wrap function jta.setWrapStyleWord(true);// Activate the line breaking and 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); } }); } // Overwrite the interface ActionListener method 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(); } }The above is all about this article, I hope it will be helpful to everyone's learning.