我在JFrame上有一个JLabel,在我的JFrame上有一个带有图片背景的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();
}
}
提前致谢。
答案 0 :(得分:2)
你的油漆方法完全是你的问题的原因。不要在油漆区中绘制标签,但执行在那里调用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教程,因为它在那里得到了很好的解释。