首页 > Windows开发 > 详细

Swing组件基础-----文本框(文本框、密码框、文本域)

时间:2021-05-01 20:56:55      阅读:28      评论:0      收藏:0      [点我收藏+]

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组件基础-----文本框(文本框、密码框、文本域)

原文:https://www.cnblogs.com/StevenPark/p/14724348.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!