【问题标题】:Make JLabel background transparent in a frame with background [closed]在有背景的框架中使 JLabel 背景透明 [关闭]
【发布时间】:2015-05-18 17:03:01
【问题描述】:

我的 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();

    }
}

提前致谢。

【问题讨论】:

  • 嗯.... 让它 non - 不透明。严重地。或者更好的是,甚至不要将 opaque 设置为 false,因为 non-opaque 是默认设置。
  • 默认情况下它不起作用,它是非不透明的。
  • 因为你的paint方法都搞砸了。哎呀。
  • 不要在绘画时加载图像,而是在类的构建过程中加载一次。并且不要从绘画中调用 repaint
  • @HovercraftFullOfEels 令人沮丧的是,人们还没有准备好听取智者的建议;)

标签: java swing graphics transparency jlabel


【解决方案1】:

您的绘画方法完全错误是您出现问题的原因。不要在油漆中绘制标签,而是这样做在其中致电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 教程,因为那里的解释都很好。

【讨论】:

  • 这是一个 JFrame,不幸的是没有paintComponent。
  • 我用 super.paint(g) 试过了,没用。
  • @Iman:正如我所说,不要在 JFrame 中绘制,这是一件非常危险的事情。在 JFrame 中显示的 JPanel 中进行绘制。这在 Swing 绘画教程中都有解释。
  • @Iman: start here
  • 它通过在 JFrame 中添加一个 JPanel 并在 paintComponent 的末尾使用一个 repaint() 覆盖 JPanel 的 paintComponent 方法来工作。 @Hovercraft:非常感谢;)
猜你喜欢
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多