JTextArea設置不可編輯,並顯示I型光標

JTextArea設置不可編輯,並顯示I型光標

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

public   class   Test   extends   JFrame   {
    JTextArea   text;

    public   Test()   {
        super( "JTextArea ");
        this.addWindowListener(new   WindowAdapter()   {
            public   void   windowClosing(WindowEvent   windowEvent)   {
                System.exit(0);
            }
        });
        text   =   new   JTextArea();
        text.setText( "我們都有一個家,名字叫中國,兄弟姐妹都很多...... ");
        text.setEditable(false);
        text.addMouseListener(new   MouseAdapter()   {
            public   void   mouseEntered(MouseEvent   mouseEvent)   {
                text.setCursor(new   Cursor(Cursor.TEXT_CURSOR));   //鼠標進入Text區後變爲文本輸入指針
            }
            public   void   mouseExited(MouseEvent   mouseEvent)   {
                text.setCursor(new   Cursor(Cursor.DEFAULT_CURSOR));   //鼠標離開Text區後恢復默認形態
            }
        });
        text.getCaret().addChangeListener(new   ChangeListener()   {
            public   void   stateChanged(ChangeEvent   e)   {
                text.getCaret().setVisible(true);   //使Text區的文本光標顯示
            }
        });
        this.getContentPane().add(text);
        this.setSize(300,   200);
        this.setVisible(true);
    }
    public   static   void   main(String[]   args)   {
        new   Test();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章