JTextArea คลาสสามารถขยาย JTextComponent และอนุญาตให้ผู้ใช้ป้อน ข้อความหลายบรรทัด ข้างในนั้น JTextArea สามารถสร้าง CaretListener ส่วนต่อประสานซึ่งสามารถฟังเหตุการณ์การอัพเดทคาเร็ต เราสามารถตั้งค่าฟอนต์เป็นข้อความภายใน JTextArea โดยใช้ setFont() วิธีการ
ตัวอย่าง
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();
}
} ผลลัพธ์
