Lớp JTextArea trong Java Swing



Lớp JTextArea được sử dụng để tạo một khu vực dành cho text. Nó là một khu vực gồm nhiều dòng và chỉ hiển thị thuần text. Dưới đây là cú pháp khai báo của lớp javax.swing.JTextArea:

public class JTextArea
   extends JTextComponent

Lớp này kế thừa các phương thức từ các lớp sau:

  • javax.swing.text.JTextComponent

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

Các constructor của lớp JTextArea trong Java Swing

JTextArea(): Tạo một TextArea mới.

JTextArea(String s): Tạo một TextArea mới với text đã cho.

JTextArea(int row, int column): Tạo một TextArea trống, mới với số hàng và cột đã cho.

JTextArea(String s, int row, int column): Tạo một TextArea mới với text, số hàng và cột đã cho.

Các phương thức được sử dụng phổ biến của lớp JTextArea trong Java Swing

1. public void setRows(int rows): Được sử dụng để thiết lập số hàng đã cho.

2. public void setColumns(int cols): Được sử dụng để thiết lập số cột đã cho.

3. public void setFont(Font f): Được sử dụng để thiết lập font đã cho.

4. public void insert(String s, int position): Được sử dụng để chèn text vào vị trí đã cho.

5. public void append(String s): Được sử dụng để phụ thêm text đã cho vào cuối tài liệu.

Quảng cáo

Chương trình ví dụ đơn giản đầu tiên về lớp JTextArea

import java.awt.Color;  
import javax.swing.*;  
  
public class TArea {  
    JTextArea area;  
    JFrame f;  
    TArea(){  
    f=new JFrame();  
          
    area=new JTextArea(300,300);  
    area.setBounds(10,30,300,300);  
      
    area.setBackground(Color.black);  
    area.setForeground(Color.white);  
          
    f.add(area);  
      
    f.setSize(400,400);  
    f.setLayout(null);  
    f.setVisible(true);  
}  
    public static void main(String[] args) {  
        new TArea();  
    }  
}  
Quảng cáo

Chương trình ví dụ khác về lớp JTextArea

package com.vietjack.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingControlDemo {
    
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showTextAreaDemo();
   }

   private void prepareGUI(){
      mainFrame = new JFrame("Vi du Java Swing");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    

      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showTextAreaDemo(){
      headerLabel.setText("Control in action: JTextArea"); 

      JLabel  commentlabel= new JLabel("Comments: ", JLabel.RIGHT);

      final JTextArea commentTextArea = 
         new JTextArea("This is a Swing tutorial "
         +"to make GUI application in Java.",5,20);

      JScrollPane scrollPane = new JScrollPane(commentTextArea);    

      JButton showButton = new JButton("Show");

      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            statusLabel.setText( commentTextArea.getText());        
         }
      }); 

      controlPanel.add(commentlabel);
      controlPanel.add(scrollPane);        
      controlPanel.add(showButton);
      mainFrame.setVisible(true);  
   }
}

Đã có app VietJack trên điện thoại, giải bài tập SGK, SBT Soạn văn, Văn mẫu, Thi online, Bài giảng....miễn phí. Tải ngay ứng dụng trên Android và iOS.

Theo dõi chúng tôi miễn phí trên mạng xã hội facebook và youtube:

Các bạn có thể mua thêm khóa học JAVA CORE ONLINE VÀ ỨNG DỤNG cực hay, giúp các bạn vượt qua các dự án trên trường và đi thực tập doanh nghiệp với Java. Khóa học có giá chỉ 400K, nhằm ưu đãi, tạo điều kiện cho sinh viên cho thể mua khóa học.

Nội dung khóa học gồm 16 chuơng và 100 video cực hay, học trực tiếp tại https://www.udemy.com/tu-tin-di-lam-voi-kien-thuc-ve-java-core-toan-tap/ Bạn nào có nhu cầu mua, inbox trực tiếp chị Thu, trợ lý anh Tuyền để hỗ trợ thanh toán qua mã QR ngân hàng Việt Nam, fb: https://www.facebook.com/Thule.59

Anh Tuyền, tác giả khóa học, là cựu sinh viên chương trình đào tạo kĩ sư tài năng của đại học Bách Khoa Hà Nội với hơn 5 năm kinh nghiệm đi làm thực tế doanh nghiệp và cũng là Founder website vietjack.com, web giáo dục phổ biến nhất Việt Nam hiện tại (năm 2024). Java cũng là ngôn ngữ lập trình dễ đi xin việc nhất hiện tại, với mức lương cao, hãy nâng cao kiến thức IT của bản thân mình vì một Việt Nam giàu mạnh.

Loạt bài hướng dẫn của chúng tôi dựa một phần trên nguồn tài liệu của: Tutorialspoint.com


swing_control_trong_java_swing.jsp


Tài liệu giáo viên