📄 myjtextarea.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package jnotepad;import java.awt.datatransfer.*;import java.awt.dnd.*;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.Iterator;import java.util.List;import javax.swing.*;/** * * @author Administrator */class MyJTextArea extends JTextArea implements DropTargetListener { public boolean candrag = true; public MyJTextArea() { new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this); } public void dragEnter(DropTargetDragEvent dtde) { } public void dragOver(DropTargetDragEvent dtde) { } public void dropActionChanged(DropTargetDragEvent dtde) { } public void dragExit(DropTargetEvent dte) { } public void drop(DropTargetDropEvent dtde) { if (!candrag) { JOptionPane.showMessageDialog(null, "请先在菜单栏中设置支持拖拽文件!", "提示", JOptionPane.INFORMATION_MESSAGE); return; } try { Transferable tr = dtde.getTransferable(); if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); List list = (List) (dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor)); String filepath = ""; Iterator iterator = list.iterator(); while (iterator.hasNext()) { File f = (File) iterator.next(); filepath = f.getAbsolutePath(); } this.setText(""); if (filepath.endsWith(".txt")) { File file = new File(filepath); BufferedReader read = new BufferedReader(new FileReader(file)); String line = null; do { line = read.readLine(); this.append(line + "\n"); } while (line != null); } else this.setText(filepath); dtde.dropComplete(true); this.updateUI(); } else { dtde.rejectDrop(); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, " 无 法 显 示!", "失败", JOptionPane.INFORMATION_MESSAGE); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -