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. IP服务正常,域名服务异常,报400 badrequest

    IP的情况下,访问接口都正常,使用域名访问,报错400 badrequest 经确认,ssl配置无问题,证书文件本身无问题 最后查出来原因,是域名格式的问题,原域名中包含_,需要修改为- 排查过程: ...

  2. 用描述程序的方式emo,扎心了...

    用描述程序的方式emo,扎心了... 众所周知写程序是个枯燥无聊的过程,再加上生活的不顺与坎坷,当程序语言与emo结合起来,看谁还说程序员不懂感情! 首当其冲的就是循环语句了 世界上最寂寞的感觉,是我 ...

  3. win10环境下 jdk8安装点击下一步没反应解决办法

    win10 安装 jdk8 这个界面点下一步,每次总是没反应. jdk8官网下载的,要是第三方下载的铁定要怀疑是不是安装包问题了 解决方法:将输入法切换到系统自带的.(我一开始默认的是百度输入法) 这 ...

  4. AWS Cloud Practioner 官方课程笔记 - Part 3

    AWS Security 方案和功能 Amazon Inspector AWS Shield Price and Support Free Tier: Always Free, 12-month fr ...

  5. SpringBoot兼容SpringMVC带有.do后缀的请求

    背景 MVC框架请求的都是.do后缀,但controller控制层拦截的是没有后缀的链接.如controller请求/111/222,当请求/111/222.do时,可以正常进入.当我们将存量一些旧工 ...

  6. .NET 多版本 WinForm 开源控件库 SunnyUI

    前言 给大家推荐一款开源的 Winform 控件库,可以帮助我们开发更加美观.漂亮的 WinForm 界面. 项目介绍 SunnyUI.NET 是一个基于 .NET Framework 4.0+..N ...

  7. Qml 实现仿前端的 Notification (悬浮出现页面上的通知消息)

    [写在前面] 经常接触前端的朋友应该经常见到下面的控件: 在前端中一般称它为 Notification 或 Message,但本质是一种东西,即:悬浮弹出式的消息提醒框. 这种组件一般具有以下特点: ...

  8. 图片 电力电网行业IT运维方案

    智能电网背景下,电力.电网企业信息化逐渐渗透到其业务链的各个环节,云计算.物联网.移动互联网等新技术的应用,更驱动信息化与业务创新深度融合.电力.电网企业集团信息系统群逐渐朝着一体化方向发展,信息链越 ...

  9. windows下配置pytorch环境

    借鉴了B站大佬的视频,自己总结安装如下. 首先安装anaconda 按照操作顺序,依次安装,按照我个人习惯,不喜欢讲文件安装在C盘,你们自己决定. 安装完毕之后. 之后打开Anaconda Promp ...

  10. 如何实现高效运维?来谈谈性能优化那些事(含直播回顾 Q&A)

    数据库性能问题,常常是困扰DBA高效运维的难题之一.如何多角度地帮助DBA,找到"数据库慢"的原因,保证系统高效.稳定.安全地运行? 2021年10月14日,云和恩墨技术顾问,拥有 ...