java 如何使 jTextArea 透明背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30435186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make jTextArea Transparent background
提问by hyunjin-Kim
I want to make a transparent background jTextArea. I try to setBackground(new color(0,0,0,0)); jTextField is Working, jTextArea didn't working.
我想制作一个透明的背景 jTextArea。我尝试 setBackground(new color(0,0,0,0)); jTextField 有效,jTextArea 无效。
like this code.
喜欢这个代码。
// Not working.. Just remains gray.
jScrollPane1.setOpaque(false);
jScrollPane1.setBackground(new Color(0,0,0,0));
jTextArea1.setOpaque(false);
jTextArea1.setBackground(new Color(0,0,0,0));
// Working.. As it wants to be transparent.
jTextField1.setOpaque(false);
jTextField1.setBackground(new Color(0,0,0,0));
How can I jTextArea transparent background?
我如何才能 jTextArea 透明背景?
Thanks & Regards.
感谢和问候。
采纳答案by Jean-Baptiste Yunès
A JScrollPane
is a composed component, it controls/contains a JViewport
which is the component that does the drawings. See API:
AJScrollPane
是一个组合组件,它控制/包含一个JViewport
执行绘图的组件。见API:
A common operation to want to do is to set the background color that will be used if the main viewport view is smaller than the viewport, or is not opaque. 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.
想要做的一个常见操作是设置如果主视口视图小于视口或不透明时将使用的背景颜色。这可以通过设置视口的背景颜色来实现,通过 scrollPane.getViewport().setBackground()。设置视口而不是滚动窗格的颜色的原因是默认情况下 JViewport 是不透明的,这意味着它将使用其背景颜色完全填充其背景。因此,当 JScrollPane 绘制其背景时,视口通常会在其上绘制。
So you should change the opaque and color properties of the JViewport
as well. You can access it with jScrollPane1.getViewport()
.
所以你也应该改变 opaque 和 color 属性JViewport
。您可以使用jScrollPane1.getViewport()
.
回答by josemr
The following worked for me.
以下对我有用。
JTextArea textArea = new JTextArea();
textArea.setOpaque(false);
textArea.setBackground(new Color(red, green, blue, alpha));
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.getViewport().setOpaque(false);
scrollPane.setOpaque(false);
回答by mr mcwolf
This is example include 50% transparent
这是示例,包括 50% 透明
JTextArea textArea = new JTextArea();
textArea.setOpaque(false);
JScrollPane scrollPane = new JScrollPane(textArea) {
@Override
protected void paintComponent(Graphics g) {
try {
Composite composite = ((Graphics2D)g).getComposite();
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
((Graphics2D)g).setComposite(composite);
paintChildren(g);
}
catch(IndexOutOfBoundsException e) {
super.paintComponent(g);
}
}
};
scrollPane.getViewport().setOpaque(false);
scrollPane.setOpaque(false);
editsorry for the bug. This is work.
编辑抱歉这个错误。这是工作。