Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Cách hiển thị văn bản in đậm trong JTextArea trong Java

Lớp JTextArea mở rộng JTextComponent và cho phép người dùng nhập nhiều dòng văn bản. JTextArea có thể tạo giao diện CaretListener để lắng nghe sự kiện cập nhật con trỏ. Chúng ta có thể thiết lập font cho văn bản trong JTextArea bằng phương thức setFont().

Ví dụ

import java.awt.*;
import javax.swing.*;

public class JTextAreaTextBoldTest extends JFrame {
    private JTextArea textArea;

    public JTextAreaTextBoldTest() {
        setTitle("JTextAreaTextBold Test");
        setLayout(new BorderLayout());
        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        Font boldFont = new Font(textArea.getFont().getName(), Font.BOLD, textArea.getFont().getSize());
        textArea.setFont(boldFont); // bold text
        add(textArea);
        setSize(375, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JTextAreaTextBoldTest();
    }
}

Kết quả

Cách hiển thị văn bản in đậm trong JTextArea trong Java