Java 使 JTextField 背景透明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22700216/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:20:48  来源:igfitidea点击:

Making a JTextField background transparent

javaswingjtextfield

提问by RuneRebellion

I'm trying to make the background for a JTextField transparent, that way so the JLabel underneath it is still visible, but whenever entering text into the JTextField you can see the text. Here's what I have now basically.

我试图使 JTextField 的背景透明,这样它下面的 JLabel 仍然可见,但是每当在 JTextField 中输入文本时,您都可以看到文本。这就是我现在所拥有的。

The JTextField background is set to black in the below picture. enter image description here

JTextField 背景在下图中设置为黑色。 在此处输入图片说明

In theory, if the JTextField's background was transparent it should look like this. enter image description here

理论上,如果 JTextField 的背景是透明的,它应该是这样的。 在此处输入图片说明

So, my question is how would I make the JTextField's background transparent?

所以,我的问题是如何使 JTextField 的背景透明?

采纳答案by wumpz

This example does simple use setOpaque(false). The labels text is always visible. I tested it with Java 1.7 and 1.8. So if it does not work for you, what else did you do, to initalize your frame?

这个例子使用简单setOpaque(false)。标签文本始终可见。我用 Java 1.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                   
}

回答by Ankit

Change look and feel setting to metal or window It will work if u have set it transparent already Nimbus laf makes textareas backgroung visible as soon as the program runs

将外观和感觉设置更改为金属或窗口 如果您已经将其设置为透明,它将起作用 Nimbus laf 在程序运行后立即使 textareas 背景可见