ビューポートビューとしてScrollableを実装するJPanelを備えたJScrollPaneがあります。ScrollableTracksViewportHeightとScrollableTracksViewportWidthをfalseに設定すると、パネルは希望のサイズのままになります。良い。問題は、スクロール可能なパネルの周りのスペースを誰が所有しているのかわからないことです。すべてのコンポーネントとダイアログの背景を変更しようとしたように感じますが、何も実行されていないようです。醜い灰色をネオンイエローのような楽しい色に変えるにはどうすればよいですか?
これが私が話していることのスクリーンショットです:
これが私が欲しいものです:
そして、最初の画像を複製するコードは次のとおりです。
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();
}
});
}
}