谁拥有 Java Swing 可滚动视口的边缘颜色?
我有一个 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它全部属于
JViewport
,你可以在其中绘制任何想要的颜色,如图此处。It all belongs to the
JViewport
, which you can paint any desired color, as shown here.