【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了TextComponentDemo.java提供大家参考,希望对大家有所帮助!
import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.undo.*;
public class TextComponentDemo extends JFrame
{
JTextPane textPane;
LimitedStyledDocument lsd;
JTextArea changeLog;
String newline = "\n";
static final int MAX_CHARACTERS = 300;
Hashtable actions;
//undo helpers
protected UndoAction undoAction;
protected RedoAction redoAction;
protected UndoManager undo = new UndoManager();
public TextComponentDemo()
{
//some initial setup
super("TextComponentDemo");
//Create the document for the text area.
lsd = new LimitedStyledDocument(MAX_CHARACTERS);
//Create the text pane and configure it.
textPane = new JTextPane(lsd); //All right! No 60's jokes.
textPane.setCaretPosition(0);
textPane.setMargin(new Insets(5,5,5,5));
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(200, 200));
//Create the text area for the status log and configure it.
changeLog = new JTextArea(5, 30);
changeLog.setEditable(false);
JScrollPane scrollPaneForLog = new JScrollPane(changeLog);
//Create a split pane for the change log and the text area.
JSplitPane splitPane = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
scrollPane, scrollPaneForLog);
splitPane.setOneTouchExpandable(true);
//Create the status area.
JPanel statusPane = new JPanel(new GridLayout(1, 1));
CaretListenerLabel caretListenerLabel =
new CaretListenerLabel("Caret Status");
statusPane.add(caretListenerLabel);
//Add the components to the frame.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(splitPane, BorderLayout.CENTER);
contentPane.add(statusPane, BorderLayout.SOUTH);
setContentPane(contentPane);
//Set up the menu bar.
createActionTable(textPane);
JMenu editMenu = createEditMenu();
JMenu styleMenu = createStyleMenu();
JMenuBar mb = new JMenuBar();
mb.add(editMenu);
mb.add(styleMenu);
setJMenuBar(mb);
//Add some key bindings to the keymap.
addKeymapBindings();
//Put the initial text into the text pane.
initDocument();
//Start watching for undoable edits and caret changes.
lsd.addUndoableEditListener(new MyUndoableEditListener());
textPane.addCaretListener(caretListenerLabel);
lsd.addDocumentListener(new MyDocumentListener());
}
//This listens for and reports caret movements.
protected class CaretListenerLabel extends JLabel implements CaretListener {
public CaretListenerLabel (String label)
{
super(label);
}
public void caretUpdate(CaretEvent e)
{
//Get the location in the text.
int dot = e.getDot();
int mark = e.getMark();
if (dot == mark)
{ // no selection
try {
Rectangle caretCoords = textPane.modelToView(dot);
//Convert it to view coordinates.
setText("caret: text position: " + dot
+ ", view location = ["
+ caretCoords.x + ", "
+ caretCoords.y + "]"
+ newline);
} catch (BadLocationException ble)
{
setText("caret: text position: " + dot + newline);
}
}
else if (dot < mark)
{
setText("selection from: " + dot
+ " to " + mark + newline);
}
else
{
setText("selection from: " + mark
+ " to " + dot + newline);
}
}
}
//This one listens for edits that can be undone.
protected class MyUndoableEditListener
implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent e)
{
//Remember the edit and update the menus.
undo.addEdit(e.getEdit());
undoAction.updateUndoState();
redoAction.updateRedoState();
}
}
//And this one listens for any changes to the document.
protected class MyDocumentListener
implements DocumentListener {
public void insertUpdate(DocumentEvent e)
{
displayEditInfo(e);
}
public void removeUpdate(DocumentEvent e)
{
displayEditInfo(e);
}
public void changedUpdate(DocumentEvent e)
{
displayEditInfo(e);
}
private void displayEditInfo(DocumentEvent e)
{
Document doc = (Document)e.getDocument();
int changeLength = e.getLength();
changeLog.append(e.getType().toString() + ": " +
changeLength + " character" +
((changeLength == 1) ? ". " : "s. ") +
" Text length = " + doc.getLength() + "." + newline);
}
}
//Add a couple of emacs key bindings to the key map for navigation.
protected void addKeymapBindings()
{
//Add a new key map to the keymap hierarchy.
Keymap keymap = textPane.addKeymap("MyEmacsBindings",
textPane.getKeymap());
//Ctrl-b to go backward one character
Action action = getActionByName(DefaultEditorKit.backwardAction);
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK);
keymap.addActionForKeyStroke(key, action);
//Ctrl-f to go forward one character
action = getActionByName(DefaultEditorKit.forwardAction);
key = KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK);
keymap.addActionForKeyStroke(key, action);
//Ctrl-p to go up one line
action = getActionByName(DefaultEditorKit.upAction);
key = KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK);
keymap.addActionForKeyStroke(key, action);
//Ctrl-n to go down one line
action = getActionByName(DefaultEditorKit.downAction);
key = KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK);
keymap.addActionForKeyStroke(key, action);
textPane.setKeymap(keymap);
}
//Create the edit menu.
protected JMenu createEditMenu()
{
JMenu menu = new JMenu("Edit");
//Undo and redo are actions of our own creation.
undoAction = new UndoAction();
menu.add(undoAction);
redoAction = new RedoAction();
menu.add(redoAction);
menu.addSeparator();
//These actions come from the default editor kit.
//Get the ones we want and stick them in the menu.
menu.add(getActionByName(DefaultEditorKit.cutAction));
menu.add(getActionByName(DefaultEditorKit.copyAction));
menu.add(getActionByName(DefaultEditorKit.pasteAction));
menu.addSeparator();
menu.add(getActionByName(DefaultEditorKit.selectAllAction));
return menu;
}
//Create the style menu.
protected JMenu createStyleMenu()
{
JMenu menu = new JMenu("Style");
Action action = new StyledEditorKit.BoldAction();
action.putValue(Action.NAME, "Bold");
menu.add(action);
action = new StyledEditorKit.ItalicAction();
action.putValue(Action.NAME, "Italic");
menu.add(action);
action = new StyledEditorKit.UnderlineAction();
action.putValue(Action.NAME, "Underline");
menu.add(action);
menu.addSeparator();
menu.add(new StyledEditorKit.FontSizeAction("12", 12));
menu.add(new StyledEditorKit.FontSizeAction("14", 14));
menu.add(new St
上一篇:
TextCompare.java
下一篇:
国家公务员面试入门备考全指导