Computer >> Máy Tính >  >> Lập trình >> Java

Làm cách nào chúng ta có thể đặt hướng của JTextArea từ phải sang trái trong Java?

A JTextArea là một lớp con của JTextComponent lớp và nó là một thành phần văn bản nhiều dòng để hiển thị văn bản hoặc cho phép người dùng nhập văn bản. JTextArea có thể tạo CaretListener khi chúng tôi đang cố gắng triển khai chức năng của JTextArea. Theo mặc định, một JTextarea cho phép định hướng từ trái sang phải , nếu người dùng muốn nhập văn bản từ phải sang trái bằng cách sử dụng s etComponentOrientation ( ComponentOrientation.RIGHT_TO_LEFT ) phương thức của lớp JTextArea.

Ví dụ

import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
public class JTextAreaOrientationTest extends JFrame {
   private JTextArea textArea;
   public JTextAreaOrientationTest() {
      setTitle("JTextAreaOrientation Test");
      textArea = new JTextArea();
      textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      add(new JScrollPane(textArea), BorderLayout.CENTER);
      setSize(400, 275);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String args[]) {
      new JTextAreaOrientationTest();
   }
}

Đầu ra

Làm cách nào chúng ta có thể đặt hướng của JTextArea từ phải sang trái trong Java?