package com.instantcommunication.gui;import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;public class Login extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel l_jp=null;
private JLabel l_nameLabel=null,l_passLabel=null,l_image=null;
private JTextField l_jtf=null;
private JPasswordField l_jpf=null;
private JButton l_jb=null;
private ImageIcon icon=new ImageIcon("a.jpg");
Container con=this.getLayeredPane();
Container con2=this.getContentPane();
public Login(){
setTitle("登录");
l_jp=new JPanel(null);
l_nameLabel=new JLabel("用户名");
l_passLabel=new JLabel("密 码");
l_image=new JLabel(icon);
l_jtf=new JTextField(10);
l_jpf=new JPasswordField(10);
l_jb=new JButton("登录");
this.setBounds(200, 200, 380, 280);
l_nameLabel.setBounds(70,60,50,30);
l_passLabel.setBounds(70,110,50,30);
l_jtf.setBounds(120, 60, 180, 30);
l_jpf.setBounds(120, 110, 180, 30);
l_jb.setBounds(150,170,80,30);
l_image.setBounds(0, 0, 380, 280);
con.add(l_image);
con2.add(l_nameLabel);
con2.add(l_passLabel);
con2.add(l_jtf);
con2.add(l_jpf);
con2.add(l_jb);
this.add(l_jp);
l_jb.addActionListener(this);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//repaint();
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(l_jb)){
setVisible(false);
}
}
public static void main(String[] args){
new Login();
}
}
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;public class Login extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel l_jp=null;
private JLabel l_nameLabel=null,l_passLabel=null,l_image=null;
private JTextField l_jtf=null;
private JPasswordField l_jpf=null;
private JButton l_jb=null;
private ImageIcon icon=new ImageIcon("a.jpg");
Container con=this.getLayeredPane();
Container con2=this.getContentPane();
public Login(){
setTitle("登录");
l_jp=new JPanel(null);
l_nameLabel=new JLabel("用户名");
l_passLabel=new JLabel("密 码");
l_image=new JLabel(icon);
l_jtf=new JTextField(10);
l_jpf=new JPasswordField(10);
l_jb=new JButton("登录");
this.setBounds(200, 200, 380, 280);
l_nameLabel.setBounds(70,60,50,30);
l_passLabel.setBounds(70,110,50,30);
l_jtf.setBounds(120, 60, 180, 30);
l_jpf.setBounds(120, 110, 180, 30);
l_jb.setBounds(150,170,80,30);
l_image.setBounds(0, 0, 380, 280);
con.add(l_image);
con2.add(l_nameLabel);
con2.add(l_passLabel);
con2.add(l_jtf);
con2.add(l_jpf);
con2.add(l_jb);
this.add(l_jp);
l_jb.addActionListener(this);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//repaint();
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(l_jb)){
setVisible(false);
}
}
public static void main(String[] args){
new Login();
}
}
解决方案 »
- 电脑故障 请求帮助
- java方法的参数问题
- 如何写日志文件
- 怎样用java把视频video格式(mov,avi.mpeg等)转换为flv格式.
- 刷新JFrame
- 可以在继承JPanel的类中在public void paintComponent(Graphics g)方法中添加背静图片吗?(应用程序中)
- java能直接删除非空的directory么?求教代码
- 请帮忙翻译JavaAPI文档中关于equals()的这段英文
- 请教高手!!!(关于输入输出流)
- 我刚学java,想请教一下,我在文本中创建了一个接口,一个类,还有一个扩展类,保存为.java文件时,如何给文件名?
- JNI中C操作java类属性的问题。
- java 求时间段补集
这里的两个Container实际上指的都是当前的容器。
因此下面的用法:就相当于this和con,con2是等价的。
也就是说,你在往一个容器里面添加组件,当然会出现覆盖。
还有你要用"a.jpg"做背景的话,其实是分层排放的,你没有设置l_image放置在更下的一层。看似你分层了,其实没有。可以使用:con..add(l_image,new Integer(Integer.MIN_VALUE));来将l_image放置在最底层。
还有你要设置你顶层的组件属性为透明,不然底层会被顶层覆盖。
con.add(l_image);
改成
con.add(l_image, new Integer(Integer.MIN_VALUE));
l_image.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());试试看。
至于paint和painComponent方法的好处吗?
painComponent只存在于swing中,awt中没有此方法。painComponent指的的是容器本身自己画出自己的组件;
paint方法swing和awt中都有此方法。
paint在swing中:改变容器本身的组件;
paint在awt中:改变组件样式。
说的有可能不对,要是详细的话,还是自己去查查的好!
this.setLayout(null);
Container c=this.getContentPane();
//设置为空布局
this.setLayout(null);//背景图片必须放在最下面
BacImage bi=new BacImage();
///设置图片位置
bi.setBounds(0, 0, 360, 360);
c.add(bi);//背景图片内部类
class BacImage extends JPanel
{ private static final long serialVersionUID = 1L;
Image im=null;
public BacImage()
{
im=new ImageIcon("image/login.gif").getImage();
}
//画图片函数
public void paintComponent(Graphics g)
{
g.drawImage(im, 0, 0, 360, 360, this);
}
}
public static void asBackground(JFrame frame, String path) {
frame.getContentPane().setLayout(null);
((JPanel) frame.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon(path); // 添加图片
JLabel lblPicture = new JLabel(img);
frame.getLayeredPane().add(lblPicture, new Integer(Integer.MIN_VALUE));
lblPicture.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
frame.setBounds(300, 50, img.getIconWidth() + 5,
img.getIconHeight() + 25);
}
直接调用这个方法,frame是你当前的JFrame,path是你图片的路径,绝对路径或者相对路径都可以。
效果图: