为什么我用java写的一个界面一开始不能显示出组件,到我把鼠标一定到相应位置才显示?
packagecom.view;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.even...
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();
}
}
} 展开
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();
}
}
} 展开
4个回答
展开全部
资源没有释放,控件重叠,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有时swing 组件太笨重占用内存很多, 程序会出现 这种情况,可能是jvm 运存太小,不必在意。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询