20.文本框、密码框、文本域

文本框:JTextField
 1 package com.gui.lesson6;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class TestDemo extends JFrame {
 7     public TestDemo() {
 8         Container container = this.getContentPane();
 9 
10         //文本框
11         JTextField jTextField = new JTextField("hello");
12         JTextField jTextField2 = new JTextField("world", 20);
13         container.add(jTextField, BorderLayout.NORTH);
14         container.add(jTextField2, BorderLayout.SOUTH);
15 
16 
17         this.setVisible(true);
18         this.setSize(300, 300);
19         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
20     }
21 
22     public static void main(String[] args) {
23         new TestDemo();
24     }
25 }

密码框:JPasswordField、setEchoChar
 1 package com.gui.lesson6;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class TestDemo2 extends JFrame {
 7     public TestDemo2() {
 8         Container container = getContentPane();
 9 
10         //面板
11         JPasswordField passwordField = new JPasswordField();//****
12         passwordField.setEchoChar('*');
13 
14         container.add(passwordField);
15         
16         setVisible(true);
17         setSize(500, 350);
18         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
19 
20     }
21 
22     public static void main(String[] args) {
23         new TestDemo2();
24     }
25 }

文本域:JTextArea、配合面板JScrollPane使用、也是最常见的使用
 1 package com.gui.lesson6;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 
 6 public class TestDemo3 extends JFrame {
 7     public TestDemo3() {
 8         Container container = this.getContentPane();
 9 
10         //文本域
11         JTextArea textArea = new JTextArea(20, 50);
12         textArea.setText("小朋友学习Java之路");
13 
14         //Scroll面板
15         JScrollPane scrollPane = new JScrollPane(textArea);
16         container.add(scrollPane);
17 
18         this.setVisible(true);
19         this.setSize(500, 350);
20         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
21     }
22 
23     public static void main(String[] args) {
24         new TestDemo3();
25     }
26 }

一般文本域配合滚动面板使用,因为设置了行列,所以超过行列就会有滚动条

 
posted @ 2020-03-30 17:04  断浮  阅读(265)  评论(0编辑  收藏  举报