1、文本框
import java.awt.*;
import javax.swing.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();
JTextField jTextField01 = new JTextField("I‘m");
JTextField jTextField02 = new JTextField("Steven",20);
container.add(jTextField01,BorderLayout.NORTH);
container.add(jTextField02,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
效果:
注:若没有布局,只会出现“Steven”
2、密码框(JPasswordField)
import java.awt.*;
import javax.swing.*;
public class TestTextDemo02 extends JFrame {
public TestTextDemo02() {
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();//****
passwordField.setEchoChar(‘*‘);
container.add(passwordField);
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
效果:
3、文本域 (JTextArea)
import java.awt.*;
import javax.swing.*;
public class TestTextDemo03 extends JFrame {
public TestTextDemo03() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("欢迎和Steven一起练Java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,500,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo03();
}
}
效果:
注:均需配合面板使用!
Swing组件基础-----文本框(文本框、密码框、文本域)