textfield.java

来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 101 行

JAVA
101
字号
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.gui.internal.edit;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;

public class TextField extends AbstractEditField implements ActionListener,FocusListener,KeyListener {
    JTextComponent field;
    JScrollPane scrollPane;
    Object oldValue;
    public TextField(RaplaContext sm,String fieldName) throws RaplaException {
        this( sm,fieldName, 1, DEFAULT_LENGTH);
    }
        
    public TextField(RaplaContext sm,String fieldName, int rows, int columns) throws RaplaException {
        super( sm);
        setFieldName( fieldName );
        if ( rows > 1 ) {
            field = new JTextArea();
            scrollPane = new JScrollPane( field, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            ((JTextArea) field).setColumns( columns);
            ((JTextArea) field).setRows( rows );
        } else {
            field = new JTextField( columns);
        }
        field.addFocusListener(this);
        field.addKeyListener(this);
        setValue("");
    }

    protected Object getValue() {
        return field.getText().trim();
    }
    
    protected void setValue(Object object) {
        if (object == null)
            object = "";
        field.setText((String)object);
        oldValue = (String) object;
    }
    public JComponent getComponent() {
        if ( scrollPane != null ) {
            return scrollPane;
        } else {
            return field;
        }
    }

    public void actionPerformed(ActionEvent evt) {
        if (field.getText().equals(oldValue))
            return;
        oldValue = field.getText();
        fireContentChanged();
    }

    public void focusLost(FocusEvent evt) {
        if (field.getText().equals(oldValue))
            return;
        oldValue = field.getText();
        fireContentChanged();
    }

    public void focusGained(FocusEvent evt) {
    }

    public void keyPressed(KeyEvent evt) {}
    public void keyTyped(KeyEvent evt) {}
    public void keyReleased(KeyEvent evt) {
        if (field.getText().equals(oldValue))
            return;
        oldValue = field.getText();
        fireContentChanged();
    }

}

⌨️ 快捷键说明

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