維持秩序,我的其他問題繪畫的問題,有人建議要問seperately如下:定製的ScrollPane
以下是測試類的代碼,我在其中添加我的自定義的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可以在這裏看到:Custom JScrollBar-Thumb is painted, but doesn't move 我改變(感謝mgarin)的唯一事情是 g.drawImage(img, thumbBounds.x, thumbBounds.y, new Color(255,255,255,0), null);
和下面的情況發生,如果我移動日UMB(請不要介意設計,它只是爲了測試一些不透明的東西...)
你能發表一個實際的問題嗎?我在上面的原文中沒有看到一個,只是描述性的東西。另外,我爲您的問題添加了一個「java」標記。 –
從圖片判斷,滾動容器在滾動時不會更新,並且會從前面的內容中獲得這些線條。您可能只需在滾動結束時執行'SwingUtilities.updateComponentTreeUI(scroll1)'操作,以便正確更新。 – Ozzy