JEdi​​torPane半透明背景选择bug

时间:2013-12-01 11:25:15

标签: java swing user-interface jeditorpane visual-glitch

我有一个JEditorPane,在JScrollPane中(两者都是透明的)。这些都在具有半透明背景的JPanel中。当我选择JEditorPane内的文本时,我得到了这个奇怪的错误: enter image description here

所以其他元素不在同一个JPanel中,“鬼”围绕选择,更多见于此截图:

Java GUI对我来说是第一次,我只做了没有gui的服务器端应用程序。

小组代码:

    @SuppressWarnings("serial")
    public class NewsPanel extends JPanel {

    private JEditorPane newsArea;


    public NewsPanel() {
        setLayout(new BorderLayout());
        setBackground(new Color(226, 0, 0, 179));
        loadContent();
        setSize(500,400);
    }

    private void loadContent() {
        newsArea = new JEditorPane();
        newsArea.setEditable(false);
        newsArea.setBorder(BorderFactory.createEmptyBorder());
        newsArea.setSelectionColor(Color.GRAY);
        newsArea.setOpaque(false);
        newsArea.setBackground(new Color(0, 0, 0, 0));

        HTMLEditorKit kit = new HTMLEditorKit();
         StyleSheet styleSheet = kit.getStyleSheet();
         styleSheet.addRule("A {color:#0088CC}");
         styleSheet.addRule("#newsHeader {font-weight:bold;font-size:14px;color:#339933;}");
         styleSheet.addRule("#newsBody {font-size:10px;padding-left:20px;}");
         newsArea.setEditorKit(kit);


        JScrollPane scrollPane = new JScrollPane(newsArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(scrollPane, BorderLayout.CENTER);

        scrollPane.getViewport().setBackground(new Color(0, 0, 0, 0));
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setViewportBorder(BorderFactory.createEmptyBorder());
        scrollPane.setOpaque(false);
        scrollPane.setBackground(new Color(0, 0, 0, 0));
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }

    @Override
    public boolean isOptimizedDrawingEnabled() {
        return false;
    }

    public void reload() {
        removeAll();
        loadContent();
        validate();
        repaint();
    }
}

任何想法如何解决?我确信这很简单,但就像我说的,我是UI的新手

1 个答案:

答案 0 :(得分:0)

  

这些是在具有半透明背景的JPanel中。当我选择JEditorPane中的文本时,我得到了这个奇怪的错误:

查看Backgrounds With Transparency可能出现的问题以及几个解决方案。