⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textcomponent.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)TextComponent.java	1.34 98/07/01
 *
 * Copyright 1995-1998 by Sun Microsystems, Inc.,
 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information
 * of Sun Microsystems, Inc. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Sun.
 */
package java.awt;

import java.awt.peer.TextComponentPeer;
import java.awt.event.*;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import sun.awt.SunToolkit;


/**
 * The <code>TextComponent</code> class is the superclass of 
 * any component that allows the editing of some text. 
 * <p>
 * A text component embodies a string of text.  The 
 * <code>TextComponent</code> class defines a set of methods 
 * that determine whether or not this text is editable. If the
 * component is editable, it defines another set of methods
 * that supports a text insertion caret. 
 * <p>
 * In addition, the class defines methods that are used 
 * to maintain a current <em>selection</em> from the text. 
 * The text selection, a substring of the component's text, 
 * is the target of editing operations. It is also referred
 * to as the <em>selected text</em>.
 *
 * @version	1.34, 07/01/98
 * @author 	Sami Shaio
 * @author 	Arthur van Hoff
 * @since       JDK1.0
 */
public class TextComponent extends Component {

    /**
     * The value of the text.
     */
    String text;

    /**
     * A boolean indicating whether or not this TextComponent is editable.
     */
    boolean editable = true;

    /**
     * The selection start.
     */
    int selectionStart;

    /**
     * The selection end.
     */
    int selectionEnd;

    transient protected TextListener textListener;

    /*
     * JDK 1.1 serialVersionUID 
     */
    private static final long serialVersionUID = -2214773872412987419L;

    /**
     * Constructs a new text component initialized with the 
     * specified text. Sets the value of the cursor to 
     * <code>Cursor.TEXT_CURSOR</code>.
     * @param      text the initial text that the component presents.
     * @see        java.awt.Cursor
     * @since      JDK1.0
     */
    TextComponent(String text) {
	this.text = text;
	setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
    }

    boolean areInputMethodsEnabled() {
	try {
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            return ((SunToolkit) toolkit).enableInputMethodsForTextComponent();
        } catch (Exception e) {
            // if something bad happens, just don't enable input methods
            return false;
	}
    }

    /**
     * Removes the TextComponent's peer.  The peer allows us to modify
     * the appearance of the TextComponent without changing its
     * functionality.
     */
    public void removeNotify() {
      synchronized(getTreeLock()) {
	TextComponentPeer peer = (TextComponentPeer)this.peer;
	if (peer != null) {
	    text = peer.getText();
	    selectionStart = peer.getSelectionStart();
	    selectionEnd = peer.getSelectionEnd();
	}
	super.removeNotify();
      }
    }

    /**
     * Sets the text that is presented by this 
     * text component to be the specified text. 
     * @param       t   the new text.
     * @see         java.awt.TextComponent#getText  
     * @since       JDK1.0
     */
    public synchronized void setText(String t) {
	text = t;
	TextComponentPeer peer = (TextComponentPeer)this.peer;
	if (peer != null) {
	    peer.setText(t);
	}
    }

    /**
     * Gets the text that is presented by this text component.
     * @see     java.awt.TextComponent#setText
     * @since   JDK1.0
     */
    public synchronized String getText() {
	TextComponentPeer peer = (TextComponentPeer)this.peer;
	if (peer != null) {
	    text = peer.getText();
	}
	return text;
    }

    /**
     * Gets the selected text from the text that is
     * presented by this text component.  
     * @return      the selected text of this text component.
     * @see         java.awt.TextComponent#select
     * @since       JDK1.0
     */
    public synchronized String getSelectedText() {
	return getText().substring(getSelectionStart(), getSelectionEnd());
    }

    /**
     * Indicates whether or not this text component is editable.
     * @return     <code>true</code> if this text component is
     *                  editable; <code>false</code> otherwise.
     * @see        java.awt.TextComponent#setEditable
     * @since      JDK1ble
     */
    public boolean isEditable() {
	return editable;
    }

    /**
     * Sets the flag that determines whether or not this
     * text component is editable.
     * <p>
     * If the flag is set to <code>true</code>, this text component 
     * becomes user editable. If the flag is set to <code>false</code>, 
     * the user cannot change the text of this text component. 
     * @param     t   a flag indicating whether this text component 
     *                      should be user editable.
     * @see       java.awt.TextComponent#isEditable
     * @since     JDK1.0
     */
    public synchronized void setEditable(boolean b) {
	editable = b;
	TextComponentPeer peer = (TextComponentPeer)this.peer;
	if (peer != null) {
	    peer.setEditable(b);
	}
    }

    /**
     * Gets the start position of the selected text in 
     * this text component. 
     * @return      the start position of the selected text. 
     * @see         java.awt.TextComponent#setSelectionStart
     * @see         java.awt.TextComponent#getSelectionEnd
     * @since       JDK1.0
     */
    public synchronized int getSelectionStart() {
	TextComponentPeer peer = (TextComponentPeer)this.peer;
	if (peer != null) {
	    selectionStart = peer.getSelectionStart();
	}
	return selectionStart;
    }

    /**
     * Sets the selection start for this text component to  
     * the specified position. The new start point is constrained 
     * to be at or before the current selection end. It also
     * cannot be set to less than zero, the beginning of the 
     * component's text.
     * If the caller supplies a value for <code>selectionStart</code>
     * that is out of bounds, the method enforces these constraints
     * silently, and without failure.
     * @param       selectionStart   the start position of the 
     *                        selected text.
     * @see         java.awt.TextComponent#getSelectionStart
     * @see         java.awt.TextComponent#setSelectionEnd
     * @since       JDK1.1
     */
    public synchronized void setSelectionStart(int selectionStart) {
	/* Route through select method to enforce consistent policy
    	 * between selectionStart and selectionEnd.
    	 */
	select(selectionStart, getSelectionEnd());
    }

    /**
     * Gets the end position of the selected text in 
     * this text component. 
     * @return      the end position of the selected text. 
     * @see         java.awt.TextComponent#setSelectionEnd
     * @see         java.awt.TextComponent#getSelectionStart
     * @since       JDK1.0
     */
    public synchronized int getSelectionEnd() {
	TextComponentPeer peer = (TextComponentPeer)this.peer;
	if (peer != null) {
	    selectionEnd = peer.getSelectionEnd();
	}
	return selectionEnd;
    }

    /**
     * Sets the selection end for this text component to  
     * the specified position. The new end point is constrained 
     * to be at or after the current selection start. It also
     * cannot be set beyond the end of the component's text.
     * If the caller supplies a value for <code>selectionEnd</code>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -