我正在尝试使JTextField的背景透明,这样它下面的JLabel仍然可见,但无论何时在JTextField中输入文本,您都可以看到文本。这就是我现在所拥有的。
在下图中,JTextField背景设置为黑色。

理论上,如果JTextField的背景是透明的,它应该是这样的。

因此,我的问题是如何使JTextField的背景透明?
发布于 2014-03-28 06:29:58
这个例子简单地使用了setOpaque(false)。标签文本始终可见。我使用Java1.7和1.8对其进行了测试。所以,如果它对你不起作用,你还做了什么,初始化你的框架?
public class TextField extends javax.swing.JFrame {
public TextField() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jLabel1.setText("Test");
getContentPane().add(jLabel1);
jLabel1.setBounds(60, 40, 70, 14);
jTextField1.setText("jTextField1");
jTextField1.setOpaque(false);
getContentPane().add(jTextField1);
jTextField1.setBounds(50, 30, 90, 40);
pack();
}// </editor-fold>
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TextField().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}发布于 2015-06-14 20:52:38
更改外观和感觉设置为金属或窗口它将工作,如果你已经设置为透明的Nimbus laf使textareas背景可见,只要程序运行
发布于 2021-04-12 18:57:52
看起来Nimbus不支持JTextField、JTextArea等格式的透明背景。下面的代码不是让背景变得透明,而是让组件设置背景颜色以匹配其父组件的背景:
private static final HierarchyListener _hl = new HierarchyListener() {
@Override
public void hierarchyChanged(HierarchyEvent e) {
Component c = e.getComponent();
for (Component p = c; p != null; p = p.getParent()) {
if (p.isOpaque()) {
int bk = p.getBackground().getRGB();
c.setBackground(new Color(bk));
break;
}
}
}
};
public static void makeComponentCopyParentBackground(Component c) {
c.removeHierarchyListener(_hl); // Guard against client calling multiple times.
c.addHierarchyListener(_hl);
}https://stackoverflow.com/questions/22700216
复制相似问题