Swing设置透明问题

年轻的魅力 2014-08-06 11:38:27
我编写了一个Swing登录框,有设置背景,但是外层的jpanel会把背景覆盖掉,感觉很丑,要怎么设置透明,各位大神帮帮忙!
代码如下:

public class UserLogin extends JFrame{

private Graphics g;

//构造函数
public UserLogin(){
//加载背景图片
final ImageIcon icon = new ImageIcon("D:\\workspace\\images\\bg_login.jpg");
JPanel container = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(icon != null){
g2d.drawImage(icon.getImage(), 0, 0, getWidth(), this.getHeight(), this);
}
}
};

//logo图片
JLabel lblIamge = new JLabel();
ImageIcon admin_p = new ImageIcon("D:\\workspace\\images\\admin_p.gif");
lblIamge.setIcon(admin_p);

//用户名、密码、登录
JTextField tfUserName = new JTextField(15);
JPasswordField pfPassword = new JPasswordField(15);
JButton bntLogin = new JButton("登录");
tfUserName.setBackground(null);
tfUserName.setOpaque(false);
pfPassword.setOpaque(false);

JPanel center_right = new JPanel();

center_right.setLayout(new GridLayout(3,1,10,10));
center_right.setBackground(null);
center_right.setOpaque(false);
center_right.add(tfUserName);
center_right.add(pfPassword);
center_right.add(bntLogin);


JPanel center = new JPanel();
center.add(lblIamge);
center.add(center_right);


container.add(center);

//this.setBounds(0,0,400,300);
this.setSize(400,300); //设置窗口大小
this.setLocationRelativeTo(null); //设置窗口居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(container);
this.setVisible(true);

}

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new UserLogin();

}
});
}

运行后的效果:

...全文
270 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanghongjy 2014-08-07
  • 打赏
  • 举报
回复
最简单,jpane 上面放 jlabel 显示图片,文本框方上面, ok
  • 打赏
  • 举报
回复
你把Jpanel的Background设为Null应该就变成透明了,试一下!
oh_Maxy 2014-08-06
  • 打赏
  • 举报
回复
http://blog.163.com/lh_bbs/blog/static/143070803201108213037/ http://bbs.csdn.net/topics/370132863 这两个地方都有讨论。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧