「JTextComponent 1」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==JTextComponent 1== [Jython JTextComponent 1][Jython Swing][Swing][Jython][Python] *http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#comma…」) |
|||
(同じ利用者による、間の3版が非表示) | |||
1行目: | 1行目: | ||
− | ==JTextComponent 1== | + | ==[[JTextComponent 1]]== |
− | [Jython JTextComponent 1][Jython Swing][Swing][Jython][Python] | + | [[Jython JTextComponent 1]] | [[Jython Swing]] | [[Swing]] | [[Jython]] | [[Python]] | |
*http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#commands | *http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#commands | ||
7行目: | 7行目: | ||
*メニューとアクションの割付 | *メニューとアクションの割付 | ||
*キーボード押下との割付 | *キーボード押下との割付 | ||
− | * | + | *Undo、[[R]]edoの実装 |
*Documentの利用 | *Documentの利用 | ||
*Documentのイベント感知 | *Documentのイベント感知 | ||
27行目: | 27行目: | ||
import javax.swing.Action; | import javax.swing.Action; | ||
import javax.swing.InputMap; | import javax.swing.InputMap; | ||
− | import javax.swing.JFrame; | + | import javax.swing.[[JFrame]]; |
− | import javax.swing. | + | import javax.swing.J[[Menu]]; |
− | import javax.swing. | + | import javax.swing.J[[Menu]]Bar; |
import javax.swing.JScrollPane; | import javax.swing.JScrollPane; | ||
import javax.swing.JTextArea; | import javax.swing.JTextArea; | ||
import javax.swing.KeyStroke; | import javax.swing.KeyStroke; | ||
− | import javax.swing. | + | import javax.swing.[[Swing]]Utilities; |
import javax.swing.event.UndoableEditEvent; | import javax.swing.event.UndoableEditEvent; | ||
import javax.swing.event.UndoableEditListener; | import javax.swing.event.UndoableEditListener; | ||
45行目: | 45行目: | ||
*/ | */ | ||
public class JTextTest { | public class JTextTest { | ||
− | private JFrame frame; | + | private [[JFrame]] frame; |
− | private Map | + | private Map<Object, Action> actionMap = new HashMap<Object, Action>(); |
private UndoManager undo = new UndoManager(); | private UndoManager undo = new UndoManager(); | ||
private UndoAction undoAction; | private UndoAction undoAction; | ||
− | private | + | private [[R]]edoAction redoAction; |
− | private | + | private J[[Menu]] get[[Menu]]() { |
− | + | J[[Menu]] menu = new J[[Menu]]("Edit"); | |
menu.add(actionMap.get(DefaultEditorKit.cutAction)); | menu.add(actionMap.get(DefaultEditorKit.cutAction)); | ||
64行目: | 64行目: | ||
menu.add(undoAction); | menu.add(undoAction); | ||
− | redoAction = new | + | redoAction = new [[R]]edoAction(); |
menu.add(redoAction); | menu.add(redoAction); | ||
79行目: | 79行目: | ||
} | } | ||
private void createUI() { | private void createUI() { | ||
− | frame = new JFrame("TextTest"); | + | frame = new [[JFrame]]("TextTest"); |
− | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | frame.setDefaultCloseOperation([[JFrame]].EXIT_ON_CLOSE); |
JTextArea txtArea = new JTextArea(10, 30); | JTextArea txtArea = new JTextArea(10, 30); | ||
− | // Documentのイベント感知(Undo | + | // Documentのイベント感知(Undo [[R]]edo 管理を行う) |
Document doc = txtArea.getDocument(); | Document doc = txtArea.getDocument(); | ||
doc.addUndoableEditListener( | doc.addUndoableEditListener( | ||
96行目: | 96行目: | ||
); | ); | ||
− | frame.getContentPane().add(new JScrollPane(txtArea), BorderLayout. | + | frame.getContentPane().add(new JScrollPane(txtArea), BorderLayout.CENTE[[R]]); |
// TextComponentが持つActionをMapに格納 | // TextComponentが持つActionをMapに格納 | ||
createActionMap(txtArea); | createActionMap(txtArea); | ||
// メニューを作成 | // メニューを作成 | ||
− | + | J[[Menu]] menu = get[[Menu]](); | |
− | + | J[[Menu]]Bar mb = new J[[Menu]]Bar(); | |
mb.add(menu); | mb.add(menu); | ||
− | frame. | + | frame.setJ[[Menu]]Bar(mb); |
− | // | + | // テキストのUndo、[[R]]edoにキーを割り当てる (Ctrl+z,Ctrl+y) |
InputMap inputMap = txtArea.getInputMap(); | InputMap inputMap = txtArea.getInputMap(); | ||
− | KeyStroke undoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event. | + | KeyStroke undoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CT[[R]]L_MASK); |
− | KeyStroke redoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event. | + | KeyStroke redoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CT[[R]]L_MASK); |
inputMap.put(undoKey, undoAction); | inputMap.put(undoKey, undoAction); | ||
inputMap.put(redoKey, redoAction); | inputMap.put(redoKey, redoAction); | ||
117行目: | 117行目: | ||
} | } | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
− | + | [[Swing]]Utilities.invokeLater( | |
− | new | + | new [[R]]unnable(){ |
public void run() { | public void run() { | ||
JTextTest jft = new JTextTest(); | JTextTest jft = new JTextTest(); | ||
140行目: | 140行目: | ||
} | } | ||
updateState(); | updateState(); | ||
− | // | + | // [[R]]edo を可能に |
redoAction.updateState(); | redoAction.updateState(); | ||
} | } | ||
149行目: | 149行目: | ||
@SuppressWarnings("serial") | @SuppressWarnings("serial") | ||
− | class | + | class [[R]]edoAction extends AbstractAction { |
− | public | + | public [[R]]edoAction() { |
− | super(" | + | super("[[R]]edo"); |
setEnabled(false); | setEnabled(false); | ||
} | } | ||
163行目: | 163行目: | ||
} | } | ||
public void updateState() { | public void updateState() { | ||
− | setEnabled(undo. | + | setEnabled(undo.can[[R]]edo()); |
} | } | ||
} | } | ||
} | } |
2020年2月16日 (日) 04:28時点における最新版
JTextComponent 1
Jython JTextComponent 1 | Jython Swing | Swing | Jython | Python |
内容
- メニューとアクションの割付
- キーボード押下との割付
- Undo、Redoの実装
- Documentの利用
- Documentのイベント感知
ソースコード
実行例
ソースコード
import java.awt.BorderLayout; import java.awt.Event; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.util.HashMap; import java.util.Map; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.InputMap; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import javax.swing.event.UndoableEditEvent; import javax.swing.event.UndoableEditListener; import javax.swing.text.DefaultEditorKit; import javax.swing.text.Document; import javax.swing.text.JTextComponent; import javax.swing.undo.UndoManager; /** * @see http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#commands */ public class JTextTest { private JFrame frame; private Map<Object, Action> actionMap = new HashMap<Object, Action>(); private UndoManager undo = new UndoManager(); private UndoAction undoAction; private RedoAction redoAction; private JMenu getMenu() { JMenu menu = new JMenu("Edit"); menu.add(actionMap.get(DefaultEditorKit.cutAction)); menu.add(actionMap.get(DefaultEditorKit.copyAction)); menu.add(actionMap.get(DefaultEditorKit.pasteAction)); menu.addSeparator(); undoAction = new UndoAction(); menu.add(undoAction); redoAction = new RedoAction(); menu.add(redoAction); menu.addSeparator(); menu.add(actionMap.get(DefaultEditorKit.selectAllAction)); return menu; } private void createActionMap(JTextComponent txt) { Action[] actions = txt.getActions(); for(Action action : actions) { this.actionMap.put(action.getValue(Action.NAME), action); } } private void createUI() { frame = new JFrame("TextTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea txtArea = new JTextArea(10, 30); // Documentのイベント感知(Undo Redo 管理を行う) Document doc = txtArea.getDocument(); doc.addUndoableEditListener( new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent e) { undo.addEdit(e.getEdit()); undoAction.updateState(); redoAction.updateState(); } } ); frame.getContentPane().add(new JScrollPane(txtArea), BorderLayout.CENTER); // TextComponentが持つActionをMapに格納 createActionMap(txtArea); // メニューを作成 JMenu menu = getMenu(); JMenuBar mb = new JMenuBar(); mb.add(menu); frame.setJMenuBar(mb); // テキストのUndo、Redoにキーを割り当てる (Ctrl+z,Ctrl+y) InputMap inputMap = txtArea.getInputMap(); KeyStroke undoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK); KeyStroke redoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CTRL_MASK); inputMap.put(undoKey, undoAction); inputMap.put(redoKey, redoAction); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater( new Runnable(){ public void run() { JTextTest jft = new JTextTest(); jft.createUI(); } } ); } @SuppressWarnings("serial") class UndoAction extends AbstractAction { public UndoAction() { super("Undo"); setEnabled(false); } public void actionPerformed(ActionEvent e) { try { undo.undo(); } catch (Exception ex) { ex.printStackTrace(); } updateState(); // Redo を可能に redoAction.updateState(); } public void updateState() { setEnabled(undo.canUndo()); } } @SuppressWarnings("serial") class RedoAction extends AbstractAction { public RedoAction() { super("Redo"); setEnabled(false); } public void actionPerformed(ActionEvent e) { try { undo.redo(); } catch (Exception ex) { ex.printStackTrace(); } updateState(); } public void updateState() { setEnabled(undo.canRedo()); } } }
© 2006 矢木浩人