为什么我用java写的一个界面一开始不能显示出组件,到我把鼠标一定到相应位置才显示?

package com.view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.imageio.ImageIO;
import javax.swing.*;

import java.io.*;
public class Login extends JWindow implements ActionListener{
//定义组件
JLabel jl_username,jl_password;
JTextField jt_username,jt_password;
JRadioButton department,teacher,student,vistor;
JButton login,reset,exit;

public static void main(String[] args) {

Login login=new Login();
}
public Login()
{

//初始化组件
BackImage bi=new BackImage();
jl_username=new JLabel("用户名:",new ImageIcon("image/username.jpg"),SwingConstants.CENTER);
jl_password=new JLabel("密 码:",new ImageIcon("image/password.jpg"),SwingConstants.CENTER);
jt_username=new JTextField();
jt_password=new JTextField();
department=new JRadioButton("部门");
teacher=new JRadioButton("老师");
student=new JRadioButton("学生");
vistor=new JRadioButton("游客");
login=new JButton("登录");
login.addActionListener(this);
reset=new JButton("重置");
reset.addActionListener(this);
exit=new JButton("退出");
exit.addActionListener(this);

//布局
//设置空布局
this.setLayout(null);
bi.setBounds(0,0,441,313);
jl_username.setBounds(107,107,63,17);
jl_password.setBounds(114,143,63,17);
jt_username.setBounds(183,107,145,20);
jt_password.setBounds(183,142,145,20);
department.setBounds(117,186,53,15);
teacher.setBounds(117,186,53,15);
//student.setBounds(170,186,53,15);
//vistor.setBounds(117,186,53,15);
login.setBounds(117,212,63,25);
reset.setBounds(197,212,63,25);
//添加组件
this.add(bi);

this.add(jl_username);
this.add(jl_password);
this.add(jt_username);
this.add(jt_password);
this.add(department);
this.add(teacher);
this.add(student);
this.add(vistor);
this.add(login);
this.add(reset);
//窗口属性

this.setVisible(true);
this.setSize(441,313);
this.validate();
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口居中
int width=getToolkit().getDefaultToolkit().getScreenSize().width;
int height=getToolkit().getDefaultToolkit().getScreenSize().height;
this.setLocation(width/2-200,height/2-200);
}

class BackImage extends JPanel
{
Image im;
public BackImage()
{
try {
im = ImageIO.read(new File("image/login.jpg"));
} catch (Exception e) {
e.printStackTrace();
}
}

public void paint(Graphics g)
{
g.drawImage(im,0,0,441,313,this);
}
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//登录
if(arg0.getSource()==login)
{

}
//重置
if(arg0.getSource()==reset)
{

}
//退出
if(arg0.getSource()==exit)
{
this.dispose();
}
}
}

你那个BackImage是个容器,占用的位置很大的,他占据了整个窗口,而你重写了paint方法又重新画了个图,这个图把其它组件清除了,你是不是不画图就没事,当然你如果调用super.paint也会清除其它组件。因为这个容器和其它组件重叠了。建议把组件放到BackImage里,在paint方法里画完图后,调用super.paintChildren(g)画子组件。最好画图前还是调下 super.paint(g);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-11-25
资源没有释放,控件重叠,
第2个回答  2012-11-25
有时swing 组件太笨重占用内存很多, 程序会出现 这种情况,可能是jvm 运存太小,不必在意。
第3个回答  2012-11-24
试了,不会呀