当前位置: 代码迷 >> 综合 >> swing编程实现闪屏登录效果
  详细解决方案

swing编程实现闪屏登录效果

热度:4   发布时间:2024-01-22 03:14:38.0
1、先写一个闪屏类
package com.mhl.Tools;
import java.awt.*;
import javax.swing.*;
//开发一个闪屏类

public class paint extends JPanel implements Runnable

{

Thread t;

int x=10;

int i=0,j=40,u=10;
//
String gg[]={"系","统","正","在","加","载","请","稍","后"};

int k=0,tt=0;

String shi[]={"满","汉","楼","融","满","汉","精","华","做","天","下","美","味",

"招","八","方","食","客","结","四","海","良","朋","|","满","汉","楼","工","作","室"};

Font f=new Font("隶书",Font.PLAIN,18);



boolean ifok=true;

int width=180;

int height=0;

int dian=0;



public paint()

{

t=new Thread(this);

t.start();

}



@Override

public void run() {

while(true)

{

if(x<=380) repaint();

try {

Thread.sleep(100);

i++;

j=j-6;

u=u+10;



if(tt==3) width=width-20;

if(i==4)

{

tt++;

if(ifok&&x>120+k*20) k++;

if(k>=gg.length-1) ifok=false;

x=x+10;

i=0;

j=40;

u=10;

dian++;

if(dian>3) dian=0;

}

} catch (InterruptedException e) {

System.out.println("线程中断");

e.printStackTrace();

}

}

}



public void paintComponent(Graphics g)

{

Image image;

image=Toolkit.getDefaultToolkit().getImage("Images/index/index.gif");

g.drawImage(image, 0, 0, this.getWidth(), 200, this);



int r=(int)(Math.random()*255);

int b=(int)(Math.random()*255);

int y=(int)(Math.random()*255);



g.setColor(new Color(253,250,250));

g.fillRect(x, 210, 390-x, 30);

g.setColor(new Color(253,250,250));

if(i>1) g.fillRect(x, 225-(j+20)/2, 10, j+20);

if(j>25) g.setColor(new Color(r,b,y));

else g.setColor(new Color(123,194,252));

g.fillRect(x, 225-j/2, 10, j);

g.setColor(new Color(123,194,252));

g.drawRect(10, 210, 380, 30);



if(x<120)

{

for(int l=0;l<gg.length;l++)

{

g.setColor(new Color(0,0,0));

g.drawString(gg[l],120+l*20,230);

}

for(int l=0;l<dian;l++)

{

g.setColor(new Color(0,0,0));

g.drawString("*", 300+l*10, 235);

}

g.drawString("*", 300+10*dian, 235);

}

else

{

g.setColor(new Color(23,23,230));

g.drawString(gg[k],120+k*20,230);

for(int l=k+1;l<gg.length;l++)

{

g.setColor(new Color(0,0,0));

g.drawString(gg[l],120+l*20,230);

}

if(x>300+dian*10)

g.setColor(new Color(23,23,230));

for(int l=0;l<dian;l++)

{

g.drawString("*", 300+l*10, 235);

}

g.drawString("*",300+10*dian,235);

}



//-----------逐字写诗

if(tt<3)

{

for(int rr=0;rr<=tt;rr++)

{

g.setColor(new Color(r,b,y));

g.drawString(shi[rr], 170, 60+rr*20);

}

g.drawString(shi[tt], 170, 60+tt*20);

}

if(tt>=3&&tt<7)

{

g.setColor(new Color(230,0,0));

for(int rr=0;rr<3;rr++)

g.drawString(shi[rr], 170, 60+rr*20);

g.setColor(new Color(r,b,y));

if(tt<8)

for(int rr=3;rr<=tt;rr++)

g.drawString(shi[rr], 150, rr*20-20);

if(tt>=7)

{

for(int rr=3;rr<=7;rr++)

g.drawString(shi[rr], 150, rr*20-20);

}

}

if(tt>=7&&tt<13)

{

g.setColor(new Color(230,0,0));

for(int rr=0;rr<3;rr++)

g.drawString(shi[rr], 170, 60+rr*20);

for(int rr=3;rr<=7;rr++)

g.drawString(shi[rr], 150, rr*20-20);

g.setColor(new Color(r,b,y));

if(tt<13) for(int rr=8;rr<=tt;rr++)

g.drawString(shi[rr], 120, rr*20-120);

//减120,是因为此时rr不是5,而是在7~13之间,减去前面的5个*20,再减20,即减120

if(tt>=13) for(int rr=8;rr<=13;rr++)

g.drawString(shi[rr], 120, rr*20-120);

}

if(tt>=13&&tt<18)

{

g.setColor(new Color(230,0,0));

for(int rr=0;rr<3;rr++)

g.drawString(shi[rr], 170, 60+rr*20);

for(int rr=3;rr<=7;rr++)

g.drawString(shi[rr], 150, rr*20-20);

for(int rr=8;rr<13;rr++)

g.drawString(shi[rr], 120, rr*20-120);

g.setColor(new Color(r,b,y));

if(tt<18) for(int rr=13;rr<=tt;rr++)

g.drawString(shi[rr], 90, rr*20-220);

if(tt>=18) for(int rr=13;rr<=18;rr++)

g.drawString(shi[rr], 90, rr*20-220);

}

if(tt>=18&&tt<23)

{

g.setColor(new Color(230,0,0));

for(int rr=0;rr<3;rr++)

g.drawString(shi[rr], 170, 60+rr*20);

for(int rr=3;rr<=7;rr++)

g.drawString(shi[rr], 150, rr*20-20);

for(int rr=8;rr<13;rr++)

g.drawString(shi[rr], 120, rr*20-120);

for(int rr=13;rr<18;rr++)

g.drawString(shi[rr], 90, rr*20-220);

g.setColor(new Color(r,b,y));

if(tt<23) for(int rr=18;rr<=tt;rr++)

g.drawString(shi[rr], 60, rr*20-320);

if(tt>=23) for(int rr=18;rr<=23;rr++)

g.drawString(shi[rr], 60, rr*20-320);

}

if(tt>=23&&tt<30)

{

g.setColor(new Color(230,0,0));

for(int rr=0;rr<3;rr++)

g.drawString(shi[rr], 170, 60+rr*20);

for(int rr=3;rr<=7;rr++)

g.drawString(shi[rr], 150, rr*20-20);

for(int rr=8;rr<13;rr++)

g.drawString(shi[rr], 120, rr*20-120);

for(int rr=13;rr<18;rr++)

g.drawString(shi[rr], 90, rr*20-220);

for(int rr=18;rr<23;rr++)

g.drawString(shi[rr], 60, rr*20-320);

g.setColor(new Color(r,b,y));

if(tt<30) for(int rr=23;rr<=tt;rr++)

g.drawString(shi[rr], 30, rr*20-400);

if(tt>=30) for(int rr=23;rr<=30;rr++)

g.drawString(shi[rr], 30, rr*20-400);

}

if(tt>=30)

{

g.setColor(new Color(230,0,0));

for(int rr=0;rr<3;rr++)

g.drawString(shi[rr], 170, 60+rr*20);

for(int rr=3;rr<=7;rr++)

g.drawString(shi[rr], 150, rr*20-20);

for(int rr=8;rr<13;rr++)

g.drawString(shi[rr], 120, rr*20-120);

for(int rr=13;rr<18;rr++)

g.drawString(shi[rr], 90, rr*20-220);

for(int rr=18;rr<23;rr++)

g.drawString(shi[rr], 60, rr*20-320);

for(int rr=23;rr<30;rr++)

g.drawString(shi[rr], 30, rr*20-400);

}

}



}

2、用户登陆窗口类
package com.mhl.View;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;


import com.mhl.Tools.*;
public class UserLogin extends JDialog implements ActionListener {

/**
* @param args
*/

LoginBackImage bgimage=null; //背景图片
paint p=null; //闪屏

JLabel jb1,jb2,jb3;
JTextField juserName;
JPasswordField jPwd;

JButton jconfirm,jcancel;

public static void main(String[] args) {

UserLogin login=new UserLogin();

}

public UserLogin()
{
jb1=new JLabel("请输入用户编号:");
jb1.setFont(myFont.f1);
//设置jb1在容器中的位置和大小,注意:只有在空布局中才使用此设置
jb1.setBounds(60, 190, 150, 30);
this.add(jb1);


juserName=new JTextField();
juserName.setBounds(180,190,120,30);
//设置下凹
juserName.setBorder(BorderFactory.createLoweredBevelBorder());
this.add(juserName);

jb2=new JLabel("(或员工号)");
jb2.setFont(myFont.f4);
jb2.setForeground(Color.red);
jb2.setBounds(100,210,100,30);
this.add(jb2);

jb3=new JLabel("用户密码:");
jb3.setBounds(60,240,150,30);
jb3.setFont(myFont.f1);
this.add(jb3);

jPwd=new JPasswordField();
jPwd.setBounds(180,240,120,30);
//设置边框下凹
jPwd.setBorder(BorderFactory.createLoweredBevelBorder());
this.add(jPwd);


jconfirm=new JButton("确定");
jconfirm.setBounds(105,300,75,30);
jconfirm.setFont(myFont.f1);
this.add(jconfirm);

jcancel=new JButton("取消");
jcancel.setBounds(215,300,75,30);
jcancel.setFont(myFont.f1);
jcancel.addActionListener(this);
this.add(jcancel);


//空布局
this.setLayout(null);
this.setSize(360,360);

//加背景图片
bgimage=new LoginBackImage();
bgimage.setBounds(0, 0, 360, 360);
this.add(bgimage);

//加闪屏效果
p=new paint();
this.add(p);
//取掉边框修饰效果
this.setUndecorated(true);
//窗体居中
this.setLocationRelativeTo(null);
this.setVisible(true);


}

@Override
public void actionPerformed(ActionEvent e) {

if(e.getSource()==jconfirm)
{
//todo
}else if(e.getSource()==jcancel)
{

System.exit(0);
}
}
}
/*
* 登陆截面的背景图片
*/
class LoginBackImage extends JPanel
{
Image bg=null;
LoginBackImage()
{
try {
bg=ImageIO.read(new File("Images/login.gif"));
} catch (IOException e) {

e.printStackTrace();
}

}
public void paintComponent(Graphics g)
{
g.drawImage(bg, 0, 0,360,360,this);
}
}


3、实现闪屏登陆效果
package com.mhl.View;

import java.awt.*;
import javax.swing.*;


import com.mhl.Tools.*;
public class CataLog extends JWindow implements Runnable {

/**
* @param args
*/

paint p=null;
public static void main(String[] args) {

CataLog ct=new CataLog();
Thread t=new Thread(ct);
t.start();
}


public CataLog()
{
this.setSize(400,240);

//加闪屏
p=new paint();
this.add(p);

this.setLocationRelativeTo(null);
this.setVisible(true);
}


@Override
public void run() {

while(true)
{

try {
Thread.sleep(30*500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//启动登陆窗口
new UserLogin();
this.dispose();
}
}

}


4、登陆窗口类中用到的类型
package com.mhl.Tools;
import java.awt.*;
public class myFont {

public static Font f0=new Font("宋体",Font.PLAIN, 18);
public static Font f1=new Font("宋体",Font.PLAIN, 16);
public static Font f2=new Font("宋体",Font.PLAIN,15);
public static Font f3=new Font("宋体",Font.PLAIN,14);
public static Font f4=new Font("宋体",Font.PLAIN,13);
}