Transparency for JTextField not working

后端 未结 3 1978
离开以前
离开以前 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:49

    no idea what you tried, for better help sooner post an SSCCE, short. runnable, compilable with setBackground instead of Image

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class LabelImageText extends JPanel {
    
        private static final long serialVersionUID = 1L;
    
        public LabelImageText() {
            JTextField jUsername = new JTextField(10);
            jUsername.setText("MyText");
            jUsername.setOpaque(false);
            //jUsername.setBorder(null);
            add(jUsername);
            JTextField jPassword = new JTextField(15);
            jPassword.setText("MyText");
            jPassword.setOpaque(false);
            //jPassword.setBorder(null);
            add(jPassword);
            setBackground(Color.RED);
        }
    
        private static void createAndShowUI() {
            JFrame frame = new JFrame("set Opaque");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new LabelImageText());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createAndShowUI();
                }
            });
        }
    }
    

提交回复
热议问题