gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:39:46 26 4
gpt4 key购买 nike

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

}
}

提前致谢。

最佳答案

你的绘画方法完全是错误的,这就是你的问题的原因。不要在 Paint 中绘制标签,但一定在其中调用 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 教程,因为那里都有很好的解释。

关于java - 使 JLabel 背景在有背景的框架中透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30309022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com