Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将html加载到styledocument_Java_Html_Parsing_Htmleditorkit - Fatal编程技术网

Java 如何将html加载到styledocument

Java 如何将html加载到styledocument,java,html,parsing,htmleditorkit,Java,Html,Parsing,Htmleditorkit,我正在使用样式文档在JTextPane中显示我的内容。 我搜索了一会儿,发现我可以用HTMLEditorKit将从文本窗格获取的文档写入文件。但是当我想用HTMLEditorKit读取这个文件时,它没有在正确的文档中进行解析。我得到两个不同的结果: 我在文本窗格中获得普通html代码 我的文本窗格中没有内容 保存: Document doc = textpane.getStyledDocument(); HTMLEditorKit kit = new HTMLEditorKit(); kit.w

我正在使用样式文档在JTextPane中显示我的内容。 我搜索了一会儿,发现我可以用HTMLEditorKit将从文本窗格获取的文档写入文件。但是当我想用HTMLEditorKit读取这个文件时,它没有在正确的文档中进行解析。我得到两个不同的结果:

  • 我在文本窗格中获得普通html代码
  • 我的文本窗格中没有内容
  • 保存:

    Document doc = textpane.getStyledDocument();
    HTMLEditorKit kit = new HTMLEditorKit();
    kit.write(new FileOutputStream("path"), doc, 0, doc.getLength());
    
    HTMLEditorKit kit = new HTMLEditorKit();
    Document doc = null;
    Document doc2 = kit.createDefaultDocument();
    
    kit.read(new FileInputStream("path"), doc, 0);
    kit.read(new FileInputStream("path"), doc2, 0);
    
    textpane.setDocument(doc);
    textpane.setDocument(doc2);
    
    加载(2个版本):

    Document doc = textpane.getStyledDocument();
    HTMLEditorKit kit = new HTMLEditorKit();
    kit.write(new FileOutputStream("path"), doc, 0, doc.getLength());
    
    HTMLEditorKit kit = new HTMLEditorKit();
    Document doc = null;
    Document doc2 = kit.createDefaultDocument();
    
    kit.read(new FileInputStream("path"), doc, 0);
    kit.read(new FileInputStream("path"), doc2, 0);
    
    textpane.setDocument(doc);
    textpane.setDocument(doc2);
    

    您需要将JTextPane的内容类型设置为“text/html”。这将正确显示html

    初始化JTextPane时,添加以下行:
    textpane.setContentType(“text/html”)

    将保存更改为如下所示:

    Document document = textpane.getStyledDocument();
    EditorKit kit = textpane.getEditorKit();
    kit.write(new FileOutputStream(new File("path")), doc, 0, doc.getLength());
    
    EditorKit kit = pane2.getEditorKit();
    Document doc = kit.createDefaultDocument();
    kit.read(new FileInputStream(new File("path")), doc, 0);
    textpane.setDocument(doc);
    
        private void AboutGame()
        {
            JEditorPane Log = CreateEditorPane("AboutGame.html");
    
            JScrollPane LogScrollPanel = new JScrollPane(Log);
            LogScrollPanel.setVerticalScrollBarPolicy
                                (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            LogScrollPanel.setPreferredSize(new Dimension(800, 400));
            LogScrollPanel.setMinimumSize(new Dimension(10, 10));
    
            Object Message = LogScrollPanel;
    
            JOptionPane.showMessageDialog(null, 
                                              Message, "About Game", 1);
        }
    
    将加载更改为如下所示:

    Document document = textpane.getStyledDocument();
    EditorKit kit = textpane.getEditorKit();
    kit.write(new FileOutputStream(new File("path")), doc, 0, doc.getLength());
    
    EditorKit kit = pane2.getEditorKit();
    Document doc = kit.createDefaultDocument();
    kit.read(new FileInputStream(new File("path")), doc, 0);
    textpane.setDocument(doc);
    
        private void AboutGame()
        {
            JEditorPane Log = CreateEditorPane("AboutGame.html");
    
            JScrollPane LogScrollPanel = new JScrollPane(Log);
            LogScrollPanel.setVerticalScrollBarPolicy
                                (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            LogScrollPanel.setPreferredSize(new Dimension(800, 400));
            LogScrollPanel.setMinimumSize(new Dimension(10, 10));
    
            Object Message = LogScrollPanel;
    
            JOptionPane.showMessageDialog(null, 
                                              Message, "About Game", 1);
        }
    

    在我的测试环境中使用此设置,我能够将
    JTextPane
    中的文本设置为某种html,从窗格中获取html,将其写入文件,然后从同一文件中读回。我不认为有任何理由使用
    HTMLEditorKit
    ,因为您没有做任何特定于html的事情,但是您可以根据自己的需要进行更改

    我猜您不想更改为保存而编写的代码。所以就让它保持原样吧
    您需要按如下方式更改加载代码:

    HTMLEditorKit kit = new HTMLEditorKit();
    StyledDocument doc2 = (StyledDocument)kit.createDefaultDocument();
    kit.read(new FileInputStream(file), doc2, 0);
    pane = new JTextPane();
    pane.setEditorKit(kit);//set EditorKit of JTextPane as kit.
    pane.setDocument(doc2);
    

    例如考虑下面给出的代码演示:

    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class HTMLKit extends JFrame implements ActionListener{
         public static final String text = "As told by Wikipedia\n"
        +"Java is a general-purpose, concurrent, class-based, object-oriented computer programming language."
        + "It is specifically designed to have as few implementation "
        + "dependencies as possible. It is intended to let application developers write once, run anywhere (WORA), "
        + "meaning that code that runs on one platform does not need to be recompiled to run on another. "
        + "Java applications are typically compiled to bytecode (class file) that can run on any Java virtual "
        + "machine (JVM) regardless of computer architecture. Java is, as of 2012, one of the most popular programming "
        + "languages in use, particularly for client-server web applications, with a reported 10 million users.";
        JTextPane pane ;
        DefaultStyledDocument doc ;
        StyleContext sc;
        JButton save;
        JButton load;
        JScrollPane jsp;
        public static void main(String[] args) 
        {
            try 
            {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        HTMLKit se = new HTMLKit();
                        se.createAndShowGUI();
                    }
                });
            } catch (Exception evt) {}
        }
        public void createAndShowGUI()
        {
            setTitle("TextPane");
            sc = new StyleContext();
            doc = new DefaultStyledDocument(sc);
            pane = new JTextPane(doc);
            save = new JButton("Save");
            load = new JButton("Load");
            JPanel panel = new JPanel();
            panel.add(save);panel.add(load);
            save.addActionListener(this);load.addActionListener(this);
            final Style heading2Style = sc.addStyle("Heading2", null);
            heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
            heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
            heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
            heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));
            try 
            {
                doc.insertString(0, text, null);
                doc.setParagraphAttributes(0, 1, heading2Style, false);
            } catch (Exception e) 
            {
                System.out.println("Exception when constructing document: " + e);
                System.exit(1);
            }
            jsp = new JScrollPane(pane);
            getContentPane().add(jsp);
            getContentPane().add(panel,BorderLayout.SOUTH);
            setSize(400, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
        public void actionPerformed(ActionEvent evt)
        {
            if (evt.getSource() == save)
            {
                save();
            }
            else if (evt.getSource() == load)
            {
                load();
            }
        }
        private void save()
        {
            JFileChooser chooser = new JFileChooser(".");
            chooser.setDialogTitle("Save");
            int returnVal = chooser.showSaveDialog(this);
            if(returnVal == JFileChooser.APPROVE_OPTION) 
            {
                File file = chooser.getSelectedFile();
                if (file != null)
                {
                    try
                    {
                        Document doc = pane.getStyledDocument();
                        HTMLEditorKit kit = new HTMLEditorKit();
                        kit.write(new FileOutputStream(file), doc, 0, doc.getLength());
                        JOptionPane.showMessageDialog(this,"Saved successfully!!","Success",JOptionPane.INFORMATION_MESSAGE);
                    }
                    catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
                else 
                {
                    JOptionPane.showMessageDialog(this,"Please enter a fileName","Error",JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        private void load()
        {
            JFileChooser chooser = new JFileChooser(".");
            chooser.setDialogTitle("Open");
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int returnVal = chooser.showOpenDialog(this);
            if(returnVal == JFileChooser.APPROVE_OPTION) 
            {
                File file = chooser.getSelectedFile();
                if (file!= null)
                {
                    try
                    {
                        HTMLEditorKit kit = new HTMLEditorKit();
                        StyledDocument doc2 = (StyledDocument)kit.createDefaultDocument();
                        kit.read(new FileInputStream(file), doc2, 0);
                        JTextPane pane1 = new JTextPane();
                        pane1.setEditorKit(kit);
                        pane1.setDocument(doc2);
                        repaint();
                        jsp.setViewportView(pane1);
                        repaint();
                        JOptionPane.showMessageDialog(this,"Loaded successfully!!","Success",JOptionPane.INFORMATION_MESSAGE);
                    }
                    catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
                else 
                {
                    JOptionPane.showMessageDialog(this,"Please enter a fileName","Error",JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    
    }
    

    您可以在以下窗格中显示html:

    Document document = textpane.getStyledDocument();
    EditorKit kit = textpane.getEditorKit();
    kit.write(new FileOutputStream(new File("path")), doc, 0, doc.getLength());
    
    EditorKit kit = pane2.getEditorKit();
    Document doc = kit.createDefaultDocument();
    kit.read(new FileInputStream(new File("path")), doc, 0);
    textpane.setDocument(doc);
    
        private void AboutGame()
        {
            JEditorPane Log = CreateEditorPane("AboutGame.html");
    
            JScrollPane LogScrollPanel = new JScrollPane(Log);
            LogScrollPanel.setVerticalScrollBarPolicy
                                (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            LogScrollPanel.setPreferredSize(new Dimension(800, 400));
            LogScrollPanel.setMinimumSize(new Dimension(10, 10));
    
            Object Message = LogScrollPanel;
    
            JOptionPane.showMessageDialog(null, 
                                              Message, "About Game", 1);
        }
    

    如果您想在Java应用程序中呈现任意HTML,最好使用WebKit之类的“真实”HTML引擎。JavaFX在WebView中为它提供了一个绑定:故事如下,。。我最初插入了一些格式化的文本(比如标题的粗体),现在用户可以添加更多的文本,然后我想用格式保存这些文本。。我不想显示html文件或类似的东西。您是否在写入保存的文件后检查了它。用浏览器打开,查看它是否正确显示。此外,您可能希望使用inputstreamreader进行编码。