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(); }
}

随机推荐

  1. C#/.NET/.NET Core技术前沿周刊 | 第 3 期(2024年8.26-8.31)

    前言 C#/.NET/.NET Core技术前沿周刊,你的每周技术指南针!记录.追踪C#/.NET/.NET Core领域.生态的每周最新.最实用.最有价值的技术文章.社区动态.优质项目和学习资源等. ...

  2. 小tips:nodejs请求接口超时使用中间件connect-timeout实现自动超时机制

    如果在请求中不设置超时时间,那么一直处理loading卡屏状态,使用connect-timeout来设置自动超时时间. 安装: npm install connect-timeout -S 如下例子: ...

  3. 【论文解读】RLAIF基于人工智能反馈的强化学习

    [论文解读]RLAIF基于人工智能反馈的强化学习 一.简要介绍 人类反馈强化学习(RLHF)可以有效地将大型语言模型(LLM)与人类偏好对齐,但收集高质量的人类偏好标签是一个关键瓶颈.论文进行了一场R ...

  4. DOM – Event Listener (bubble, capture, passive, custom event)

    前言 老掉牙的东西, 主要是想写 passive, 随便也写一点 bubble, capture 和 custom event 吧. Bubble Dom 监听事件是会冒泡的. 什么意思 ? 上图有 ...

  5. ASP.NET Core Library – MailKit SMTP Client

    前言 以前写的 SMTP Client 相关文章: Asp.net core 学习笔记 ( Smtp and Razor template 电子邮件和 Razor 模板 ) ASP.NET Email ...

  6. 信创环境经典版SuerMap iManager ARM版部署流程

    一.环境 操作系统:银河麒麟kylin V10 CPU:鲲鹏920 SuperMap iManager 10.2.1 硬件:4H32G机器 磁盘分区格式建议如下(请严格按照如下,减少后期有用/目录资源 ...

  7. 北京智和信通 | 无人值守的IDC机房动环综合监控运维

    随着信息技术的发展和全面应用,数据中心机房已成为各大企事业单位维持业务正常运营的重要组成部分,网络设备.系统.业务应用数量与日俱增,规模逐渐扩大,一旦机房内的设备出现故障,将对数据处理.传输.存储以及 ...

  8. 2021 IT运维巡展北京站圆满落幕,北京智和信通荣获IT运维样板工程

    10月21日,以"数智转型 运维赋能"为主题的"2021(第十二届)IT运维巡展北京站"圆满落幕.会上行业专家.企业代表以及用户代表等共聚一堂,探讨数智时代下I ...

  9. 原生CSS、HTML 和 JavaScript 实现酷炫表单

    一直使用 Vue/React ,习惯了用组件,偶尔想用原生三剑客写点 Demo 发现样式丑的不忍直视.最近看 掘金小册<玩转CSS的艺术之美>看到 CSS 相关的内容,发现原生 CSS 也 ...

  10. ShardingSphere系列(二)——ShardingSphere-JDBC绑定表

    完整的项目示例地址:https://gitee.com/learnhow/shardingsphere/tree/v1.1/jdbc 紧接上一篇文章,这次我们介绍绑定表的概念. 绑定表指分片规则一致的 ...