Java eleven文本组件

一个应用程序所有输入功能的界面几乎都是由文本组件接收用户输入的。

1、文本框组件JTextField

用来显示或编辑一个文本

构造方法 说明
JTextField() 构造一个默认的文本框组件
JTextField(String text) 包含指定字符串
JTextField(int columns) 指定列数
JTextField(String text,int columns) 指定列数并包含文本

example

 package Eleven;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import java.awt.Container;

import java.awt.FlowLayout;

public class JTextFieldD extends JFrame{
 private JTextField jtextField1;
 private JTextField jtextField;
 
 public JTextFieldD(){
  super();
  final FlowLayout flowLayout = new FlowLayout();
  flowLayout.setAlignment(FlowLayout.LEFT);
  getContentPane().setLayout(flowLayout);
  setTitle("文本演示框");
  setBounds(100,100,412,166);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container container = getContentPane();
  JPanel p1 = new JPanel(new FlowLayout());
  
  
  p1.add(new JLabel("name:"));
  jtextField = new JTextField();
  p1.add(jtextField);
  
  
  p1.add(new JLabel("sex"));
  jtextField1 = new JTextField(4);
  p1.add(jtextField1);
  container.add(p1);
  setVisible(true);
 }
 
 public static void main(String[] args){
  new JTextFieldD();
  
 }
}

Java eleven文本组件_第1张图片

2、密码框组件JPasswordField

JPasswordField提供一个setEchoChar()方法,设置密码框的回显字符

passwordField().setEchoChar("*");

 

构造方法 说明
JPasswordField() 构造一个默认的密码框组件
JPasswordField(String text) 包含指定字符
JPasswordField(int columns) 指定列数
JPasswordField(String text,int columns) 指定字符和列数

example

package Eleven;

import javax.swing.JFrame;

import java.awt.FlowLayout;

import javax.swing.JLabel;

import javax.swing.JPasswordField;

public class JPasswordFieldD extends JFrame{
 public JPasswordFieldD(){
  super();
  getContentPane().setLayout(new FlowLayout());
  setBounds(100,100,192,67);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  final JLabel label = new JLabel();
  label.setText("Password: ");
  getContentPane().add(label);
  JPasswordField passwordField = new JPasswordField("1225");
  //void javax.swing.JPasswordField.setEchoChar(char c)设置回显字符
  passwordField.setEchoChar('*');
  passwordField.setColumns(10);//设置密码框列数
  getContentPane().add(passwordField);
  setVisible(true);
 }
 public static void main(String[] args){
  JPasswordFieldD frame = new JPasswordFieldD();
  frame.setVisible(true);
 }
}

3、文本域组件JTextArea

在程序中用于接收用户的多行文字输入

 

Method Describle
JTextArea() 空文本域
JTextArea(String text) 包含文本内容
JTextArea(String text,int columns) 包含文本内容指定列数

example

package Eleven;

import javax.swing.JFrame;

import javax.swing.JTextArea;

import java.awt.BorderLayout;

import javax.swing.JLabel;

import javax.swing.JScrollPane;

import javax.swing.JPanel;

import java.awt.FlowLayout;

import javax.swing.JButton;

public class JTextAreaD extends JFrame{
 private JTextArea textArea;
 public JTextAreaD(){
  super();
  final BorderLayout borderLayout = new BorderLayout();
  getContentPane().setLayout(borderLayout);
  setBounds(100,100,231,190);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  final JLabel label = new JLabel("input your comment");//创建标签组件
  getContentPane().add(label,BorderLayout.NORTH);//添加标签到窗体顶部
  
  final JScrollPane scrollPane = new JScrollPane();//创建滚动面板
  textArea = new JTextArea("\n\nI love this book\n\n");
  //void javax.swing.JTextArea.setLineWrap(boolean wrap)
  textArea.setLineWrap(true);//设置文本自动换行
  //void javax.swing.JScrollPane.setViewportView(Component view)
  scrollPane.setViewportView(textArea);//设置滚动面板管理文本域
  getContentPane().add(scrollPane,BorderLayout.CENTER);//添加滚动面板到窗体
  
  final JPanel panel = new JPanel();
  //布局管理器
  final FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER,20,2);
  panel.setLayout(flowLayout);//设置面板布局管理器
  /*void java.awt.Container.add(Component comp, Object constraints)
   * Parameters:
  comp the component to be added
  constraints an object expressing layout constraints
   for this component*/
  getContentPane().add(panel,BorderLayout.SOUTH);//添加面板到窗体底部
  panel.add(new JButton("OK"));//添加按钮到面板
  panel.add(new JButton("Close"));
  setVisible(true);
 }
 public static void main(String[] args){
   new JTextAreaD();
  
 }
}

你可能感兴趣的:(Java eleven文本组件)