Transparency for JTextField not working

后端 未结 3 2034
离开以前
离开以前 2021-01-28 16:08

I\'m working on a log in server & my JTextFields aren\'t transparent when I set Opaque to false.

My code:

//username  
             


        
3条回答
  •  难免孤独
    2021-01-28 16:59

    Basically, the UI delegate of the text field paints not only the text but also the field area (within the border) regardless of the opaque setting.

    What you can do, is set the background color to a transparent value, something like new Color(0, 0, 0, 0) for example, which is fully transparent.

    For example...

    JTextField jUsername = new JTextField(10);  
    jUsername.setBounds(520, 284, 190, 25);  
    jUsername.setBackground(new Color(0, 0, 0, 0));
    jUsername.setOpaque(false);  
    jUsername.setBorder(null);  
    getContentPane().add(jUsername);  
    
    //password  
    JTextField jPassword = new JTextField(15);  
    jPassword.setBounds(520, 374, 190, 25);  
    jPassword.setBackground(new Color(0, 0, 0, 0));
    jPassword.setOpaque(false);  
    jPassword.setBorder(null); 
    //jPassword.setBackground(new Color(Color.TRANSLUCENT));
    getContentPane().add(jPassword);
    

    You can affect the transparency of a color by changing the last parameter, for example new Color(255, 255, 255, 128) would white, 50% transparent...

    You may also wish to change the caret color, take a look at JTextComponent#setCaretColor for more details

提交回复
热议问题