视频链接:20、文本框、密码框、文本域_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1DJ411B75F?p=20&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5
1.文本框
示例代码:
package com.yundait.lesson06;
import javax.swing.*;
import java.awt.*;
public class TextFieldDemo extends JFrame {
public TextFieldDemo(){
Container container = this.getContentPane();
//创建文本框
JTextField jTextField = new JTextField("Hello");
JTextField jTextField1 = new JTextField("World");
//将文本框添加到容器中
container.add(jTextField,BorderLayout.NORTH);
container.add(jTextField1,BorderLayout.CENTER);
//设置可见、窗口尺寸、关闭事件
this.setVisible(true);
this.setSize(600,700);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TextFieldDemo();
}
}
运行结果:
2.密码框
示例代码
package com.yundait.lesson06;
import com.yundait.lesson05.JButtonDemo01;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class PasswordFieldDemo extends JFrame {
public PasswordFieldDemo(){
Container container = this.getContentPane();
//创建密码框
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
//将密码框添加到容器中
container.add(jPasswordField);
this.setVisible(true);
this.setSize(600,700);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new PasswordFieldDemo();
}
}
运行结果
3.文本域
示例代码
package com.yundait.lesson05;
import javax.swing.*;
import java.awt.*;
public class JScrollDemo extends JFrame {
public JScrollDemo(){
//创建一个容器,
Container container = this.getContentPane();
//创建文本域
JTextArea jTextArea = new JTextArea(200,500);
jTextArea.setText("欢迎来到山东");
//创建滚动窗格,并将文本域添加上
JScrollPane jScrollPane = new JScrollPane(jTextArea);
//在容器中添加滚动窗格
container.add(jScrollPane);
//设置窗口可见、尺寸、关闭事件
this.setVisible(true);
this.setBounds(100,100,450,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
运行结果