java swing 文本编辑器_Java Swing - 如何使用JTextArea和DefaultEditorKit创建文本编辑器...

/*

Java Swing, 2nd Edition

By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole

ISBN: 0-596-00408-7

Publisher: O'Reilly

*/

// Main.java

//An example showing several DefaultEditorKit features. This class is designed

//to be easily extended for additional functionality.

//

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Hashtable;

import javax.swing.AbstractAction;

import javax.swing.Action;

import javax.swing.ImageIcon;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JOptionPane;

import javax.swing.JTextArea;

import javax.swing.JToolBar;

import javax.swing.text.DefaultEditorKit;

import javax.swing.text.JTextComponent;

public class Main extends JFrame {

private Action openAction = new OpenAction();

private Action saveAction = new SaveAction();

private JTextComponent textComp;

private Hashtable actionHash = new Hashtable();

public static void main(String[] args) {

Main editor = new Main();

editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

editor.setVisible(true);

}

// Create an editor.

public Main() {

super("Swing Editor");

textComp = createTextComponent();

makeActionsPretty();

Container content = getContentPane();

content.add(textComp, BorderLayout.CENTER);

content.add(createToolBar(), BorderLayout.NORTH);

setJMenuBar(createMenuBar());

setSize(320, 240);

}

// Create the JTextComponent subclass.

protected JTextComponent createTextComponent() {

JTextArea ta = new JTextArea();

ta.setLineWrap(true);

return ta;

}

// Add icons and friendly names to actions we care about.

protected void makeActionsPretty() {

Action a;

a = textComp.getActionMap().get(DefaultEditorKit.cutAction);

a.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));

a.putValue(Action.NAME, "Cut");

a = textComp.getActionMap().get(DefaultEditorKit.copyAction);

a.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));

a.putValue(Action.NAME, "Copy");

a = textComp.getActionMap().get(DefaultEditorKit.pasteAction);

a.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));

a.putValue(Action.NAME, "Paste");

a = textComp.getActionMap().get(DefaultEditorKit.selectAllAction);

a.putValue(Action.NAME, "Select All");

}

// Create a simple JToolBar with some buttons.

protected JToolBar createToolBar() {

JToolBar bar = new JToolBar();

// Add simple actions for opening & saving.

bar.add(getOpenAction()).setText("");

bar.add(getSaveAction()).setText("");

bar.addSeparator();

// Add cut/copy/paste buttons.

bar.add(textComp.getActionMap().get(DefaultEditorKit.cutAction))

.setText("");

bar.add(textComp.getActionMap().get(DefaultEditorKit.copyAction))

.setText("");

bar.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction))

.setText("");

return bar;

}

// Create a JMenuBar with file & edit menus.

protected JMenuBar createMenuBar() {

JMenuBar menubar = new JMenuBar();

JMenu file = new JMenu("File");

JMenu edit = new JMenu("Edit");

menubar.add(file);

menubar.add(edit);

file.add(getOpenAction());

file.add(getSaveAction());

file.add(new ExitAction());

edit.add(textComp.getActionMap().get(DefaultEditorKit.cutAction));

edit.add(textComp.getActionMap().get(DefaultEditorKit.copyAction));

edit.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction));

edit.add(textComp.getActionMap().get(DefaultEditorKit.selectAllAction));

return menubar;

}

// Subclass can override to use a different open action.

protected Action getOpenAction() {

return openAction;

}

// Subclass can override to use a different save action.

protected Action getSaveAction() {

return saveAction;

}

protected JTextComponent getTextComponent() {

return textComp;

}

// ********** ACTION INNER CLASSES ********** //

// A very simple exit action

public class ExitAction extends AbstractAction {

public ExitAction() {

super("Exit");

}

public void actionPerformed(ActionEvent ev) {

System.exit(0);

}

}

// An action that opens an existing file

class OpenAction extends AbstractAction {

public OpenAction() {

super("Open", new ImageIcon("icons/open.gif"));

}

// Query user for a filename and attempt to open and read the file into

// the

// text component.

public void actionPerformed(ActionEvent ev) {

JFileChooser chooser = new JFileChooser();

if (chooser.showOpenDialog(Main.this) != JFileChooser.APPROVE_OPTION)

return;

File file = chooser.getSelectedFile();

if (file == null)

return;

FileReader reader = null;

try {

reader = new FileReader(file);

textComp.read(reader, null);

} catch (IOException ex) {

JOptionPane.showMessageDialog(Main.this,

"File Not Found", "ERROR", JOptionPane.ERROR_MESSAGE);

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException x) {

}

}

}

}

}

// An action that saves the document to a file

class SaveAction extends AbstractAction {

public SaveAction() {

super("Save", new ImageIcon("icons/save.gif"));

}

// Query user for a filename and attempt to open and write the text

// component's content to the file.

public void actionPerformed(ActionEvent ev) {

JFileChooser chooser = new JFileChooser();

if (chooser.showSaveDialog(Main.this) != JFileChooser.APPROVE_OPTION)

return;

File file = chooser.getSelectedFile();

if (file == null)

return;

FileWriter writer = null;

try {

writer = new FileWriter(file);

textComp.write(writer);

} catch (IOException ex) {

JOptionPane.showMessageDialog(Main.this,

"File Not Saved", "ERROR", JOptionPane.ERROR_MESSAGE);

} finally {

if (writer != null) {

try {

writer.close();

} catch (IOException x) {

}

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值