JDocumentEditor
package infonode;
/**
*
* @author sony
*/
//JDocumentEditor.java
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.undo.*;
public class JDocumentEditor extends JTextPane {
UndoHandler uh = null;
Font myFont;
javax.swing.plaf.FontUIResource f;
public JDocumentEditor() {
this.setContentType("text/html");
this.setFont(new Font("arial", 0, 14));
this.setText("hey i am deepak");
this.setDragEnabled(true);
}
public void setTextForeground(Color c) {
MutableAttributeSet att = new SimpleAttributeSet();
StyleConstants.setForeground(att, c);
this.setCharacterAttributes(att, false);
}
public void setTextBackground(Color c) {
MutableAttributeSet att = new SimpleAttributeSet();
StyleConstants.setBackground(att, c);
this.setCharacterAttributes(att, false);
}
public void setTextBold() {
EditorKit styleEd = this.getEditorKit();
if (!(styleEd instanceof StyledEditorKit)) {
return;
}
MutableAttributeSet att = ((StyledEditorKit) styleEd).getInputAttributes();
StyleConstants.setBold(att, !StyleConstants.isBold(att));
this.setCharacterAttributes(att, false);
System.out.println(this.getSelectedText());
}
public void setTextItalic() {
EditorKit styleEd = this.getEditorKit();
if (!(styleEd instanceof StyledEditorKit)) {
return;
}
MutableAttributeSet att = ((StyledEditorKit) styleEd).getInputAttributes();
StyleConstants.setItalic(att, !StyleConstants.isItalic(att));
this.setCharacterAttributes(att, false);
}
public void setTextUnderline() {
EditorKit styleEd = this.getEditorKit();
if (!(styleEd instanceof StyledEditorKit)) {
return;
}
MutableAttributeSet att = ((StyledEditorKit) styleEd).getInputAttributes();
StyleConstants.setUnderline(att, !StyleConstants.isUnderline(att));
this.setCharacterAttributes(att, false);
}
public void setTextStrikeThrough() {
EditorKit styleEd = this.getEditorKit();
if (!(styleEd instanceof StyledEditorKit)) {
return;
}
MutableAttributeSet att = ((StyledEditorKit) styleEd).getInputAttributes();
StyleConstants.setStrikeThrough(att, !StyleConstants.isStrikeThrough(att));
this.setCharacterAttributes(att, false);
}
public void setTextSuperscript() {
EditorKit styleEd = this.getEditorKit();
if (!(styleEd instanceof StyledEditorKit)) {
return;
}
MutableAttributeSet att = ((StyledEditorKit) styleEd).getInputAttributes();
StyleConstants.setSuperscript(att, !StyleConstants.isSuperscript(att));
this.setCharacterAttributes(att, false);
}
public void setTextSubscript() {
EditorKit styleEd = this.getEditorKit();
if (!(styleEd instanceof StyledEditorKit)) {
return;
}
MutableAttributeSet att = ((StyledEditorKit) styleEd).getInputAttributes();
StyleConstants.setSubscript(att, !StyleConstants.isSubscript(att));
this.setCharacterAttributes(att, false);
}
public void setfonts() {
try {
EditorKit styleEd = this.getEditorKit();
if (!(styleEd instanceof StyledEditorKit)) {
return;
}
MutableAttributeSet att = ((StyledEditorKit) styleEd).getInputAttributes();
File fontFile = new File("C://Users//sony//Desktop//1131.ttf");
myFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(Font.PLAIN, 22f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(myFont);
System.out.println(this.getFont());
f = new javax.swing.plaf.FontUIResource(myFont);
System.out.println(f.getFontName());
this.setFont(myFont);
this.setFont(f);
System.out.println(this.getFont());
this.setCharacterAttributes(att, false);
} catch (FontFormatException ex) {
Logger.getLogger(JDocumentEditor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JDocumentEditor.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setTextFontFamily(String fnt) {
MutableAttributeSet att = new SimpleAttributeSet();
StyleConstants.setFontFamily(att, fnt);
this.setCharacterAttributes(att, false);
}
public void setTextFontSize(int size) {
MutableAttributeSet att = new SimpleAttributeSet();
StyleConstants.setFontSize(att, size);
this.setCharacterAttributes(att, false);
}
public void setTextAlignment(int align) {
MutableAttributeSet att = new SimpleAttributeSet();
StyleConstants.setAlignment(att, align);
this.setParagraphAttributes(att, false);
}
public void setTextIndent(float indent) {
MutableAttributeSet att = new SimpleAttributeSet();
StyleConstants.setFirstLineIndent(att, indent);
this.setParagraphAttributes(att, false);
}
public void setTextSpaceAbove(float space) {
MutableAttributeSet att = new SimpleAttributeSet();
StyleConstants.setSpaceAbove(att, space);
this.setParagraphAttributes(att, false);
}
public void setTextSpaceBelow(float space) {
MutableAttributeSet att = new SimpleAttributeSet();
StyleConstants.setSpaceBelow(att, space);
this.setParagraphAttributes(att, false);
}
public void setTextLineSpacing(float space) {
MutableAttributeSet att = new SimpleAttributeSet();
StyleConstants.setLineSpacing(att, space);
this.setParagraphAttributes(att, false);
}
public void insertHTML(String s) {
try {
((HTMLEditorKit) this.getEditorKit()).read(new java.io.StringReader(s), this.getDocument(), this.getSelectionStart());
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void insertImage(String file) {
try {
((HTMLEditorKit) this.getEditorKit()).read(new java.io.StringReader("<img src=\"" + file + "\" />"), this.getDocument(), this.getSelectionStart());
ImageIcon imgicon = new ImageIcon(file);
this.insertIcon(imgicon);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void insertTable(int r, int c) {
try {
StringBuilder sb = new StringBuilder("<table border=1>\n");
for (int i = 0; i < r; i++) {
sb.append("<tr>");
for (int j = 0; j < c; j++) {
sb.append("<td></td>");
}
sb.append("</tr>\n");
}
sb.append("</table>");
((HTMLEditorKit) this.getEditorKit()).read(new java.io.StringReader(sb.toString()), this.getDocument(), this.getSelectionStart());
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void setUndoRedo(AbstractButton undo, AbstractButton redo) {
this.getDocument().addUndoableEditListener(uh = new UndoHandler(undo, redo));
}
public void undo() {
uh.reverseEditing(false);
}
public void redo() {
uh.reverseEditing(true);
}
public void resetUndo() {
uh.resetUndo();
}
class UndoHandler implements UndoableEditListener, ActionListener {
private UndoManager um = null;
private AbstractButton undo = null, redo = null;
public UndoHandler(AbstractButton umi, AbstractButton rmi) {
um = new UndoManager();
undo = umi;
redo = rmi;
JDocumentEditor.this.getDocument().addUndoableEditListener(this);
undo.addActionListener(this);
redo.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
reverseEditing(ae.getSource() == redo);
}
public void reverseEditing(boolean r) {
try {
if (r) {
um.redo();
} else {
um.undo();
}
refreshState();
} catch (Exception ex) {
javax.swing.JOptionPane.showMessageDialog(null,
"Error in " + (r ? "redo " : "undo ") + ex.toString());
}
}
public void resetUndo() {
um.discardAllEdits();
undo.setEnabled(false);
redo.setEnabled(false);
JDocumentEditor.this.getDocument().addUndoableEditListener(this);
}
public void refreshState() {
undo.setEnabled(um.canUndo());
redo.setEnabled(um.canRedo());
undo.setText(undo.isEnabled() ? um.getUndoPresentationName() : "Undo");
redo.setText(undo.isEnabled() ? um.getRedoPresentationName() : "Redo");
}
public void undoableEditHappened(UndoableEditEvent uee) {
try {
um.addEdit(uee.getEdit());
this.refreshState();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String a[]) {
new JDocumentEditor();
JFrame jf = new JFrame();
jf.setVisible(true);
JButton jb1 = new JButton("bold");
final JTextField jfield = new JTextField(" ");
jf.add(jfield);
jf.add(jb1);
jf.setLayout(new FlowLayout());
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(jfield.getText());
}
});
}
}
package infonode;
/**
*
* @author sony
*/
//DocumentTest.java
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.text.*;
class DocumentTest extends JFrame implements ActionListener {
Font myFont = null;
JDocumentEditor ed = new JDocumentEditor();
JFileChooser fc = new JFileChooser(), img = new JFileChooser();
public DocumentTest() {
super("Document Editor");
createToolbar();
this.getContentPane().add(new JScrollPane(ed), "Center");
fc.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(java.io.File f) {
String t = f.getName().toLowerCase();
return f.isDirectory() || t.endsWith(".html");
}
public String getDescription() {
return "HTML";
}
});
img.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(java.io.File f) {
String t = f.getName().toLowerCase();
return f.isDirectory() || t.endsWith(".jpg") || t.endsWith(".jpeg") || t.endsWith(".png") || t.endsWith(".gif");
}
public String getDescription() {
return "Image Files";
}
});
ed.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
ed.insertHTML("<p> </p>");
}
}
});
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
ed.requestFocus();
}
private void createToolbar() {
JToolBar tool = new JToolBar();
String buttonName[] = {"New", "Open", "Save", "Font", "Size", "Left", "Center", "Right", "Above", "Below", "Indent", "Line Space", "Font Colour", "Background", "Bold", "Italic", "Underline", "Superscript", "Subscript", "Strike", "Image", "Table", "Print", "Gujarati"};
JButton temp = null;
JPanel p = new JPanel(new java.awt.GridLayout(4, 6));
for (int i = 0; i < buttonName.length; i++) {
temp = new JButton(buttonName[i]);
temp.addActionListener(this);
p.add(temp);
}
JButton undo = new JButton("Undo"), redo = new JButton("Redo");
ed.setUndoRedo(undo, redo);
p.add(undo);
tool.add(p);
this.getContentPane().add(tool, "North");
}
public void actionPerformed(ActionEvent ae) {
String com = ae.getActionCommand();
if (com.equals("Font")) {
ed.setTextFontFamily(JOptionPane.showInputDialog(this, "Enter Font Name"));
} else if (com.equals("Size")) {
ed.setTextFontSize(Integer.parseInt(JOptionPane.showInputDialog(this, "Enter font size")));
} else if (com.equals("Gujarati")) {
ed.setfonts();
} else if (com.equals("Left")) {
ed.setTextAlignment(StyleConstants.ALIGN_LEFT);
} else if (com.equals("Center")) {
ed.setTextAlignment(StyleConstants.ALIGN_CENTER);
} else if (com.equals("Right")) {
ed.setTextAlignment(StyleConstants.ALIGN_RIGHT);
} else if (com.equals("Above")) {
ed.setTextSpaceAbove(Float.parseFloat(JOptionPane.showInputDialog(this, "Enter space above")));
} else if (com.equals("Below")) {
ed.setTextSpaceBelow(Float.parseFloat(JOptionPane.showInputDialog(this, "Enter space below")));
} else if (com.equals("Indent")) {
ed.setTextIndent(Float.parseFloat(JOptionPane.showInputDialog(this, "Enter first line indent")));
} else if (com.equals("Line Space")) {
ed.setTextLineSpacing(Float.parseFloat(JOptionPane.showInputDialog(this, "Enter line spacing")));
} else if (com.equals("Font Colour")) {
ed.setTextForeground(JColorChooser.showDialog(this, "Font colour", null));
} else if (com.equals("Background")) {
ed.setTextBackground(JColorChooser.showDialog(this, "Background", null));
} else if (com.equals("Bold")) {
ed.setTextBold();
} else if (com.equals("Italic")) {
ed.setTextItalic();
} else if (com.equals("Underline")) {
ed.setTextUnderline();
} else if (com.equals("Superscript")) {
ed.setTextSuperscript();
} else if (com.equals("Subscript")) {
ed.setTextSubscript();
} else if (com.equals("Strike")) {
ed.setTextStrikeThrough();
} else if (com.equals("New")) {
ed.setDocument(((javax.swing.text.html.HTMLEditorKit) ed.getEditorKit()).createDefaultDocument());
ed.resetUndo();
} else if (com.equals("Save")) {
saveFile();
} else if (com.equals("Open")) {
openFile();
ed.resetUndo();
} else if (com.equals("Image")) {
if (img.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
ed.insertImage(img.getSelectedFile().getAbsolutePath());
}
} else if (com.equals("Table")) {
ed.insertTable(Integer.parseInt(JOptionPane.showInputDialog(this, "Enter rows")), Integer.parseInt(JOptionPane.showInputDialog(this, "Enter columns")));
} else if (com.equals("Print")) {
try {
ed.print();
} catch (Exception ex) {
}
}
ed.requestFocus();
System.out.println(ed.getSelectedText() + ed.getText());
}
private void saveFile() {
if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
java.io.File f = fc.getSelectedFile();
try {
java.io.FileWriter fw = new java.io.FileWriter(f);
ed.write(fw);
fw.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.toString());
}
}
private void openFile() {
ed.setEditable(false);
if (fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
java.io.File f = fc.getSelectedFile();
try {
ed.read(new java.io.FileReader(f), null);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.toString());
}
}
public static void main(String arg[]) {
DocumentTest dt = new DocumentTest();
}
}
随机推荐
- [Udemy] AWS Certified Data Analytics Specialty - 2.Storage
S3 Replication (CRR & SRR) S3 Encryption S3 Security 其中两个ACL基本不会考 记住这3个event发送的target DynamoDB D ...
- C++性能优化——能用array就不要用unordered_map作为查询表
unordered_map需要哈希值计算和表查询的开销,当key值为整数且连续,直接用数组作为查询表具有更高的效率. #include <iostream> #include <ch ...
- 深度学习环境安装-conda-torch-Jupyter Notebook
conda的安装 为什么要安装这个,它是什么? 它是一个管理环境的,当我们跑项目的时候,往往这些项目所需要的pickets库和环境是不同的,这时候如果自己的电脑里面只有一个版本的库的话,就运行不了,比 ...
- 合合信息扫描全能王发布“黑科技”,让AI替人“思考”图像处理问题
现阶段,手机扫描正越来越多地进入到人们的生活中.随着扫描应用场景的不断拓宽,诸多细节的问题逐渐显露,比如使用者在拍照扫描文档时,手指不小心"入镜"了,只能重拍:拍电脑屏幕时,画面上 ...
- mongo集群同步数据异常,手动同步节点副本数据
转载请注明出处: 数据同步方案 当副本集节点的复制进程落后太多,以至于主节点覆盖了该节点尚未复制的 oplog 条目时,副本集节点就会变为"陈旧".节点跟不上,就会变得" ...
- ROS基础入门——实操教程
ROS基础入门--实操教程 前言 本教程实操为主,少说书.可供参考的文档中详细的记录了ROS的实操和理论,只是过于详细繁杂了,看得脑壳疼,于是做了这个笔记. Ruby Rose,放在这里相当合理 前言 ...
- 利用3D标签,生成RLE标签编码,并保存到csv文件
# coding:utf-8from glob import globimport osimport SimpleITK as sitkfrom pathlib import Pathimport n ...
- 在Vue3中如何实现四种全局状态数据的统一管理?
四种全局状态数据 在实际开发当中,会遇到四种全局状态数据:异步数据(一般来自服务端).同步数据.同步数据又分为三种:localstorage.cookie.内存.在传统的 Vue3 当中,分别采用不同 ...
- k8s的pod的理解
pod共享相同的IP地址和端口空间. 这意味着在同一 pod中的容器运行的 多个进程需要注意不能绑定到相同的端口号, 否则会导致端口冲突, 但这只涉及同一pod中的容器. 由于每个pod都有独立的端口 ...
- 三步搞定 ARM64 离线部署 Kubernetess + KubeSphere
背景 随着我国对信息安全的愈发重视,国产化的趋势也越来越浓,包括国产操作系统.国产 CPU 等.由于 ARM 架构国产 CPU 在维持创新可信和先进性方面的潜在优势,其应用也将会越来越广泛. Kube ...