在有背景的框架中使 JLabel 背景透明

Make JLabel background transparent in a frame with background

我的 JFrame 上有一个 JLabel 和一个带有图片背景的 JFrame。问题是虽然我的 JLabel 是不透明的,但它仍然有一个令人讨厌的灰色背景。
这是我的代码:

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class MainFrame extends JFrame {

    private JTextField username;
    private JPasswordField password;
    private JLabel passwordLbl;
    private JLabel usernameLbl;
    private GridBagConstraints gc;

    public MainFrame() {

        username = new JTextField(10);
        password = new JPasswordField(10);
        passwordLbl = new JLabel("Password: ");
        usernameLbl = new JLabel("Username: ");

        usernameLbl.setOpaque(true);
        passwordLbl.setOpaque(true);

        setLayout(new GridBagLayout());

        gc = new GridBagConstraints();
        gc.weightx = 1;
        gc.weighty = 1;

        gc.gridx = 1;
        gc.gridy = 0;
        add(username, gc);

        gc.gridx = 1;
        gc.gridy = 1;
        add(password, gc);

        gc.gridx = 0;
        gc.gridy = 0;
        add(usernameLbl, gc);

        gc.gridx = 0;
        gc.gridy = 1;
        add(passwordLbl, gc);

        setSize(400, 600);
        setLocation(400, 50);

        setUndecorated(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }

    @Override
    public void paint(Graphics arg0) {
        Image img = getToolkit().getImage("pics/blue_and_red.jpg");

        arg0.drawImage(img, 0, 0, getWidth(), getHeight(), this);

        username.repaint();
        password.repaint();
        usernameLbl.setOpaque(true);
        usernameLbl.repaint();
        passwordLbl.repaint();

    }
}

提前致谢。

您的绘画方法完全错误是您出现问题的原因。不要在油漆中绘制标签,但 do 在其中调用 super.paint(g)

更好的是,不要在 JFrame 的 paint 方法中绘制,而是在 JPanel 的 paintComponent 方法中绘制,然后在那里调用 super 的方法。

public class MainPanel extends JPanel {

    private JTextField username;
    private JPasswordField password;
    private JLabel passwordLbl;
    private JLabel usernameLbl;
    private GridBagConstraints gc;
    private Image img;

    public MainPanel() {
        img = getToolkit().getImage("pics/blue_and_red.jpg");

        username = new JTextField(10);
        password = new JPasswordField(10);
        passwordLbl = new JLabel("Password: ");
        usernameLbl = new JLabel("Username: ");

        // usernameLbl.setOpaque(true);
        // passwordLbl.setOpaque(true);

        setLayout(new GridBagLayout());

        gc = new GridBagConstraints();
        gc.weightx = 1;
        gc.weighty = 1;

        gc.gridx = 1;
        gc.gridy = 0;
        add(username, gc);

        gc.gridx = 1;
        gc.gridy = 1;
        add(password, gc);

        gc.gridx = 0;
        gc.gridy = 0;
        add(usernameLbl, gc);

        gc.gridx = 0;
        gc.gridy = 1;
        add(passwordLbl, gc);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null) {
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }

        // username.repaint();
        // password.repaint();
        // usernameLbl.setOpaque(true);
        // usernameLbl.repaint();
        // passwordLbl.repaint();
    }

    private static void createAndShowGui() {
      // create JFrame
      JFrame frame = new JFrame("BUI");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      // add our MainPanel to the JFrame
      frame.getContentPane().add(new MainPanel());
      frame.pack(); // pack it
      frame.setLocationByPlatform(true);
      frame.setVisible(true); // show it
   }

   public static void main(String[] args) {
      // this is for starting our Swing app on the event thread
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

}

最重要的是,请阅读 Painting with Swing 标准 Swing 教程,因为那里有很好的解释。