Java学习-GUI编程-文本框、密码框、文本域
public class TestTextDemo extends JFrame {
public TestTextDemo(){
Container container = this.getContentPane();
JTextField jTextField1 = new JTextField("hello");
JTextField jTextField2 = new JTextField("world",20);
container.add(jTextField1,BorderLayout.NORTH);
container.add(jTextField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo();
}
}
运行程序,文本框可以输入:
密码框
public class TestTextDemo extends JFrame {
public TestTextDemo(){
Container container = this.getContentPane();
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
container.add(jPasswordField);
this.setVisible(true);
this.setBounds(100,100,100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo();
}
}
运行程序,输入的字符显示为 ‘*’:
文本域
public class TestTextDemo extends JFrame {
public TestTextDemo(){
Container container = this.getContentPane();
JTextArea jTextArea = new JTextArea(20,50);
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo();
}
}
运行程序,可以在文本域内输入文字: