解答例 - 実習課題4 - 15.テキスト・コンポーネント2
(実習課題4)
実習課題3のプログラムを改良しなさい。
- 「カット」「コピー」「ペースト」の機能を追加しなさい。
- 「DefaultEditorKit」を利用する事。
解答例
/** * CutCopyPasteEditor.java * TECHSCORE Javaユーザインタフェース15章 実習課題4 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.ui.chapter15.exercise4; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JSlider; import javax.swing.JTextField; import javax.swing.JTextPane; import javax.swing.text.DefaultEditorKit; public class CutCopyPasteEditor extends JFrame implements ActionListener { private JTextPane textPane; private JMenuItem insertMenu; public CutCopyPasteEditor() { super("CutCopyPasteEditor"); setDefaultCloseOperation(EXIT_ON_CLOSE); textPane = new JTextPane(); JScrollPane scroll = new JScrollPane( textPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scroll.setPreferredSize(new Dimension(300, 200)); getContentPane().add(scroll); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); insertMenu = new JMenuItem("insert"); menuBar.add(insertMenu); insertMenu.addActionListener(this); JMenu editMenu = new JMenu("edit"); JMenuItem cutMenu = new JMenuItem(new DefaultEditorKit.CutAction()); cutMenu.setText("cut"); editMenu.add(cutMenu); JMenuItem copyMenu = new JMenuItem(new DefaultEditorKit.CopyAction()); copyMenu.setText("copy"); editMenu.add(copyMenu); JMenuItem pasteMenu = new JMenuItem(new DefaultEditorKit.PasteAction()); pasteMenu.setText("paste"); editMenu.add(pasteMenu); menuBar.add(editMenu); pack(); } String[] components = { "Button", "Slider", "TextField", "Label", "Icon" }; public void actionPerformed(ActionEvent event) { Object component = JOptionPane.showInputDialog( this, "追加したいアイテムを選択して下さい", "SELECT ITEM", JOptionPane.INFORMATION_MESSAGE, null, components, components[0]); //選択されたコンポーネントを追加する。 if (component != null) { if (component.equals(components[0])) { textPane.insertComponent(new JButton("button")); } else if (component == components[1]) { textPane.insertComponent(new JSlider()); } else if (component == components[2]) { textPane.insertComponent(new JTextField(100)); } else if (component == components[3]) { textPane.insertComponent(new JLabel("label")); } else if (component == components[4]) { textPane.insertIcon( new ImageIcon("com/techscore/ui/chapter15/exercise4/4dd.gif")); } } } public static void main(String[] args) { new CutCopyPasteEditor().setVisible(true); } }
package com.techscore.ui.chapter15.exercise4; /** * StyledEditor.java * TECHSCORE Javaユーザインタフェース15章 実習課題4 * * Copyright (c) 2002 Four-Dimensional Data, Inc. */ import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JSlider; import javax.swing.JTextField; import javax.swing.JTextPane; import javax.swing.text.DefaultEditorKit; import javax.swing.text.StyledEditorKit; public class StyledEditor extends JFrame implements ActionListener { private JTextPane textPane; private JMenuItem insertMenu; public StyledEditor() { super("Simple Editor"); setDefaultCloseOperation(EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); //insert menu setJMenuBar(menuBar); insertMenu = new JMenuItem("insert"); menuBar.add(insertMenu); insertMenu.addActionListener(this); //edit menu JMenu editMenu = new JMenu("edit"); JMenuItem cutMenu = new JMenuItem(new DefaultEditorKit.CutAction()); cutMenu.setText("cut"); editMenu.add(cutMenu); JMenuItem copyMenu = new JMenuItem(new DefaultEditorKit.CopyAction()); copyMenu.setText("copy"); editMenu.add(copyMenu); JMenuItem pasteMenu = new JMenuItem(new DefaultEditorKit.PasteAction()); pasteMenu.setText("paste"); editMenu.add(pasteMenu); menuBar.add(editMenu); Box box = new Box(BoxLayout.Y_AXIS); //checkbox Box childBox = new Box(BoxLayout.X_AXIS); JCheckBox boldFont = new JCheckBox(new StyledEditorKit.BoldAction()); boldFont.setText("bold"); childBox.add(boldFont); JCheckBox italicFont = new JCheckBox(new StyledEditorKit.ItalicAction()); italicFont.setText("italic"); childBox.add(italicFont); box.add(childBox); //text textPane = new JTextPane(); JScrollPane scroll = new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scroll.setPreferredSize(new Dimension(300, 200)); box.add(scroll); getContentPane().add(box); pack(); } String[] components = { "Button", "Slider", "TextField", "Label" }; public void actionPerformed(ActionEvent event) { Object component = JOptionPane.showInputDialog( this, "追加したいアイテムを選択して下さい", "SELECT ITEM", JOptionPane.INFORMATION_MESSAGE, null, components, components[0]); //選択されたコンポーネントを追加する。 if (component != null) { if (component.equals(components[0])) { textPane.insertComponent(new JButton("button")); } else if (component.equals(components[1])) { textPane.insertComponent(new JSlider()); } else if (component.equals(components[2])) { textPane.insertComponent(new JTextField(100)); } else if (component.equals(components[3])) { textPane.insertComponent(new JLabel("label")); } } } public static void main(String[] args) { new StyledEditor().setVisible(true); } }