[英]How to set JScrollPane Background transparent and JScrollBarr Visible with JTextArea?
This is what I want to achieve: 这是我要实现的目标:
What I tried: 我试过的
jTextArea.setOpaque(false);
this makes JTextArea
transparent. 这使JTextArea
透明。 jScrollPane.setOpaque(false);
this gives no effect 这没有效果 then I tried this which hides both JScrollPane
and JTextArea
. 然后我尝试了这同时隐藏JScrollPane
和JTextArea
。
jScrollPane.getViewPort().setOpaque(false); jScrollPane.setOpaque(false);
then I tried this which hides both JScrollPane
and JTextArea
. 然后我尝试了这同时隐藏JScrollPane
和JTextArea
。
jScrollPane.setViewPort(new MyViewPort()); class MyViewPort() extends JViewPort{ public MyViewPort(){ setOpaque(false); } }
What I want to achieve is JScrollPane
background transparent and transparent JTextArea
where I should able to add text and visible JScrollPane
. 我要实现的是JScrollPane
背景透明和透明的JTextArea
,我应该可以在其中添加文本和可见的JScrollPane
。
Update: I did like this I can add text in textArea but the jscrollPane is not transparent: 更新:我这样做是可以在textArea中添加文本,但jscrollPane不透明:
public class TransparentBackground extends javax.swing.JFrame {
public TransparentBackground() {
jScrollPane = new javax.swing.JScrollPane();
jTextArea = new javax.swing.JTextArea();
lblBackground = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jTextArea.setColumns(20);
jTextArea.setRows(5);
jScrollPane.setViewportView(jTextArea);
jScrollPane.getViewport().setOpaque(false);
jScrollPane.setOpaque(false);
jTextArea.setOpaque(false);
getContentPane().add(jScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 40, 580, 300));
lblBackground.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bg.png"))); // NOI18N
getContentPane().add(lblBackground, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 680, 390));
pack();
}
And When I use 当我使用
jScrollPane.setViewPort(new MyViewPort());
class MyViewPort() extends JViewPort{
public MyViewPort(){
setOpaque(false);
}
}
Both textArea and JScrollPane disappears(transparent) but need jTextArea transparent and editable or can add text in it. textArea和JScrollPane都消失(透明),但需要jTextArea透明且可编辑,或者可以在其中添加文本。
When I set custom view port result is like this 当我设置自定义视图端口结果是这样的
I don't know whats wrong with privious code, may be due to the use of drag and drop. 我不知道私有代码有什么问题,可能是由于拖放的缘故。 Here is code that worked. 这是起作用的代码。 Thank you Camickr and MadProgrammer for you suggestion. 谢谢Camickr和MadProgrammer的建议。 :) :)
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;
public class TransparentBackground1 extends JFrame {
private javax.swing.JScrollPane jScrollPane;
private javax.swing.JTextArea jTextArea;
private javax.swing.JLabel lblBackground;
public TransparentBackground1() {
setPreferredSize(new Dimension(675, 375));
jScrollPane = new JScrollPane();
jTextArea = new JTextArea();
lblBackground = new JLabel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
jTextArea.setColumns(20);
jTextArea.setRows(5);
jScrollPane.setViewportView(jTextArea);
//Code To make transparent
jScrollPane.getViewport().setOpaque(false);
jScrollPane.setOpaque(false);
jTextArea.setOpaque(false);
lblBackground.setIcon(new ImageIcon(getClass().getResource("/bg.png"))); // NOI18N
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TransparentBackground().setVisible(true);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.