JDocumentEditor

本文介绍了一个使用Java Swing实现的富文本编辑器,该编辑器支持多种文本样式操作,如字体大小、颜色调整等,并具备基本的文件操作功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


    }
}


内容概要:本文档详细介绍了基于Python的在线二手电子产品回收系统的设计与实现。项目旨在通过构建一个可靠、安全、透明的平台,提高废旧电子产品的回收率,推动资源的合理再利用,提供安全可靠的交易平台,加强环保意识,促进二手市场的发展,并实现数据驱动的智能化服务。项目面临的主要挑战包括废旧电子产品的检测与评估、信息不对称与交易风险、市场需求的预测与定价、用户体验优化及平台的安全性与数据保护。解决方案涵盖智能化评估与回收定价、高效的二手产品处理流程、完善的售后保障体系、创新的市场需求分析、全程透明化与安全性保障以及定制化用户体验。系统采用微服务架构,包括用户管理、商品评估、交易管理、数据分析、支付与结算等模块。项目还涉及前端界面设计、API接口开发、数据库设计与实现、模型训练与优化、部署与应用等方面。 适合人群:具备一定编程基础,特别是对Python和Web开发有一定了解的研发人员,以及对二手电子产品回收和环保事业感兴趣的从业者。 使用场景及目标:①帮助用户方便地将闲置电子产品回收、交易或再利用,提高废旧电子产品的回收率;②通过智能化的数据分析为用户提供价格评估、市场需求分析等服务,提高回收效率;③提供安全可靠的交易平台,确保交易的公平性和安全性;④推动二手市场的健康发展,为消费者提供经济实惠的产品选择;⑤增强公众的环保意识,推动社会向绿色、低碳方向发展。 其他说明:本文档不仅提供了系统的功能模块设计、数据库表结构、API接口规范,还展示了具体代码实现和GUI界面设计,为开发者提供了全面的技术参考。此外,项目强调了数据安全和隐私保护的重要性,确保平台在运行过程中能够有效保护用户信息。项目未来改进方向包括增强模型的精准度、拓展国际市场、提供更多支付和融资选项、跨平台数据集成与分析、更加智能的回收流程以及强化社交化与社区功能。
内容概要:本文档详细介绍了基于C语言和单片机设计的固态继电器驱动空调温控系统,涵盖了从硬件电路设计、程序设计、GUI设计到代码详解的完整流程。项目旨在实现高效精准的温度控制、提升系统可靠性和寿命、灵活的参数设置和人机交互、降低能耗、模块化设计便于扩展与维护,以及促进智能家居与工业自动化发展。项目通过高精度温度采集与滤波算法、固态继电器驱动与保护电路设计、滞环控制算法、多层次软件模块化设计等创新点,确保系统的高效节能、智能化和高可靠性。; 适合人群:具备一定单片机和C语言编程基础的研发人员,尤其是从事嵌入式系统设计、智能家居和工业自动化领域的工程师。; 使用场景及目标:①实现高效精准的温度控制,确保室内温度维持在理想范围;②提升系统可靠性和寿命,减少故障率和维护成本;③支持灵活的参数设置和用户友好的人机交互界面,提升用户体验;④降低能耗,实现节能控制,推动绿色建筑和节能环保产业的发展;⑤通过模块化设计,便于后续功能升级和系统扩展,如远程监控、数据分析等智能化功能。; 其他说明:项目设计充分考虑了实际应用中的挑战,如温度采集的精度与稳定性、电气兼容性、系统响应速度与控制稳定性、软件设计的资源优化与抗干扰等,提出了针对性的解决方案。系统不仅适用于家庭智能空调,还能广泛应用于工业、商业建筑、医疗环境及农业温室等多个领域。未来改进方向包括智能温度预测与自适应控制、多传感器融合技术应用、远程监控与云平台集成、低功耗与绿色节能优化等。通过该系统,不仅能够精确控制室内温度,保障舒适环境,还能有效节能,延长设备使用寿命,具有重要的实际应用价值和推广意义。
标题基于SpringBoot的学生学习成果管理平台研究AI更换标题第1章引言介绍研究背景、目的、意义以及论文结构。1.1研究背景与目的阐述学生学习成果管理的重要性及SpringBoot技术的优势。1.2研究意义分析该平台对学生、教师及教育机构的意义。1.3论文方法与结构简要介绍论文的研究方法和整体结构。第2章相关理论与技术概述SpringBoot框架、学习成果管理理论及相关技术。2.1SpringBoot框架简介介绍SpringBoot的基本概念、特点及应用领域。2.2学习成果管理理论基础阐述学习成果管理的核心理论和发展趋势。2.3相关技术分析分析平台开发所涉及的关键技术,如数据库、前端技术等。第3章平台需求分析与设计详细分析平台需求,并设计整体架构及功能模块。3.1需求分析从学生、教师、管理员等角度对平台需求进行深入分析。3.2整体架构设计设计平台的整体架构,包括技术架构和逻辑架构。3.3功能模块设计具体设计平台的核心功能模块,如成果展示、数据分析等。第4章平台实现与测试阐述平台的实现过程,并进行功能测试与性能分析。4.1平台实现详细介绍平台的开发环境、关键代码实现及技术难点解决方案。4.2功能测试对平台各项功能进行全面测试,确保功能正确无误。4.3性能分析分析平台的性能指标,如响应时间、并发处理能力等。第5章平台应用与效果评估探讨平台在实际教学中的应用,并对其效果进行评估。5.1平台应用案例选取典型应用案例,展示平台在实际教学中的使用情况。5.2效果评估方法介绍平台效果评估的具体方法和指标。5.3评估结果分析根据评估数据,对平台的应用效果进行深入分析。第6章结论与展望总结论文的主要研究成果,并指出未来研究方向。6.1研究结论概括性地阐述论文的研究结论和主要贡献。6.2研究展望针对当前研究的不足之处,提出未来改进和扩展的方向。
内容概要:本文详细介绍了一个基于Python实现的锂电池寿命预测项目,该项目采用双向门控循环单元(BiGRU)来处理锂电池的多维时序数据。文章首先阐述了项目背景,强调了锂电池寿命预测的重要性及其面临的挑战。接着介绍了项目的目标,包括实现高精度预测、提升模型适应性、优化电池管理系统、降低成本、推动技术创新、提供可扩展平台、增强鲁棒性和泛化能力、实现自动化流程以及促进学术与产业结合。然后讨论了项目中遇到的挑战及相应的解决方案,如数据预处理、模型选择、训练优化等。最后展示了项目的模型架构,包括数据输入层、特征提取层、BiGRU编码层、全连接层和输出层,并提供了部分Python代码示例,涵盖了数据预处理、模型构建、训练过程和预测评估等内容。; 适合人群:对锂电池寿命预测感兴趣的科研人员、从事电池管理系统开发的工程师、以及有一定Python编程基础并希望了解深度学习应用于时间序列分析的开发者。; 使用场景及目标:①适用于需要对锂电池进行健康状态监测和寿命预测的场合,如电动汽车、便携式电子产品等领域;②帮助研究人员和工程师理解BiGRU模型的工作原理及其在电池寿命预测中的应用;③为电池管理系统提供智能决策支持,优化维护策略,降低运营成本;④推动锂电池寿命预测技术的创新发展,促进学术研究与产业应用的结合。; 其他说明:此项目不仅提供了详细的理论和技术介绍,还附带了完整的代码示例,方便读者实践操作。建议读者在学习过程中结合实际数据进行实验,逐步掌握BiGRU模型的设计与实现,并根据具体应用场景调整模型参数,以达到最佳预测效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值