GUI-Swing-文本框/密码框/文本域
文本框
代码:
1 package com.luckylu.gui; 2 3 import javax.swing.*; 4 import java.awt.*; 5 6 public class TestTextDemo extends JFrame { 7 public TestTextDemo() { 8 Container container = this.getContentPane(); 9 10 JTextField textField1 = new JTextField("hello--up"); 11 JTextField textField2 = new JTextField("hello--down"); 12 13 container.add(textField1,BorderLayout.NORTH); 14 container.add(textField2,BorderLayout.SOUTH); 15 16 this.setVisible(true); 17 this.setTitle("文本框案例"); 18 this.setBounds(200,200,200,300); 19 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 20 21 } 22 23 public static void main(String[] args) { 24 new TestTextDemo(); 25 } 26 }
结果:
密码框
代码:
1 package com.luckylu.gui; 2 3 import javax.swing.*; 4 import java.awt.*; 5 6 public class TestJPasswordFied extends JFrame { 7 public TestJPasswordFied(){ 8 Container container = this.getContentPane(); 9 10 JPasswordField passwordField = new JPasswordField(); // 11 passwordField.setEchoChar('$'); // 任意符号 12 13 container.add(passwordField); 14 15 this.setVisible(true); 16 this.setTitle("密码框案例"); 17 this.setBounds(200,200,200,300); 18 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 19 20 } 21 22 public static void main(String[] args) { 23 new TestJPasswordFied(); 24 } 25 }
结果:
文本域
代码:
1 package com.luckylu.gui; 2 3 import javax.swing.*; 4 import java.awt.*; 5 6 public class TestAreaDemo extends JFrame { 7 public TestAreaDemo() { 8 Container container = this.getContentPane(); 9 10 //创建文本域 11 JTextArea textArea = new JTextArea(20, 50);//设置文本域的行数和列数 12 textArea.setText("请输入~~~~"); //设置文本域的默认文本 13 14 //Scroll面板 15 JScrollPane scrollPane = new JScrollPane(textArea); //创建面板并将文本域装入面板 16 container.add(scrollPane); //将面板装入容器 17 18 this.setVisible(true); 19 this.setTitle("带滚动条的文本域"); 20 this.setBounds(200,200,300,280); 21 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 22 23 } 24 25 public static void main(String[] args) { 26 new TestAreaDemo(); 27 } 28 }
结果: