[英]How to set JScrollPane Background transparent and JScrollBarr Visible with JTextArea?
這是我要實現的目標:
我試過的
jTextArea.setOpaque(false);
這使JTextArea
透明。 jScrollPane.setOpaque(false);
這沒有效果 然后我嘗試了這同時隱藏JScrollPane
和JTextArea
。
jScrollPane.getViewPort().setOpaque(false); jScrollPane.setOpaque(false);
然后我嘗試了這同時隱藏JScrollPane
和JTextArea
。
jScrollPane.setViewPort(new MyViewPort()); class MyViewPort() extends JViewPort{ public MyViewPort(){ setOpaque(false); } }
我要實現的是JScrollPane
背景透明和透明的JTextArea
,我應該可以在其中添加文本和可見的JScrollPane
。
更新:我這樣做是可以在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();
}
當我使用
jScrollPane.setViewPort(new MyViewPort());
class MyViewPort() extends JViewPort{
public MyViewPort(){
setOpaque(false);
}
}
textArea和JScrollPane都消失(透明),但需要jTextArea透明且可編輯,或者可以在其中添加文本。
我不知道私有代碼有什么問題,可能是由於拖放的緣故。 這是起作用的代碼。 謝謝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.