• Java:JTextArea类


    JTextArea文本域组件,其实跟JTextField文本框组件的使用非常相似,只不过文本框是一行,但文本域可以是多行。

    JTextArea类所在包

    JPasswordField类的所在包不用说大家都知道是javax.swing包了,所以开头导入。

    import javax.swing.*;
    

    JTextArea类构造方法

    public JTextArea();
    public JTextArea(String text);
    public JTextArea(int rows, int columns);
    public JTextArea(String text, int rows, int columns);
    

    主要的构造方法就这些,这里简单说一下text就是文本域中的初始内容,rows和columns分别代表文本域的行数和列数。

    JTextArea类使用方法

    JTextArea的使用真的可以说跟JTextField极为相似,先是设置文本,下面的代码就是将文本域的内容设置为字符串text。如果你想清空所有文本,其实就是将文本设置为一个空串,即让text = ""。

    text_area.setText(text);
    

    接下来是追加文本的方法,就是在当前文本的后面追加一个字符串text,实现起来很简单。

    text_area.append(text)
    

    之后是将焦点重新转移到文本域,因为当你点完按钮后,焦点就到了按钮,如果要继续在文本域内输入需要点一下文本域才能继续编辑,所以JTextArea类又提供了一个方法来帮助你将焦点重新回到文本域。

    text_area.requestFocus();
    

    下一个方法是用来获取当前文本域内的文本的,返回的是一个字符串。

    String str = text_area.getText();
    

    最后一个方法是来设置自动换行的,默认情况下我们会发现如果我们输入到头了,文本域不会自动为你换行,所以为了让文本域在一行到头时自动换行,JTextArea类又提供了一个方法来解决这个问题。这里的flag为一个boolean类型的值,如果为true则设置自动换行,否则是取消自动换行。

    text_area.setLineWrap(flag);
    

    代码

    package technology;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyFirstTextArea extends JFrame {
    	final static long serialVersionUID = 1L;
    	JPanel panel_center = new JPanel(), panel_south = new JPanel();
    	Container container = getContentPane();
    	JTextArea text_area = new JTextArea("文本域", 10, 20);
    	JButton clear_button = new JButton("清空"), laugh_button = new JButton("哈哈"), system_button = new JButton("记录");
    	
    	public MyFirstTextArea()
    	{
    		super("JFrame窗体");
    		this.setBounds(200, 100, 400, 300);
    		text_area.setLineWrap(true);
    		clear_button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				text_area.setText("");
    				text_area.requestFocus();
    			}
    		});
    		laugh_button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				text_area.append("233");
    				text_area.requestFocus();
    			}
    		});
    		system_button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				System.out.println(text_area.getText());
    				System.out.println("====================================");
    				text_area.requestFocus();
    			}
    		});
    		panel_center.add(text_area);
    		panel_south.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 20));
    		panel_south.add(clear_button);
    		panel_south.add(laugh_button);
    		panel_south.add(system_button);
    		container.setLayout(new BorderLayout());
    		container.add(panel_center, BorderLayout.CENTER);
    		container.add(panel_south, BorderLayout.SOUTH);
    		this.setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    		new MyFirstTextArea();
    	}
    }
    

    效果图如下:

    在这里插入图片描述

  • 相关阅读:
    ASP.NET MVC 视图
    ASP.NET MVC 控制器
    ASP.NET MVC 入门
    SQL 兼容性设置
    this template attempted to load component assembly 'NuGet.VsiualStudio.interop,version=1.0.0.0 culture=neutral publickeytoken=0000000'. 解决方案
    SQL 时间函数
    [原创]PageOffice+MobOffice 电脑、移动跨平台Office文档在线流转解决方案
    [原创]Java调用PageOffice给Word中的Table赋值
    [原创]Java调用PageOffice在线打开数据库中保存的Word文件
    [原创]新版PageOffice V4.0为什么用弹出窗口的方式打开文件?
  • 原文地址:https://www.cnblogs.com/000zwx000/p/12530829.html
Copyright © 2020-2023  润新知