A JTextArea เป็นคลาสย่อยของ JTextComponent คลาสและเป็น องค์ประกอบข้อความหลายบรรทัด เพื่อแสดงข้อความหรืออนุญาตให้ผู้ใช้ป้อนข้อความ JTextArea สามารถสร้าง CaretListener อินเทอร์เฟซเมื่อเราพยายามใช้ฟังก์ชันการทำงานของ JTextArea โดยค่าเริ่มต้น JTextArea ชั้นเรียนสามารถรองรับ ตัด คัดลอก และวาง เรายังปิดหรือปิด . ได้ การทำงานของ ตัด คัดลอก และวาง โดยใช้ getInputMap().put() วิธีการของ JTextArea ระดับ. เราสามารถใช้ KeyStroke.getKeyStroke("control X") สำหรับการตัด KeyStroke.getKeyStroke("control C") สำหรับคัดลอกและ KeyStroke.getKeyStroke("control V") สำหรับแปะ
ตัวอย่าง
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextAreaCutCopyPasteDisableTest extends JFrame {
private JTextArea textArea;
private JButton cut, copy, paste;
private JPanel panel;
public JTextAreaCutCopyPasteDisableTest() {
setTitle("JTextAreaCutCopyPasteDisable Test");
setLayout(new BorderLayout());
panel = new JPanel();
textArea = new JTextArea();
cut = new JButton("Cut");
cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
textArea.getInputMap().put(KeyStroke.getKeyStroke("control X"), "none");// disable cut
}
});
panel.add(cut);
copy = new JButton("Copy");
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
textArea.getInputMap().put(KeyStroke.getKeyStroke("control C"), "none"); // disable copy
}
});
panel.add(copy);
paste = new JButton("Paste");
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
textArea.getInputMap().put(KeyStroke.getKeyStroke("control V"), "none"); // disable paste
}
});
panel.add(paste);
add(panel, BorderLayout.NORTH);
add(new JScrollPane(textArea), BorderLayout.CENTER);
setSize(400, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String []args) {
new JTextAreaCutCopyPasteDisableTest();
}
} ผลลัพธ์
