Java - 透明 JScrollPane
我有一个 JTextArea,它位于 JScrollPane 之上。无论如何,我知道我可以使用 getViewPort() 方法来设置视口的不透明属性,但我似乎找不到任何迹象表明如何在任何地方执行此操作。
这是我到目前为止所拥有的:
if (e.getKeyCode() == KeyEvent.VK_F)
{
if (sp.isVisible())
{
sp.setVisible(false);
}
else
{
sp.setVisible(true);
}
}
I have a JTextArea and it's riding on top of a JScrollPane. Anyways, I know I can use the getViewPort()
method to set the opaque property of the viewport, but I cannot seem to find any sign of how to do that anywhere.
Here is what I have so far:
if (e.getKeyCode() == KeyEvent.VK_F)
{
if (sp.isVisible())
{
sp.setVisible(false);
}
else
{
sp.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用
setOpaque(false)
使其透明。在 JScrollPane 和 ViewPort 上都调用它。如果您也希望透明,您还必须在 JTextArea 上调用
setOpaque(false)
。You need to use
setOpaque(false)
to make it transparent. Call that both on the JScrollPane, and on it's ViewPort.You'll also have to call
setOpaque(false)
on the JTextArea, if you want that transparent as well.您与@Serplat 的对话表明您可能混淆了不透明度和透明度。
不透明度是一个布尔属性用于优化绘图的 Swing 组件的数量:
true
:组件同意绘制其矩形边界内包含的所有位。false
:组件不保证绘制其矩形边界内的所有位。透明度是一种合成数字图像的方法,如图所示在此示例中。
考虑这种区别可能有助于澄清您的问题或集中您搜索更多信息。
附录:基于@camickr的示例,下面的示例显示了一个“粘”在视口上的蓝色方块,而灰色棋盘可能会滚动到它上面。
Your colloquy with @Serplat suggests that you may be confounding opacity and transparency.
Opacity is a boolean property of Swing components used to optimize drawing:
true
: The component agrees to paint all of the bits contained within its rectangular bounds.false
: The component makes no guarantees about painting all the bits within its rectangular bounds.Transparency is a means of compositing digital images, as seen in this example.
Considering the distinction may help to clarify your question or focus your search for more information.
Addendum: Based on @camickr's example, the example below shows a blue square that "sticks" to the viewport, while the gray checkerboard may be scrolled over it.
JScrollpane 透明背景代码。
Code for JScrollpane Transparent Background.