谁拥有 Java Swing 可滚动视口的边缘颜色?

发布于 2024-11-17 10:10:20 字数 2134 浏览 2 评论 0原文

我有一个 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();
      }
    });
  }
}

I have a JScrollPane with a JPanel that implements Scrollable as its viewport view. When I set ScrollableTracksViewportHeight and ScrollableTracksViewportWidth to false, my panel stays at its preferred size. Good. The problem is that I can't figure out who owns the space around the scrollable panel. I feel like I've tried changing the background of every component and dialog, and nothing seems to do it. How can I change the ugly grey to a fun color like neon yellow?

Here's a screenshot of what I'm talking about:

Here's what I want:

enter image description here

And here's the code to duplicate the first image:

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();
      }
    });
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2024-11-24 10:10:20
scrollPane.getViewport().setBackground(Color.yellow);
scrollPane.getViewport().setBackground(Color.yellow);
一生独一 2024-11-24 10:10:20

全部属于 JViewport,你可以在其中绘制任何想要的颜色,如图此处

It all belongs to the JViewport, which you can paint any desired color, as shown here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文