📄 mjtextarea.java
字号:
/* * Copyright (C) 2003 Adam Olsen * * 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; either version 1, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 675 Mass * Ave, Cambridge, MA 02139, USA. */package com.valhalla.gui;import javax.swing.*;import java.awt.event.ActionEvent;public class MJTextArea extends JTextArea{ public MJTextArea(int rows, int cols) { super(rows, cols); CopyPasteContextMenu.registerComponent(this, true); initializeKeyboardBindings(); } public MJTextArea() { CopyPasteContextMenu.registerComponent(this, true); initializeKeyboardBindings(); } /** * adds keyboard action to this MJTextArea * @param aKeyEventAction to add */ public void addKeyboardBindingAction(KeyBindingAction aKeyEventAction) { getInputMap().put(KeyStroke.getKeyStroke(aKeyEventAction.getKeyEvent(), aKeyEventAction.getKeyModifier()), aKeyEventAction.getName()); getActionMap().put(aKeyEventAction.getName(), aKeyEventAction); } /** * initialization of "default" keybindings. As default JTextArea that MJTextArea inherits * from does not contain all handy shortcuts, they can be added here */ private void initializeKeyboardBindings() { // add Ctrl-Backspace binding to delete the last word addKeyboardBindingAction(new KeyBindingAction("ctrlBackspace", java.awt.event.KeyEvent.VK_BACK_SPACE, java.awt.Event.CTRL_MASK) { public void actionPerformed(ActionEvent e) { int lastSpaceIndex = getText().lastIndexOf(' '); int lastNLIndex = getText().lastIndexOf('\n'); String modifiedTextContents = ""; //modifiedTextContents. if (lastSpaceIndex != -1 || lastNLIndex != -1) // if this is not the last word left { int lastIndex = (lastSpaceIndex > lastNLIndex ? lastSpaceIndex : lastNLIndex); modifiedTextContents = getText().substring(0, lastIndex + 1 ); } setText(modifiedTextContents); setCaretPosition(modifiedTextContents.length()); repaint(); } }); } /** * simple abstract class to encapsulate key bindings and their actions. The reason for it is * to have all the information on key binding: key event, modifier and the action in one place */ abstract class KeyBindingAction extends AbstractAction { private int keyEvent; private int keyModifier; public KeyBindingAction(String name, int aKeyEvent, int aKeyModifier) { super(name); keyEvent = aKeyEvent; keyModifier = aKeyModifier; } public int getKeyEvent() { return keyEvent; } public int getKeyModifier() { return keyModifier; } public String getName() { return (String) getValue(Action.NAME); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -