一千萬個為什麽

搜索

沒有滾動條的JScrollPanel

我正在嘗試使用JScrollPanel來顯示可能對於包含Jpanel而言太大的JPanel。

我不想顯示滾動條(是的,這是一個有問題的UI設計,但它是我對客戶想要的最好的猜測。我們在應用程序的其他地方使用相同的想法,我覺得這個案例給了我足夠的是時候思考一下,如果我能以更好的方式做到這一點,但如果你有更好的想法我可以接受它作為答案。)

First attempt: set verticalScrollBarPolicy to NEVER. Result: Scrolling using mouse wheel doesn't work.

Second attempt: set the scrollbars to null. Result: Scrolling using mouse wheel doesn't work.

Third attempt: set scrollbars visible property to false. Result: It is immidiately set visible by Swing.

Fourth attempt: inject a scrollbar where setVisible is overridden to do nothing when called with true. Result: Can't remember exactly, but I think it just didn't work.

Fifth attempt: inject a scrollbar where setBounds are overridden. Result: Just didn't look nice. (I might have missed something here, though.)

Sixth attempt: ask stackoverflow. Result: Worked like a charm this time.

滾動條返回後滾動工作正常。

最佳答案

scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(0,0));

將隱藏滾動條,讓您使用鼠標滾輪滾動。

轉載註明原文: 沒有滾動條的JScrollPanel