【问题标题】:Who owns the color of the edges of a Java Swing scrollable viewport?谁拥有 Java Swing 可滚动视口边缘的颜色?
【发布时间】:2011-09-23 05:33:43
【问题描述】:

我有一个 JScrollPane 和一个 JPanel,它实现了 Scrollable 作为它的视口视图。当我将 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();
      }
    });
  }
}

【问题讨论】:

    标签: java swing awt jscrollpane scrollable


    【解决方案1】:
    scrollPane.getViewport().setBackground(Color.yellow);
    

    【讨论】:

      【解决方案2】:

      全部属于JViewport,你可以画任何想要的颜色,如图here

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-19
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多