本文介绍了阅读HTML时,在JEditorPane中应用ForegroundActions的性能不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用,但是我遇到了一些与前台操作不一致的性能问题。我在下面有一个简化版本的编辑器,它有三种操作:将字体颜色更改为红色或蓝色,或更改字体大小。现在使用以下testFile.html文件:

 < html> 
< head>< title>标题< / title>< / head>
< body link =#0000FFbgcolor =white>
< font size =4face =arialcolor =black>一些测试文字< / font>
< font size =3face =arialcolor =black>一些新的测试文字< / font>
< / body>
< / html>

有时我可以在编辑器中突出显示一些文本,然后按红色或蓝色按钮,工作正常,即它改变颜色。在其他情况下(即,如果我关闭JVM并重新启动它),颜色不会改变,直到我在同一文本上应用 StyledEditorKit.FontSizeAction



在应用 ForegroundActions 时有什么遗漏吗?或者这可能是一些Java错误?



以下代码:

 公共类EditorTest扩展JFrame {

私人JEditorPane editorPane;
public EditorTest()
{
editorPane = new JEditorPane();
editorPane.setContentType(text / HTML);
getContentPane()。add(editorPane,BorderLayout.CENTER);
editorPane.setEditorKit(new HTMLEditorKit());


Action a = new StyledEditorKit.ForegroundAction(RedColor,Color.RED);
editorPane.getActionMap()。put(RedColor,a);

JToolBar bar = new JToolBar();

JButton button = new JButton(blue);
button.addActionListener(new StyledEditorKit.ForegroundAction(
set-foreground-red,Color.blue));


bar.add(editorPane.getActionMap()。get(font-size-12))。setText(12);
bar.add(button);
bar.add(editorPane.getActionMap()。get(RedColor))。setText(Red);

getContentPane()。add(bar,BorderLayout.NORTH);
setSize(650,600);
setVisible(true);

档案档案=新档案(testFile.html);
FileReader reader = null;
尝试
{
reader = new FileReader(file);
editorPane.read(reader,null);
}
catch(IOException ex){}
}
}


解决方案

使用下面的,我无法重现你描述的效果。如果Swing GUI对象没有在。该示例相应地使用 EventQueue.invokeLater() $ b

  import java.awt.BorderLayout; 
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTMLEditorKit;

/ ** http://stackoverflow.com/questions/8523445 * /
公共类StyledEditorTest扩展JFrame {

公共StyledEditorTest(){
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType(text / HTML);
editorPane.setEditorKit(new HTMLEditorKit());
editorPane.setText(< hr>欢迎使用< b> StackOverFlow!< / b>< hr>);
JToolBar bar = new JToolBar();
bar.add(new StyledEditorKit.ForegroundAction(Red,Color.red));
bar.add(新StyledEditorKit.ForegroundAction(Blue,Color.blue));
bar.add(new StyledEditorKit.FontSizeAction(12,12));
bar.add(new StyledEditorKit.FontSizeAction(14,14));
bar.add(new StyledEditorKit.FontSizeAction(16,16));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.add(bar,BorderLayout.NORTH);
this.add(editorPane,BorderLayout.CENTER);
this.pack();
this.setVisible(true);


public static void main(String [] args){
EventQueue.invokeLater(new Runnable(){
$ b $ @Override
public void run(){
new StyledEditorTest();
}
});
}
}


I'm building an HTML editor using JEditorPane, but I'm getting some inconsistent performance issues with Foreground Actions. I have a simplified version of my editor below that has three actions: change the font color to red or blue, or change the font-size. Now using the following testFile.html file:

<html>
  <head><title>Title</title></head>
  <body link="#0000FF" bgcolor="white">
    <font size="4" face="arial" color="black">Some test text</font>
    <font size="3" face="arial" color="black">Some new test text </font>
  </body>
</html>

sometimes I can highlight some text in the editor, and press the red or blue color buttons, and it works fine i.e. it changes color. On other occasions (i.e. if I close my JVM and start it up again) the color will not change UNTIL I apply a StyledEditorKit.FontSizeAction on the same text.

Is there something missing in how I'm applying the ForegroundActions? Or might this be some Java bug?

Code below:

public class EditorTest extends JFrame{

private JEditorPane editorPane;
    public EditorTest() 
    {
    editorPane = new JEditorPane();     
    editorPane.setContentType("text/HTML");        
    getContentPane().add(editorPane, BorderLayout.CENTER);
    editorPane.setEditorKit(new HTMLEditorKit());


    Action a = new StyledEditorKit.ForegroundAction("RedColor", Color.RED);        
    editorPane.getActionMap().put("RedColor", a);

    JToolBar bar = new JToolBar();

    JButton button = new JButton("blue");
    button.addActionListener(new StyledEditorKit.ForegroundAction (
                        "set-foreground-red", Color.blue));


    bar.add(editorPane.getActionMap().get("font-size-12")).setText("12");
    bar.add(button);
    bar.add(editorPane.getActionMap().get("RedColor")).setText("Red");

    getContentPane().add(bar, BorderLayout.NORTH);
    setSize(650,600);
    setVisible(true);

    File file = new File("testFile.html");
    FileReader reader = null;
    try
    {
        reader = new FileReader(file);
        editorPane.read(reader, null);
    }
    catch (IOException ex){}      
    }
}
解决方案

Using the sscce below, I am unable to reproduce the effect you describe. Such anomalies may arise if Swing GUI objects are not constructed and manipulated only on the event dispatch thread. The example uses EventQueue.invokeLater() accordingly.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTMLEditorKit;

/** http://stackoverflow.com/questions/8523445 */
public class StyledEditorTest extends JFrame {

    public StyledEditorTest() {
        JEditorPane editorPane = new JEditorPane();
        editorPane.setContentType("text/HTML");
        editorPane.setEditorKit(new HTMLEditorKit());
        editorPane.setText("<hr>Welcome to <b>StackOverFlow!</b><hr>");
        JToolBar bar = new JToolBar();
        bar.add(new StyledEditorKit.ForegroundAction("Red", Color.red));
        bar.add(new StyledEditorKit.ForegroundAction("Blue", Color.blue));
        bar.add(new StyledEditorKit.FontSizeAction("12", 12));
        bar.add(new StyledEditorKit.FontSizeAction("14", 14));
        bar.add(new StyledEditorKit.FontSizeAction("16", 16));
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.add(bar, BorderLayout.NORTH);
        this.add(editorPane, BorderLayout.CENTER);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new StyledEditorTest();
            }
        });
    }
}

这篇关于阅读HTML时,在JEditorPane中应用ForegroundActions的性能不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 08:03