自定义ScrollPane的绘画问题 [英] Painting problems with custom ScrollPane

查看:102
本文介绍了自定义ScrollPane的绘画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为使我的其他问题保持秩序,建议单独询问以下内容:

To keep order in my other question it was recommended to ask the following seperately:

下面是测试类的代码,我在其中添加了自定义JScrollBar

The following is the code of the test-class, in which I add my custom JScrollBar

public class TestScrollBar {

    private static JFrame           f;
    private static Container        pane;
    private static JScrollPane      scroll1;
    private static JScrollBar       scrollbar;
    private static JPanel           panel;
    private static JList<String>    list1;

    public static void main(String[] arg){
        createBasic();
        createComponents();
        f.setVisible(true);
    }

    private static void createBasic(){
        f = new JFrame("ScrollbarTest");
        f.setBounds(100,100,300,300);

        pane = f.getContentPane();
        pane.setLayout(null);

        panel = new JPanel();
        panel.setBackground(Color.GREEN);
        panel.setLayout(null);
        panel.setBounds(50,50,200,150);
    }

    private static void createComponents(){

        String[] data = {"ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg"};
        list1 = new JList<String>(data);
        list1.setBackground(new Color(0,0,0,0));

        scrollbar = new JScrollBar();
        CustomScrollBarUI ui = new CustomScrollBarUI();
        scrollbar.setUI(ui);
        scrollbar.setOpaque(false);

        scroll1 = new JScrollPane(list1);
        scroll1.setBounds(20,20,160,110);
        scroll1.setOpaque(false);
        scroll1.getViewport().setOpaque(false);
        scroll1.setVerticalScrollBar(scrollbar);

        panel.add(scroll1);
        pane.add(panel);
    }
}

自定义ScrollBarUI可以在此处看到:自定义JScrollBar-Thumb是画,但不动 我唯一改变的(感谢麦加林)是 g.drawImage(img, thumbBounds.x, thumbBounds.y, new Color(255,255,255,0), null);

The custom ScrollBarUI can be seen here: Custom JScrollBar-Thumb is painted, but doesn't move The only thing I changed (thanks to mgarin) is g.drawImage(img, thumbBounds.x, thumbBounds.y, new Color(255,255,255,0), null);

然后发生以下情况,如果我移动拇指(请不要介意设计,这只是为了测试一些不透明的东西...)

And the following happens, if I move the thumb (Please don't mind about the design, it is just to test some Opaque stuff...)

http://tinypic.com/r/sz94pf/6

推荐答案

您将列表的背景设为透明;

You made the background of the list transparent;

list1.setBackground(new Color(0,0,0,0));

如果删除那条线,它会很好地绘制.

If you remove that line it draws fine.

关于另一个注意事项:如果要为单元格提供自定义的背景色,请尝试使用自定义ListCellRenderer,方法是使用JList上的setCellRenderer()方法.然后,您可以设置返回的组件的背景颜色.

On another note: if you want to give your cells a custom background colour, try using a custom ListCellRenderer, using the setCellRenderer() method on the JList. You can then set the background colour of the component you return.

这篇关于自定义ScrollPane的绘画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆