{"id":25486,"date":"2024-09-06T07:40:57","date_gmt":"2024-09-05T22:40:57","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25486"},"modified":"2024-09-06T07:41:00","modified_gmt":"2024-09-05T22:41:00","slug":"java-%e3%83%86%e3%82%ad%e3%82%b9%e3%83%88%e3%82%a8%e3%83%87%e3%82%a3%e3%82%bf","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25486","title":{"rendered":"Java \u30c6\u30ad\u30b9\u30c8\u30a8\u30c7\u30a3\u30bf"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import javax.swing.*;\nimport javax.swing.undo.UndoManager;\nimport java.awt.*;\nimport java.awt.event.*;\nimport java.io.*;\nimport java.awt.dnd.*;\nimport java.awt.datatransfer.*;\n\npublic class AdvancedTextEditor extends JFrame implements ActionListener {\n    JTabbedPane tabbedPane;\n    JTextArea textArea;\n    JMenuBar menuBar;\n    JMenu fileMenu, editMenu, formatMenu, themeMenu;\n    JMenuItem newItem, openItem, saveItem, saveAllItem, closeItem;\n    JMenuItem cutItem, copyItem, pasteItem, findItem, replaceItem, undoItem, redoItem;\n    JMenuItem fontItem, lightThemeItem, darkThemeItem;\n    JFileChooser fileChooser;\n    JLabel statusBar;\n    UndoManager undoManager = new UndoManager();\n    JPopupMenu contextMenu;\n\n    public AdvancedTextEditor() {\n        setTitle(\"Advanced Text Editor\");\n        setSize(800, 600);\n        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n\n        tabbedPane = new JTabbedPane();\n        add(tabbedPane, BorderLayout.CENTER);\n\n        statusBar = new JLabel(\"Status: Ready | Line: 1, Column: 1\");\n        add(statusBar, BorderLayout.SOUTH);\n\n        menuBar = new JMenuBar();\n\n        \/\/ \u30d5\u30a1\u30a4\u30eb\u30e1\u30cb\u30e5\u30fc\u306e\u4f5c\u6210\n        fileMenu = new JMenu(\"File\");\n        newItem = new JMenuItem(\"New\");\n        openItem = new JMenuItem(\"Open\");\n        saveItem = new JMenuItem(\"Save\");\n        saveAllItem = new JMenuItem(\"Save All\");\n        closeItem = new JMenuItem(\"Close\");\n        fileMenu.add(newItem);\n        fileMenu.add(openItem);\n        fileMenu.add(saveItem);\n        fileMenu.add(saveAllItem);\n        fileMenu.add(closeItem);\n\n        \/\/ \u7de8\u96c6\u30e1\u30cb\u30e5\u30fc\u306e\u4f5c\u6210\n        editMenu = new JMenu(\"Edit\");\n        cutItem = new JMenuItem(\"Cut\");\n        copyItem = new JMenuItem(\"Copy\");\n        pasteItem = new JMenuItem(\"Paste\");\n        undoItem = new JMenuItem(\"Undo\");\n        redoItem = new JMenuItem(\"Redo\");\n        findItem = new JMenuItem(\"Find\");\n        replaceItem = new JMenuItem(\"Replace\");\n        editMenu.add(cutItem);\n        editMenu.add(copyItem);\n        editMenu.add(pasteItem);\n        editMenu.add(undoItem);\n        editMenu.add(redoItem);\n        editMenu.add(findItem);\n        editMenu.add(replaceItem);\n\n        \/\/ \u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u30e1\u30cb\u30e5\u30fc\u306e\u4f5c\u6210\n        formatMenu = new JMenu(\"Format\");\n        fontItem = new JMenuItem(\"Change Font\");\n        formatMenu.add(fontItem);\n\n        \/\/ \u30c6\u30fc\u30de\u30e1\u30cb\u30e5\u30fc\u306e\u4f5c\u6210\n        themeMenu = new JMenu(\"Themes\");\n        lightThemeItem = new JMenuItem(\"Light Theme\");\n        darkThemeItem = new JMenuItem(\"Dark Theme\");\n        themeMenu.add(lightThemeItem);\n        themeMenu.add(darkThemeItem);\n\n        \/\/ \u30e1\u30cb\u30e5\u30fc\u30d0\u30fc\u306b\u30e1\u30cb\u30e5\u30fc\u3092\u8ffd\u52a0\n        menuBar.add(fileMenu);\n        menuBar.add(editMenu);\n        menuBar.add(formatMenu);\n        menuBar.add(themeMenu);\n\n        setJMenuBar(menuBar);\n\n        \/\/ \u30a2\u30af\u30b7\u30e7\u30f3\u30ea\u30b9\u30ca\u30fc\u306e\u8a2d\u5b9a\n        newItem.addActionListener(this);\n        openItem.addActionListener(this);\n        saveItem.addActionListener(this);\n        saveAllItem.addActionListener(this);\n        closeItem.addActionListener(this);\n        cutItem.addActionListener(this);\n        copyItem.addActionListener(this);\n        pasteItem.addActionListener(this);\n        undoItem.addActionListener(this);\n        redoItem.addActionListener(this);\n        findItem.addActionListener(this);\n        replaceItem.addActionListener(this);\n        fontItem.addActionListener(this);\n        lightThemeItem.addActionListener(this);\n        darkThemeItem.addActionListener(this);\n\n        \/\/ \u30c9\u30e9\u30c3\u30b0&amp;\u30c9\u30ed\u30c3\u30d7\u5bfe\u5fdc\n        new DropTarget(tabbedPane, new FileDropHandler());\n\n        \/\/ \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30e1\u30cb\u30e5\u30fc\u306e\u8a2d\u5b9a\n        contextMenu = new JPopupMenu();\n        contextMenu.add(cutItem);\n        contextMenu.add(copyItem);\n        contextMenu.add(pasteItem);\n    }\n\n    \/\/ \u30a2\u30af\u30b7\u30e7\u30f3\u30ea\u30b9\u30ca\u30fc\u306e\u51e6\u7406\n    @Override\n    public void actionPerformed(ActionEvent e) {\n        if (e.getSource() == newItem) {\n            addNewTab();\n        }\n        \/\/ \u4ed6\u306e\u30a2\u30af\u30b7\u30e7\u30f3\u3082\u3053\u3053\u306b\u8ffd\u52a0\n    }\n\n    \/\/ \u65b0\u3057\u3044\u30bf\u30d6\u3092\u8ffd\u52a0\u3059\u308b\u30e1\u30bd\u30c3\u30c9\n    private void addNewTab() {\n        JTextArea textArea = new JTextArea();\n        JScrollPane scrollPane = new JScrollPane(textArea);\n        tabbedPane.add(\"Untitled\", scrollPane);\n        tabbedPane.setSelectedComponent(scrollPane);\n        textArea.getDocument().addUndoableEditListener(e -> undoManager.addEdit(e.getEdit()));\n    }\n\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u30c9\u30ed\u30c3\u30d7\u30cf\u30f3\u30c9\u30e9\n    @SuppressWarnings(\"unchecked\")\n    class FileDropHandler extends DropTargetAdapter {\n        @Override\n        public void drop(DropTargetDropEvent dtde) {\n            dtde.acceptDrop(DnDConstants.ACTION_COPY);\n            try {\n                java.util.List&lt;File> droppedFiles = (java.util.List&lt;File>) dtde.getTransferable()\n                        .getTransferData(DataFlavor.javaFileListFlavor);\n                for (File file : droppedFiles) {\n                    openFile(file);\n                }\n            } catch (Exception ex) {\n                ex.printStackTrace();\n            }\n        }\n    }\n\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f\u30e1\u30bd\u30c3\u30c9\n    private void openFile(File file) {\n        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {\n            JTextArea textArea = new JTextArea();\n            textArea.read(reader, null);\n            JScrollPane scrollPane = new JScrollPane(textArea);\n            tabbedPane.add(file.getName(), scrollPane);\n            tabbedPane.setSelectedComponent(scrollPane);\n        } catch (IOException ex) {\n            ex.printStackTrace();\n        }\n    }\n\n    public static void main(String&#91;] args) {\n        SwingUtilities.invokeLater(() -> {\n            AdvancedTextEditor editor = new AdvancedTextEditor();\n            editor.setVisible(true);\n        });\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[76,4],"tags":[75,3],"class_list":["post-25486","post","type-post","status-publish","format-standard","hentry","category-java","category-programming","tag-java","tag-programming"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25486"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25486\/revisions"}],"predecessor-version":[{"id":25487,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25486\/revisions\/25487"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25486"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}