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

📄 textcomponent.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* TextComponent.java -- Widgets for entering text   Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.awt;import java.awt.event.TextEvent;import java.awt.event.TextListener;import java.awt.peer.TextComponentPeer;import java.io.Serializable;import java.text.BreakIterator;import java.util.EventListener;import javax.accessibility.Accessible;import javax.accessibility.AccessibleContext;import javax.accessibility.AccessibleRole;import javax.accessibility.AccessibleState;import javax.accessibility.AccessibleStateSet;import javax.accessibility.AccessibleText;import javax.swing.text.AttributeSet;/**  * This class provides common functionality for widgets than   * contain text.  *  * @author Aaron M. Renn (arenn@urbanophile.com)  */public class TextComponent extends Component  implements Serializable, Accessible{/* * Static Variables */// Constant for serializationprivate static final long serialVersionUID = -2214773872412987419L;/* * Instance Variables *//**  * @serial Indicates whether or not this component is editable.  * This is package-private to avoid an accessor method.  */boolean editable;/**  * @serial The starting position of the selected text region.  * This is package-private to avoid an accessor method.  */int selectionStart;/**  * @serial The ending position of the selected text region.  * This is package-private to avoid an accessor method.  */int selectionEnd;/**  * @serial The text in the component  * This is package-private to avoid an accessor method.  */String text;/**  * A list of listeners that will receive events from this object.  */protected transient TextListener textListener;  protected class AccessibleAWTTextComponent    extends AccessibleAWTComponent    implements AccessibleText, TextListener  {    private static final long serialVersionUID = 3631432373506317811L;    // Constructor    // Adds a listener for tracking caret changes    public AccessibleAWTTextComponent()    {      TextComponent.this.addTextListener(this);    }        public AccessibleRole getAccessibleRole()    {      return AccessibleRole.TEXT;    }        public AccessibleStateSet getAccessibleStateSet()    {      // TODO: Docs say PropertyChangeEvent will fire if this state changes.      // That means that the event has to fire when editable changes.      AccessibleStateSet ss = super.getAccessibleStateSet();      if (editable)        ss.add(AccessibleState.EDITABLE);      return ss;    }        public AccessibleText getAccessibleText()    {      return this;    }        /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getIndexAtPoint(java.awt.Point)     */    public int getIndexAtPoint(Point point)    {      return TextComponent.this.getIndexAtPoint(point);    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getCharacterBounds(int)     */    public Rectangle getCharacterBounds(int index)    {      return TextComponent.this.getCharacterBounds(index);    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getCharCount()     */    public int getCharCount()    {      return text.length();    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getCaretPosition()     */    public int getCaretPosition()    {      return TextComponent.this.getCaretPosition();    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getAtIndex(int, int)     */    public String getAtIndex(int part, int index)    {      if (index < 0 || index >= text.length())        return null;      BreakIterator it = null;      switch (part)      {      	case CHARACTER:      	  return text.substring(index, index + 1);      	case WORD:      	  it = BreakIterator.getWordInstance();      	  break;      	case SENTENCE:      	  it = BreakIterator.getSentenceInstance();      	  break;      	default:      	  return null;      }  	  it.setText(text);  	  int start = index;  	  if (!it.isBoundary(index))  	    start = it.preceding(index);   	  int end = it.following(index);  	  if (end == -1)  	    return text.substring(index);  	  else  	    return text.substring(index, end);    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getAfterIndex(int, int)     */    public String getAfterIndex(int part, int index) {      if (index < 0 || index >= text.length())        return null;      BreakIterator it = null;      switch (part)      {      	case CHARACTER:      	  return text.substring(index, index + 1);      	case WORD:      	  it = BreakIterator.getWordInstance();      	  break;      	case SENTENCE:      	  it = BreakIterator.getSentenceInstance();      	  break;      	default:      	  return null;      }  	  it.setText(text);  	  int start = index;  	  if (!it.isBoundary(index))  	    start = it.following(index);  	  // Make sure there was a complete unit.  I.e. if index is in the middle  	  // of a word, return null if there is no word after the that one.  	  if (start == -1)  	    return null;  	  int end = it.following(start);  	  if (end == -1)  	    return text.substring(index);  	  else  	    return text.substring(index, end);    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getBeforeIndex(int, int)     */    public String getBeforeIndex(int part, int index)    {      if (index < 1 || index >= text.length())        return null;      BreakIterator it = null;      switch (part)      {      	case CHARACTER:      	  return text.substring(index - 1, index);      	case WORD:      	  it = BreakIterator.getWordInstance();      	  break;      	case SENTENCE:      	  it = BreakIterator.getSentenceInstance();      	  break;      	default:      	  return null;      }  	  it.setText(text);  	  int end = index;  	  if (!it.isBoundary(index))  	    end = it.preceding(index);   	  // Make sure there was a complete unit.  I.e. if index is in the middle  	  // of a word, return null if there is no word before that one.  	  if (end == -1)  	    return null;  	  int start = it.preceding(end);  	  if (start == -1)  	    return text.substring(0, end);  	  else  	    return text.substring(start, end);    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getCharacterAttribute(int)     */    public AttributeSet getCharacterAttribute(int index)    {      // FIXME: I suspect this really gets filled in by subclasses.      return null;    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getSelectionStart()     */    public int getSelectionStart() {      // TODO Auto-generated method stub      return selectionStart;    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getSelectionEnd()     */    public int getSelectionEnd()    {      return selectionEnd;    }    /* (non-Javadoc)     * @see javax.accessibility.AccessibleText#getSelectedText()     */    public String getSelectedText()    {      if (selectionEnd - selectionStart > 0)        return text.substring(selectionStart, selectionEnd);      else        return null;    }    /* (non-Javadoc)     * @see java.awt.event.TextListener#textValueChanged(java.awt.event.TextEvent)     */    public void textValueChanged(TextEvent event)    {      // TODO Auto-generated method stub          }      }/*************************************************************************//* * Constructors */TextComponent(String text){  this.text = text;  this.editable = true;}/*************************************************************************//* * Instance Methods *//**  * Returns the text in this component  *  * @return The text in this component.  */public synchronized StringgetText(){  TextComponentPeer tcp = (TextComponentPeer)getPeer();  if (tcp != null)    text = tcp.getText();  return(text);}/*************************************************************************//**  * Sets the text in this component to the specified string.  *  * @param text The new text for this component.  */public synchronized voidsetText(String text){  if (text == null)    text = "";  this.text = text;  TextComponentPeer tcp = (TextComponentPeer)getPeer();  if (tcp != null)    tcp.setText(text);

⌨️ 快捷键说明

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