如何在JGraphX中将组件背景设置为特定颜色?(How do I set the component background to a specific color in JGraphX?)

我想让JGraphX图形组件( https://github.com/jgraph/jgraphx )的背景成为一种特定的颜色。 我尝试了任何Swing组件的标准调用:

graphComponent.setBackground(Color.BLACK);

但这没有效果。 我试图强制重新组装组件,没有运气。 呼叫是否不正确,还是有某种特定方式强制刷新?

I want to make the background of a JGraphX graph component (https://github.com/jgraph/jgraphx) a specific color all over. I tried the standard call for any Swing component:

graphComponent.setBackground(Color.BLACK);

but this has no effect. I tried forcing a repaint of the component also, no luck. Is the call incorrect, or is there some specific way to force a refresh?

最满意答案

由于mxGraphComponent扩展了JScrollPane更改了视图端口的背景:

graphComponent.getViewport().setOpaque(true); graphComponent.getViewport().setBackground(Color.BLACK);

来自JScrollPane文档:

这可以通过scrollPane.getViewport()。setBackground()设置视口的背景颜色来实现。 设置视口颜色而不是滚动窗格的原因是默认情况下JViewport是不透明的,除其他外,它意味着它将使用其背景颜色完全填充其背景。 因此,当JScrollPane绘制其背景时,视口通常会绘制它。

Since mxGraphComponent extends JScrollPane change the background of the view port:

graphComponent.getViewport().setOpaque(true); graphComponent.getViewport().setBackground(Color.BLACK);

From JScrollPane docs:

This can be accomplished by setting the background color of the viewport, via scrollPane.getViewport().setBackground(). The reason for setting the color of the viewport and not the scrollpane is that by default JViewport is opaque which, among other things, means it will completely fill in its background using its background color. Therefore when JScrollPane draws its background the viewport will usually draw over it.

更多推荐