- 浏览: 240785 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
lliiqiang:
程序实例是指同一个程序不能运行2次?怎么判断同一个程序?其实你 ...
让你的Java程序只有一个进程实例在运行 -
noahweb:
你好! 这篇文章让我获益匪浅;同时我有个问题想要问您:http ...
JAVA界面设计大全----JTabbedPane,JScroolPane,JScrolBa的使用 -
meteormatt:
引用所以当你是用equals方法判断对象的内容是否相等,请不要 ...
"=="和equals方法究竟有什么区别? -
zhhui_syist:
...
监听整理----JTable
/* HTMLDocumentEditor.java
* @author: Charles Bell
* @version: May 27, 2002
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.undo.*;
/**HTML文件文档编辑器*/
public class HTMLDocumentEditor extends JFrame implements ActionListener
{
/** 声明一个网页文档对象变量*/
private HTMLDocument document;
/** 创建一个文本编辑板*/
private JTextPane textPane = new JTextPane();
private boolean debug = false;
/** 声明一个文件对象变量*/
private File currentFile;
/** 侦听在当前文档上的编辑器 */
protected UndoableEditListener undoHandler = new UndoHandler();
/** 添加撤消管理器 */
protected UndoManager undo = new UndoManager();
/** 添加撤消侦听器*/
private UndoAction undoAction = new UndoAction();
/** 添加恢复侦听器*/
private RedoAction redoAction = new RedoAction();
/** 添加剪切侦听器*/
private Action cutAction = new DefaultEditorKit.CutAction();
/** 添加复制侦听器*/
private Action copyAction = new DefaultEditorKit.CopyAction();
/** 添加粘贴侦听器*/
private Action pasteAction = new DefaultEditorKit.PasteAction();
/** 添加加粗侦听器*/
private Action boldAction = new StyledEditorKit.BoldAction();
/** 添加加下划线侦听器*/
private Action underlineAction = new StyledEditorKit.UnderlineAction();
/** 添加倾斜侦听器*/
private Action italicAction = new StyledEditorKit.ItalicAction();
private Action insertBreakAction = new DefaultEditorKit.InsertBreakAction();
private HTMLEditorKit.InsertHTMLTextAction unorderedListAction = new HTMLEditorKit.InsertHTMLTextAction("Bullets", "<ul><li> </li></ul>",HTML.Tag.P,HTML.Tag.UL);
private HTMLEditorKit.InsertHTMLTextAction bulletAction = new HTMLEditorKit.InsertHTMLTextAction("Bullets", "<li> </li>",HTML.Tag.UL,HTML.Tag.LI);
/** 构造方法*/
public HTMLDocumentEditor()
{
/** 设置主窗体标题*/
super("HTMLDocumentEditor");
HTMLEditorKit editorKit = new HTMLEditorKit();
/** 创建默认文档指向网页引用document*/
document = (HTMLDocument)editorKit.createDefaultDocument();
// 强制SWINGSET实现跨平台,不改变风格
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
// 如果你想用系统的界面风格替代,请注释掉上一行代码,而取消下一行代码的注释:
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception exc)
{
//产生异常,则显示错误消息:加载L&F错误
System.err.println("Error loading L&F: " + exc);
}
//调用初始化方法
init();
}
/**主方法,起动程序*/
public static void main(String[] args)
{
//创建一个类的实例,即创建一个网页编辑器
HTMLDocumentEditor editor = new HTMLDocumentEditor();
}
/**初始化各组件的方法*/
public void init()
{
//调用自定义继承WindowListener的侦听器FrameListener,给主窗体添加WindowListener
addWindowListener(new FrameListener());
JMenuBar menuBar = new JMenuBar();
getContentPane().add(menuBar, BorderLayout.NORTH);
JMenu fileMenu = new JMenu("File"); //文件
JMenu editMenu = new JMenu("Edit"); //编辑
JMenu colorMenu = new JMenu("Color"); //颜色
JMenu fontMenu = new JMenu("Font"); //字体
JMenu styleMenu = new JMenu("Style"); //样式
JMenu alignMenu = new JMenu("Align"); //对齐
JMenu helpMenu = new JMenu("Help"); //帮助
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(colorMenu);
menuBar.add(fontMenu);
menuBar.add(styleMenu);
menuBar.add(alignMenu);
menuBar.add(helpMenu);
JMenuItem newItem = new JMenuItem("New", new ImageIcon("whatsnew-bang.gif")); //新建
JMenuItem openItem = new JMenuItem("Open",new ImageIcon("open.gif")); //打开
JMenuItem saveItem = new JMenuItem("Save",new ImageIcon("save.gif")); //保存
JMenuItem saveAsItem = new JMenuItem("Save As"); //另存
JMenuItem exitItem = new JMenuItem("Exit",new ImageIcon("exit.gif")); //退出
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
saveAsItem.addActionListener(this);
exitItem.addActionListener(this);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.add(exitItem);
//给菜单项添加侦听器
JMenuItem undoItem = new JMenuItem(undoAction); //撤消
JMenuItem redoItem = new JMenuItem(redoAction); //恢复
JMenuItem cutItem = new JMenuItem(cutAction); //剪切
JMenuItem copyItem = new JMenuItem(copyAction); //复制
JMenuItem pasteItem = new JMenuItem(pasteAction); //粘贴
JMenuItem clearItem = new JMenuItem("Clear"); //清除
JMenuItem selectAllItem = new JMenuItem("Select All"); //全选
JMenuItem insertBreaKItem = new JMenuItem(insertBreakAction);
JMenuItem unorderedListItem = new JMenuItem(unorderedListAction);
JMenuItem bulletItem = new JMenuItem(bulletAction); //项目符号
cutItem.setText("Cut");
copyItem.setText("Copy");
pasteItem.setText("Paste");
insertBreaKItem.setText("Break");
cutItem.setIcon(new ImageIcon("cut.gif"));
copyItem.setIcon(new ImageIcon("copy.gif"));
pasteItem.setIcon(new ImageIcon("paste.gif"));
insertBreaKItem.setIcon(new ImageIcon("break.gif"));
unorderedListItem.setIcon(new ImageIcon("bullets.gif"));
clearItem.addActionListener(this);
selectAllItem.addActionListener(this);
editMenu.add(undoItem);
editMenu.add(redoItem);
editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
editMenu.add(clearItem);
editMenu.add(selectAllItem);
editMenu.add(insertBreaKItem);
editMenu.add(unorderedListItem);
editMenu.add(bulletItem);
JMenuItem redTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Red",Color.red));
JMenuItem orangeTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Orange",Color.orange));
JMenuItem yellowTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Yellow",Color.yellow));
JMenuItem greenTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Green",Color.green));
JMenuItem blueTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Blue",Color.blue));
JMenuItem cyanTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Cyan",Color.cyan));
JMenuItem magentaTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Magenta",Color.magenta));
JMenuItem blackTextItem = new JMenuItem(new StyledEditorKit.ForegroundAction("Black",Color.black));
redTextItem.setIcon(new ImageIcon("red.gif"));
orangeTextItem.setIcon(new ImageIcon("orange.gif"));
yellowTextItem.setIcon(new ImageIcon("yellow.gif"));
greenTextItem.setIcon(new ImageIcon("green.gif"));
blueTextItem.setIcon(new ImageIcon("blue.gif"));
cyanTextItem.setIcon(new ImageIcon("cyan.gif"));
magentaTextItem.setIcon(new ImageIcon("magenta.gif"));
blackTextItem.setIcon(new ImageIcon("black.gif"));
colorMenu.add(redTextItem);
colorMenu.add(orangeTextItem);
colorMenu.add(yellowTextItem);
colorMenu.add(greenTextItem);
colorMenu.add(blueTextItem);
colorMenu.add(cyanTextItem);
colorMenu.add(magentaTextItem);
colorMenu.add(blackTextItem);
JMenu fontTypeMenu = new JMenu("Font Type");
fontMenu.add(fontTypeMenu);
String[] fontTypes = {"SansSerif", "Serif", "Monospaced", "Dialog", "DialogInput"};
for (int i = 0; i < fontTypes.length;i++)
{
if (debug) System.out.println(fontTypes[i]);
JMenuItem nextTypeItem = new JMenuItem(fontTypes[i]);
nextTypeItem.setAction(new StyledEditorKit.FontFamilyAction(fontTypes[i], fontTypes[i]));
fontTypeMenu.add(nextTypeItem);
}
JMenu fontSizeMenu = new JMenu("Font Size");
fontMenu.add(fontSizeMenu);
int[] fontSizes = {6, 8,10,12,14, 16, 20,24, 32,36,48,72};
for (int i = 0; i < fontSizes.length;i++)
{
if (debug) System.out.println(fontSizes[i]);
JMenuItem nextSizeItem = new JMenuItem(String.valueOf(fontSizes[i]));
nextSizeItem.setAction(new StyledEditorKit.FontSizeAction(String.valueOf(fontSizes[i]), fontSizes[i]));
fontSizeMenu.add(nextSizeItem);
}
JMenuItem boldMenuItem = new JMenuItem(boldAction);
JMenuItem underlineMenuItem = new JMenuItem(underlineAction);
JMenuItem italicMenuItem = new JMenuItem(italicAction);
boldMenuItem.setText("Bold");
underlineMenuItem.setText("Underline");
italicMenuItem.setText("Italic");
boldMenuItem.setIcon(new ImageIcon("bold.gif"));
underlineMenuItem.setIcon(new ImageIcon("underline.gif"));
italicMenuItem.setIcon(new ImageIcon("italic.gif"));
styleMenu.add(boldMenuItem);
styleMenu.add(underlineMenuItem);
styleMenu.add(italicMenuItem);
JMenuItem subscriptMenuItem = new JMenuItem(new SubscriptAction());
JMenuItem superscriptMenuItem = new JMenuItem(new SuperscriptAction());
JMenuItem strikeThroughMenuItem = new JMenuItem(new StrikeThroughAction());
subscriptMenuItem.setText("Subscript");
superscriptMenuItem.setText("Superscript");
strikeThroughMenuItem.setText("StrikeThrough");
subscriptMenuItem.setIcon(new ImageIcon("subscript.gif"));
superscriptMenuItem.setIcon(new ImageIcon("superscript.gif"));
strikeThroughMenuItem.setIcon(new ImageIcon("strikethough.gif"));
styleMenu.add(subscriptMenuItem);
styleMenu.add(superscriptMenuItem);
styleMenu.add(strikeThroughMenuItem);
JMenuItem leftAlignMenuItem = new JMenuItem(new StyledEditorKit.AlignmentAction("Left Align",StyleConstants.ALIGN_LEFT));
JMenuItem centerMenuItem = new JMenuItem(new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER));
JMenuItem rightAlignMenuItem = new JMenuItem(new StyledEditorKit.AlignmentAction ("Right Align",StyleConstants.ALIGN_RIGHT));
leftAlignMenuItem.setText("Left Align");
centerMenuItem.setText("Center");
rightAlignMenuItem.setText("Right Align");
leftAlignMenuItem.setIcon(new ImageIcon("left.gif"));
centerMenuItem.setIcon(new ImageIcon("center.gif"));
rightAlignMenuItem.setIcon(new ImageIcon("right.gif"));
alignMenu.add(leftAlignMenuItem);
alignMenu.add(centerMenuItem);
alignMenu.add(rightAlignMenuItem);
JMenuItem helpItem = new JMenuItem("帮助");
helpItem.addActionListener(this);
helpMenu.add(helpItem);
JMenuItem shortcutsItem = new JMenuItem("Keyboard Shortcuts");
shortcutsItem.addActionListener(this);
helpMenu.add(shortcutsItem);
JMenuItem aboutItem = new JMenuItem("About QuantumHyperSpace");
aboutItem.addActionListener(this);
helpMenu.add(aboutItem);
JPanel editorControlPanel = new JPanel();
//editorControlPanel.setLayout(new GridLayout(3,3));
editorControlPanel.setLayout(new FlowLayout());
/* 按钮 */
JButton cutButton = new JButton(cutAction); //创建“剪切”按钮,添加剪切侦听
JButton copyButton = new JButton(copyAction); //创建“复制”按钮,添加复制侦
JButton pasteButton = new JButton(pasteAction); //创建“粘贴”按钮,添加粘贴侦
JButton boldButton = new JButton(boldAction); //创建“加粗”按钮,添加加粗侦听
JButton underlineButton = new JButton(underlineAction); //创建“下划线”按钮,添加下划线侦听
JButton italicButton = new JButton(italicAction); //创建“斜体”按钮,添加斜体侦听
//JButton insertButton = new JButton(insertAction);
//JButton insertBreakButton = new JButton(insertBreakAction);
//JButton tabButton = new JButton(tabAction);
cutButton.setText("Cut");
copyButton.setText("Copy");
pasteButton.setText("Paste");
boldButton.setText("Bold");
underlineButton.setText("Underline");
italicButton.setText("Italic");
//insertButton.setText("Insert");
//insertBreakButton.setText("Insert Break");
//tabButton.setText("Tab");
cutButton.setIcon(new ImageIcon("cut.gif"));
copyButton.setIcon(new ImageIcon("copy.gif"));
pasteButton.setIcon(new ImageIcon("paste.gif"));
boldButton.setIcon(new ImageIcon("bold.gif"));
underlineButton.setIcon(new ImageIcon("underline.gif"));
italicButton.setIcon(new ImageIcon("italic.gif"));
editorControlPanel.add(cutButton);
editorControlPanel.add(copyButton);
editorControlPanel.add(pasteButton);
editorControlPanel.add(boldButton);
editorControlPanel.add(underlineButton);
editorControlPanel.add(italicButton);
//editorControlPanel.add(insertButton);
//editorControlPanel.add(insertBreakButton);
//editorControlPanel.add(tabButton);
JButton subscriptButton = new JButton(new SubscriptAction());
JButton superscriptButton = new JButton(new SuperscriptAction());
JButton strikeThroughButton = new JButton(new StrikeThroughAction());
subscriptButton.setIcon(new ImageIcon("subscript.gif"));
superscriptButton.setIcon(new ImageIcon("superscript.gif"));
strikeThroughButton.setIcon(new ImageIcon("strikethough.gif"));
JPanel specialPanel = new JPanel();
specialPanel.setLayout(new FlowLayout());
specialPanel.add(subscriptButton);
specialPanel.add(superscriptButton);
specialPanel.add(strikeThroughButton);
//JButton leftAlignButton = new JButton(new AlignLeftAction());
//JButton centerButton = new JButton(new CenterAction());
//JButton rightAlignButton = new JButton(new AlignRightAction());
JButton leftAlignButton = new JButton(new StyledEditorKit.AlignmentAction("Left Align",StyleConstants.ALIGN_LEFT));
JButton centerButton = new JButton(new StyledEditorKit.AlignmentAction("Center",StyleConstants.ALIGN_CENTER));
JButton rightAlignButton = new JButton(new StyledEditorKit.AlignmentAction ("Right Align",StyleConstants.ALIGN_RIGHT));
JButton colorButton = new JButton(new StyledEditorKit.AlignmentAction ("Right Align",StyleConstants.ALIGN_RIGHT));
leftAlignButton.setIcon(new ImageIcon("left.gif"));
centerButton.setIcon(new ImageIcon("center.gif"));
rightAlignButton.setIcon(new ImageIcon("right.gif"));
colorButton.setIcon(new ImageIcon("color.gif"));
leftAlignButton.setText("Left Align");
centerButton.setText("Center");
rightAlignButton.setText("Right Align");
JPanel alignPanel = new JPanel();
alignPanel.setLayout(new FlowLayout());
alignPanel.add(leftAlignButton);
alignPanel.add(centerButton);
alignPanel.add(rightAlignButton);
document.addUndoableEditListener(undoHandler);
resetUndoManager();
textPane = new JTextPane(document);
textPane.setContentType("text/html");
JScrollPane scrollPane = new JScrollPane(textPane);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension scrollPaneSize = new Dimension(5*screenSize.width/8,5*screenSize.height/8);
scrollPane.setPreferredSize(scrollPaneSize);
//创建工具栏面板,并设置面板布局管理器,添加子面板
JPanel toolPanel = new JPanel();
toolPanel.setLayout(new BorderLayout());
toolPanel.add(editorControlPanel, BorderLayout.NORTH);
toolPanel.add(specialPanel, BorderLayout.CENTER);
toolPanel.add(alignPanel, BorderLayout.SOUTH);
//向主窗体添加菜单栏
getContentPane().add(menuBar, BorderLayout.NORTH);
//向主窗体添加工具栏
getContentPane().add(toolPanel, BorderLayout.CENTER);
//向主窗体添加滚动面板
getContentPane().add(scrollPane, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
startNewDocument();
show();
}
public void actionPerformed(ActionEvent ae)
{
String actionCommand = ae.getActionCommand();
if (debug)
{
int modifier = ae.getModifiers();
long when = ae.getWhen();
String parameter = ae.paramString();
System.out.println("actionCommand: " + actionCommand);
System.out.println("modifier: " + modifier);
System.out.println("when: " + when);
System.out.println("parameter: " + parameter);
}
if (actionCommand.compareTo("New") == 0)
{
startNewDocument();
}
else if (actionCommand.compareTo("Open") == 0)
{
openDocument();
}
else if (actionCommand.compareTo("Save") == 0)
{
saveDocument();
}
else if (actionCommand.compareTo("Save As") == 0)
{
saveDocumentAs();
}
else if (actionCommand.compareTo("Exit") == 0)
{
exit();
}
else if (actionCommand.compareTo("Clear") == 0)
{
clear();
}
else if (actionCommand.compareTo("Select All") == 0)
{
selectAll();
}
else if (actionCommand.compareTo("帮助") == 0)
{
help();
}
else if (actionCommand.compareTo("Keyboard Shortcuts") == 0)
{
showShortcuts();
}
else if (actionCommand.compareTo("About QuantumHyperSpace") == 0)
{
aboutQuantumHyperSpace();
}
}
public void startNewDocument()
{
Document oldDoc = textPane.getDocument();
if(oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument)editorKit.createDefaultDocument();
textPane.setDocument(document);
currentFile = null;
setTitle("HTMLDocumentEditor");
textPane.getDocument().addUndoableEditListener(undoHandler);
resetUndoManager();
}
public void openDocument()
{
try
{
File current = new File(".");
JFileChooser chooser = new JFileChooser(current);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new HTMLFileFilter());
int approval = chooser.showSaveDialog(this);
if (approval == JFileChooser.APPROVE_OPTION)
{
currentFile = chooser.getSelectedFile();
setTitle(currentFile.getName());
FileReader fr = new FileReader(currentFile);
Document oldDoc = textPane.getDocument();
if(oldDoc != null)
oldDoc.removeUndoableEditListener(undoHandler);
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument)editorKit.createDefaultDocument();
editorKit.read(fr,document,0);
document.addUndoableEditListener(undoHandler);
textPane.setDocument(document);
resetUndoManager();
}
}
catch(BadLocationException ble)
{
System.err.println("BadLocationException: " + ble.getMessage());
}
catch(FileNotFoundException fnfe)
{
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
}
public void saveDocument()
{
if (currentFile != null)
{
try
{
FileWriter fw = new FileWriter(currentFile);
fw.write(textPane.getText());
fw.close();
}
catch(FileNotFoundException fnfe)
{
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
}
else
{
saveDocumentAs();
}
}
public void saveDocumentAs()
{
try
{
File current = new File(".");
JFileChooser chooser = new JFileChooser(current);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new HTMLFileFilter());
int approval = chooser.showSaveDialog(this);
if (approval == JFileChooser.APPROVE_OPTION)
{
File newFile = chooser.getSelectedFile();
if (newFile.exists())
{
String message = newFile.getAbsolutePath() + " already exists. \n" + "Do you want to replace it?";
if (JOptionPane.showConfirmDialog(this, message) == JOptionPane.YES_OPTION)
{
currentFile = newFile;
setTitle(currentFile.getName());
FileWriter fw = new FileWriter(currentFile);
fw.write(textPane.getText());
fw.close();
if (debug) System.out.println("Saved " + currentFile.getAbsolutePath());
}
}
else
{
currentFile = new File(newFile.getAbsolutePath());
setTitle(currentFile.getName());
FileWriter fw = new FileWriter(currentFile);
fw.write(textPane.getText());
fw.close();
if (debug) System.out.println("Saved " + currentFile.getAbsolutePath());
}
}
}
catch(FileNotFoundException fnfe)
{
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}
catch(IOException ioe)
{
System.err.println("IOException: " + ioe.getMessage());
}
}
public void exit()
{
String exitMessage = "Are you sure you want to exit?";
if (JOptionPane.showConfirmDialog(this, exitMessage) == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
/**调用startNewDocument()方法,清除当前文本,开始一个新文档*/
public void clear()
{
startNewDocument();
}
/**调用JTextPane的全选方法*/
public void selectAll()
{
textPane.selectAll();
}
/**利用消息框显示帮助信息*/
public void help()
{
JOptionPane.showMessageDialog(this,"DocumentEditor.java\n" + "Author: Charles Bell\n" + "Version: May 25, 2002\n" +
"http://www.quantumhyperspace.com\n" + "QuantumHyperSpace Programming Services");
}
/**利用消息框显示快捷键*/
public void showShortcuts()
{
String shortcuts = "Navigate in | Tab\n" + "Navigate out | Ctrl+Tab\n" + "Navigate out backwards | Shift+Ctrl+Tab\n" +
"Move up/down a line | Up/Down Arrown\n" + "Move left/right a component or char | Left/Right Arrow\n" +
"Move up/down one vertical block | PgUp/PgDn\n" + "Move to start/end of line | Home/End\n" +
"Move to previous/next word | Ctrl+Left/Right Arrow\n" + "Move to start/end of data | Ctrl+Home/End\n" +
"Move left/right one block | Ctrl+PgUp/PgDn\n" + "Select All | Ctrl+A\n" +
"Extend selection up one line | Shift+Up Arrow\n" + "Extend selection down one line | Shift+Down Arrow\n" +
"Extend selection to beginning of line | Shift+Home\n" + "Extend selection to end of line | Shift+End\n" +
"Extend selection to beginning of data | Ctrl+Shift+Home\n" + "Extend selection to end of data | Ctrl+Shift+End\n" +
"Extend selection left | Shift+Right Arrow\n" + "Extend selection right | Shift+Right Arrow\n" +
"Extend selection up one vertical block | Shift+PgUp\n" + "Extend selection down one vertical block | Shift+PgDn\n" +
"Extend selection left one block | Ctrl+Shift+PgUp\n" + "Extend selection right one block | Ctrl+Shift+PgDn\n" +
"Extend selection left one word | Ctrl+Shift+Left Arrow\n" + "Extend selection right one word | Ctrl+Shift+Right Arrow\n";
JOptionPane.showMessageDialog(this,shortcuts);
}
public void aboutQuantumHyperSpace()
{
JOptionPane.showMessageDialog(this,"QuantumHyperSpace Programming Services\n" + "http://www.quantumhyperspace.com\n" +
"email: support@quantumhyperspace.com\n" + " or \n" + "email: charles@quantumhyperspace.com\n",
"QuantumHyperSpace",JOptionPane.INFORMATION_MESSAGE, new ImageIcon("quantumhyperspace.gif"));
}
/**内部类:自定义继承WindowListener的侦听器FrameListener*/
class FrameListener extends WindowAdapter
{
/**处理点击窗体关闭按钮事件,实现程序的关闭停止*/
public void windowClosing(WindowEvent we)
{
exit();
}
}
class SubscriptAction extends StyledEditorKit.StyledTextAction
{
public SubscriptAction()
{
super(StyleConstants.Subscript.toString());
}
public void actionPerformed(ActionEvent ae)
{
JEditorPane editor = getEditor(ae);
if (editor != null)
{
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean subscript = (StyleConstants.isSubscript(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setSubscript(sas, subscript);
setCharacterAttributes(editor, sas, false);
}
}
}
class SuperscriptAction extends StyledEditorKit.StyledTextAction
{
public SuperscriptAction()
{
super(StyleConstants.Superscript.toString());
}
public void actionPerformed(ActionEvent ae)
{
JEditorPane editor = getEditor(ae);
if (editor != null)
{
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean superscript = (StyleConstants.isSuperscript(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setSuperscript(sas, superscript);
setCharacterAttributes(editor, sas, false);
}
}
}
class StrikeThroughAction extends StyledEditorKit.StyledTextAction
{
public StrikeThroughAction()
{
super(StyleConstants.StrikeThrough.toString());
}
public void actionPerformed(ActionEvent ae)
{
JEditorPane editor = getEditor(ae);
if (editor != null)
{
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean strikeThrough = (StyleConstants.isStrikeThrough(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setStrikeThrough(sas, strikeThrough);
setCharacterAttributes(editor, sas, false);
}
}
}
class HTMLFileFilter extends javax.swing.filechooser.FileFilter
{
public boolean accept(File f)
{
return ((f.isDirectory()) ||(f.getName().toLowerCase().indexOf(".htm") > 0));
}
public String getDescription()
{
return "html";
}
}
protected void resetUndoManager()
{
undo.discardAllEdits();
undoAction.update();
redoAction.update();
}
class UndoHandler implements UndoableEditListener
{
/**
* Messaged when the Document has created an edit, the edit is
* added to <code>undo</code>, an instance of UndoManager.
*/
public void undoableEditHappened(UndoableEditEvent e)
{
undo.addEdit(e.getEdit());
undoAction.update();
redoAction.update();
}
}
class UndoAction extends AbstractAction
{
public UndoAction()
{
super("Undo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e)
{
try
{
undo.undo();
}
catch (CannotUndoException ex)
{
System.out.println("Unable to undo: " + ex);
ex.printStackTrace();
}
update();
redoAction.update();
}
protected void update()
{
if(undo.canUndo())
{
setEnabled(true);
putValue(Action.NAME, undo.getUndoPresentationName());
}
else
{
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}
class RedoAction extends AbstractAction
{
public RedoAction()
{
super("Redo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e)
{
try
{
undo.redo();
}
catch (CannotRedoException ex)
{
System.err.println("Unable to redo: " + ex);
ex.printStackTrace();
}
update();
undoAction.update();
}
protected void update()
{
if(undo.canRedo())
{
setEnabled(true);
putValue(Action.NAME, undo.getRedoPresentationName());
}
else
{
setEnabled(false);
putValue(Action.NAME, "Redo");
}
}
}
}
相关推荐
4. **文件I/O操作**: 文本编辑器需要读取和保存文件。Java的`java.io`包提供了File、FileReader、FileWriter、BufferedReader和BufferedWriter等类,用于进行文件的读写操作。 5. **文件对话框**: 用户需要打开或...
Java编辑器课程设计报告主要涵盖了如何使用Java编程语言构建一个类似于Editplus的图形界面文本编辑器,特别适合编辑Java源代码。设计过程中涉及到的技术和知识点包括: 1. **需求分析**: - 开放已有文件、保存...
### JAVA中的JTextPane:一个全面的解析与...通过掌握其基本用法和高级特性,开发者可以构建出功能强大且用户友好的文本编辑器或视图组件。本篇文章仅为`JTextPane`的入门介绍,更多高级特性和细节还需深入学习和实践。
在Java中,创建文本编辑器的关键在于构建一个用户友好的界面,这通常使用Java的Swing或JavaFX库完成。Swing提供了许多组件,如JFrame用于创建窗口,JTextArea和JTextPane用于显示和编辑文本,以及JMenuBar和...
对于实现一个编辑器,JTextPane或JEditorPane更为合适,因为它们支持富文本格式,并且可以插入图像和其他多媒体内容。 语法高亮是提高代码可读性和编辑器用户体验的关键特性。在Swing中实现语法高亮,通常涉及到...
通过上述内容,我们可以了解到Java文本编辑器的核心技术,包括关键字高亮、文档模型管理、文档监听器等。掌握了这些技术后,就可以开发出一个功能完善且用户体验良好的文本编辑器了。需要注意的是,实际开发中还需要...
Java编写的Java简易编辑器是一款基于Java语言开发的文本编辑工具,主要针对Java程序员,提供基本的代码编辑、编译和执行功能。这个编辑器旨在简化小型Java项目的开发流程,帮助初学者或开发者快速测试代码片段,而...
Java文档编辑器是一个基于Swing库的文本编辑应用程序,其核心功能是处理DefaultStyledDocument对象,这是Swing中用于管理文本组件内容的数据结构。这个编辑器允许用户编辑文本,改变文本样式,并能将内容保存至磁盘...
在编程世界中,一个...总的来说,Java实现的编辑器语法着色涉及了文本解析、正则表达式、GUI渲染、事件监听等多个方面的知识。通过这些技术的综合运用,我们可以构建出功能强大的代码编辑器,提高程序员的工作效率。
在本文中,我们将深入探讨如何使用Java来设计一个简单的文本编辑器——MiniEditor。这个项目旨在帮助开发者了解Java图形用户界面(GUI)开发的基本概念,同时体验如何将这些概念应用于实际应用中。让我们一起探索...
在IT行业中,文本编辑器是开发者日常工作中不可或缺的工具,尤其对于Java程序员而言更是如此。一个优秀的文本编辑器不仅能够提供基本的文本编辑功能,还能支持代码高亮、自动完成、错误检查等特性,极大地提高了编程...
在Java GUI编程中,`JTextPane`是一个...这个功能在代码编辑器、文本编辑器或任何需要突出显示特定信息的应用场景中都非常有用。在实际项目中,你可能还需要处理更多细节,比如处理文本格式的保持、撤销/重做操作等。
总结来说,MyEditor是一个基于Java Swing构建的文本编辑器,其特点是拥有JMenu驱动的用户界面,支持磁盘文件的打开和保存,以及丰富的界面编辑功能。通过理解和掌握其背后的编程技术,开发者可以进一步提升自己的...
通过`EditorMDI`,开发者可以深入理解Java Swing框架中的组件使用、事件处理、文件操作等关键概念,同时也可以作为构建自定义文本编辑器或集成开发环境(IDE)的基础。这个项目不仅展示了Swing GUI编程的基本原理,...
【通用源代码编辑器JAVA大作业报告】是一个关于使用JAVA语言开发通用源代码编辑器的课程大作业。这个编辑器旨在提供基础的源代码编辑功能,例如文件打开、编辑和保存,同时也具备了一些高级特性,如语法高亮、剪贴板...
此外,还提到了“Emacs”,这通常指的是Emacs编辑器,一个强大的文本编辑器,广泛用于编程和其他文本编辑任务。 综上所述,文件“Java程序编辑器.pdf”主要涉及了Java开发环境、IDE工具、编码标准、GUI编程、文本...
1. **文本编辑器选择**:为了实现语法高亮功能,使用`JTextPane`或`JEditorPane`作为文本编辑区域的组件,因为它们使用`StyledDocument`,支持多种格式的文本着色。 2. **关键字染色**:通过扩展`...
【标题】"editedit"可能是指一个轻量级的文本编辑软件,它的名称可能是由于输入...这两个元素结合在一起,可以提供一个全面了解和开发文本编辑器的平台,不仅包含基本的文本编辑功能,还可能涉及更复杂的富文本处理。
总的来说,`swingTextEditor`项目是一个实践Java Swing GUI编程的好例子,它展示了如何使用Swing组件创建一个简单的文本编辑器,涵盖了从基础组件到事件处理、文件操作以及文本格式化的各种功能。通过学习这个项目,...