1 / 13

そこはかとなく JAVA(10) ~ 編集機能の追加 ~

そこはかとなく JAVA(10) ~ 編集機能の追加 ~. 原田 章. 内容. 編集機能の追加 コピー、切り取り、ペースト アンドゥ(やり直し) ショートカットキーの追加 Ctrl+C: コピー Ctrl+V: ペースト. 編集機能の追加. JEditorPane にははじめから編集機能用の関数あり JEditorPane.copy() JEditorPane.cut() JEditorPane.paste() 追加の方法は前回のファイルメニューと同じ. コピー機能追加. カットとペーストは、 copy を cut,paste に変更.

mayes
Download Presentation

そこはかとなく JAVA(10) ~ 編集機能の追加 ~

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. そこはかとなくJAVA(10)~編集機能の追加~ 原田 章

  2. 内容 • 編集機能の追加 • コピー、切り取り、ペースト • アンドゥ(やり直し) • ショートカットキーの追加 • Ctrl+C:コピー • Ctrl+V:ペースト

  3. 編集機能の追加 • JEditorPaneにははじめから編集機能用の関数あり • JEditorPane.copy() • JEditorPane.cut() • JEditorPane.paste() • 追加の方法は前回のファイルメニューと同じ

  4. コピー機能追加 • カットとペーストは、copyをcut,pasteに変更 class Copy extends AbstractAction{ Copy() { super( "Copy" ); } public void actionPerformed( ActionEvent evt ) { if ( editor != null ) editor.copy(); } }

  5. アンドゥ • UndoManagerクラス • アンドゥを管理する • JEditorPaneにこれを追加する • undo()メソッドの利用 import javax.swing.undo.*; UndoManager undo = new UndoManager(); JEditorPane editor = new JEditorPane(); editor.getDocument().addUndoableEditListener( undo );

  6. アンドゥ機能の追加 • Undoできなかったときのエラーをキャッチ class Undo extends AbstractAction { Undo() { super( "Undo" ); } public void actionPerformed( ActionEvent evt ) { try { undo.undo() } catch ( CannotUndoException error ) { // 何もしない } } }

  7. PseudoSAS2.java • PseudoSAS.javaを改良 • importに追加あり • メニュー追加部分を書き換え • 編集機能の追加 • コピー、カット、ペースト • アンドゥ

  8. ショートカットキーの追加 • Keymapクラス • 入力キーとイベントの対応表 • これにキーストロークとイベントの組み合わせを追加 • JEditorPane.getKeymap()で取得 • KeyStroke.getKeyStroke()メソッド • getKeyStroke( KeyEvent.VK_C, KeyEvent.CTRL_MASK ); • JEditorPaneの場合、あらかじめ設定されているようだ

  9. 登録済みのショートカット • Ctrl+X:切り取り • Ctrl+C:コピー • Ctrl+V:ペースト • Alt+F4:終了 • ほかにもあるかも….

  10. 追加メソッド private void addShortcutKey() { Keymap keymap = editor.getKeymap(); KeyStroke keyundo = KeyStroke.getKeyStroke( KeyEvent.VK_Z, KeyEvent.CTRL_MASK ); KeyStroke keynew = KeyStroke.getKeyStroke( KeyEvent.VK_N, KeyEvent.CTRL_MASK ); KeyStroke keyopen = KeyStroke.getKeyStroke( KeyEvent.VK_O, KeyEvent.CTRL_MASK ); KeyStroke keysave = KeyStroke.getKeyStroke( KeyEvent.VK_S, KeyEvent.CTRL_MASK ); keymap.addActionForKeyStroke( keyundo, new Undo() ); keymap.addActionForKeyStroke( keynew, new New() ); keymap.addActionForKeyStroke( keyopen, new Open() ); keymap.addActionForKeyStroke( keysave, new Save() ); editor.setKeymap( keymap ); }

  11. その他の変更 • Keymapクラスのためにjavax.swing.text.*をimport • コンストラクタに • addShortcutKey()を追加

  12. おまけ • setBounds( int x, int y, int width, int height ) • ウィンドウの場所と大きさを指定 • setSize( int width, int height )の代わりに使用 • クラス構造の整理 • EditorウィンドウとOutputウィンドウはほとんどおなじもの • 共通にできる部分は共通化

  13. PseudoSAS3.java関係 • PseudoSAS3.java • 本体 • PseudoSASFrame.java • ウィンドウの共通部分 • EditorWindow.java • プログラム編集ウィンドウ • OutputWindow.java • 出力ウィンドウ • 前回のReadFile.java, WriteFile.java, SASConnector.javaも必要

More Related