2011-06-27 82 views
3

我有一個JscrollPane,其JPanel實現了Scrollable作爲它的視口視圖。當我將ScrollableTracksViewportHeight和ScrollableTracksViewportWidth設置爲false時,我的面板保持其首選大小。好。問題是,我無法弄清誰在滾動面板周圍擁有空間。我覺得我已經嘗試改變每個組件和對話框的背景,似乎沒有任何東西可以做到。我該如何將醜陋的灰色變成霓虹黃色這樣有趣的顏色?誰擁有Java Swing可滾動視口邊緣的顏色?

這裏就是我談論的截圖:

這裏就是我想:

enter image description here

下面是複製的第一圖像的代碼:

package components; 

import java.awt.*; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class DialogWithScrollPane extends JFrame { 

    ScrollablePanel scrollablePanel; 

    public DialogWithScrollPane() { 
    super(); 

    setResizable(true); 

    Container pane = getContentPane(); 

    scrollablePanel = new ScrollablePanel(); 

    final JScrollPane scrollPane = new JScrollPane(); 
    scrollPane.setPreferredSize(new Dimension(300,300)); 
    scrollPane.setViewportView(scrollablePanel); 
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    pane.add(scrollPane); 

    setMinimumSize(new Dimension(300, 300));   
    setVisible(true); 
    } 

    class ScrollablePanel extends JPanel implements Scrollable { 

    public ScrollablePanel() { 
     super(); 

     setBackground(Color.red); 
     setPreferredSize(new Dimension(200, 100)); 
    } 

    public Dimension getPreferredScrollableViewportSize() { 
     return getPreferredSize(); 
    } 

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { 
     return 0; 
    } 

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { 
     return 0; 
    } 

    public boolean getScrollableTracksViewportHeight() { 
     return false; 
    } 

    public boolean getScrollableTracksViewportWidth() { 
     return false; 
    } 

    } 

    public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
     new DialogWithScrollPane(); 
     } 
    }); 
    } 
} 

回答

4
scrollPane.getViewport().setBackground(Color.yellow); 
+0

爲清晰起見+1! – trashgod

4

It 全部屬於JViewport,您可以繪製任何所需的顏色,如here所示。

+0

有點複雜,但爲什麼不是+1 – mKorbel

+0

@mKorbel:有罪,如收費;我懇求演示代碼。 :-) – trashgod

+0

然後更好的例子http://stackoverflow.com/questions/6051755/java-wait-cursor-display-problem/6051893#6051893 :-) – mKorbel