当前位置: 技术问答>java相关
我想做象JBuilder6一样的程序开启画面,让程序初始化过程运行在后台,请大家给我出出注意!
来源: 互联网 发布时间:2015-05-21
本文导语: 如题。最好能给我一段原码,分一定送上。 | import java.awt.*; import javax.swing.*; public class SplashWindow extends JWindow { private SplashPanel SPLASHPANEL=new SplashPanel(); private int splash_x; private int splas...
如题。最好能给我一段原码,分一定送上。
|
import java.awt.*;
import javax.swing.*;
public class SplashWindow extends JWindow
{
private SplashPanel SPLASHPANEL=new SplashPanel();
private int splash_x;
private int splash_y;
private int splash_w;
private int splash_h;
private Container con;
public SplashWindow()
{
con=getContentPane();
con.setLayout(new BorderLayout());
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension splashsize=SPLASHPANEL.getPreferredSize();
splash_w=splashsize.width;
splash_h=splashsize.height;
splash_x=(int)((screensize.width-splash_w)/2);
splash_y=(int)((screensize.height-splash_h)/2);
con.add(SPLASHPANEL,BorderLayout.CENTER);
setBounds(splash_x,splash_y,splash_w,splash_h);
show();
}
public void hideMe()
{
dispose();
}
/*
public static void main(String[] args)
{
new SplashWindow();
}
*/
}
________________________________________________________
import java.awt.*;
import javax.swing.*;
public class SplashPanel extends JPanel
{
private ImageIcon img=null;
public SplashPanel()
{
img=new ImageIcon(getClass().getResource("image\splash.gif"));
setOpaque(false);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
img.paintIcon(this,g,0,0);
}
public Dimension getPreferredSize()
{
int splash_w=img.getIconWidth();
int splash_h=img.getIconHeight();
Dimension splash_dimension=new Dimension(splash_w,splash_h);
return splash_dimension;
}
public ImageIcon getImg()
{
return img;
}
public void setImg(ImageIcon pic)
{
img=pic;
}
}
import javax.swing.*;
public class SplashWindow extends JWindow
{
private SplashPanel SPLASHPANEL=new SplashPanel();
private int splash_x;
private int splash_y;
private int splash_w;
private int splash_h;
private Container con;
public SplashWindow()
{
con=getContentPane();
con.setLayout(new BorderLayout());
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension splashsize=SPLASHPANEL.getPreferredSize();
splash_w=splashsize.width;
splash_h=splashsize.height;
splash_x=(int)((screensize.width-splash_w)/2);
splash_y=(int)((screensize.height-splash_h)/2);
con.add(SPLASHPANEL,BorderLayout.CENTER);
setBounds(splash_x,splash_y,splash_w,splash_h);
show();
}
public void hideMe()
{
dispose();
}
/*
public static void main(String[] args)
{
new SplashWindow();
}
*/
}
________________________________________________________
import java.awt.*;
import javax.swing.*;
public class SplashPanel extends JPanel
{
private ImageIcon img=null;
public SplashPanel()
{
img=new ImageIcon(getClass().getResource("image\splash.gif"));
setOpaque(false);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
img.paintIcon(this,g,0,0);
}
public Dimension getPreferredSize()
{
int splash_w=img.getIconWidth();
int splash_h=img.getIconHeight();
Dimension splash_dimension=new Dimension(splash_w,splash_h);
return splash_dimension;
}
public ImageIcon getImg()
{
return img;
}
public void setImg(ImageIcon pic)
{
img=pic;
}
}
|
用 jWindow 就行啦
import javax.swing.*;
import javax.swing.event.*;
public class YourClass extends JFrame{
JWindow jWindow1 = new JWindow();
public static void main(String args[]){
YourClass f = new YourClass();
f.setTitle("....");
f.setLocation(...,...);
f.setSize(...,...);
f.setResizable(true);
f.setVisible(true);
}
//构造函数
public YourClass() {
jLabel1.setIcon(new ImageIcon("YourIco.jpg"));
jWindow1.getContentPane().add(jLabel1, BorderLayout.NORTH);
centerWin(); //置中 JWindow window
jWindow1.show();
jWindow1.addMouseListener(new MouseAdapter(){ //鼠标 点击后消失
public void mousePressed(MouseEvent e){
jWindow1.dispose();
}
});
/*
其他构造函数
一定要写在中间
......
*/
jWindow1.dispose(); //构造完后 JWindow window 消失
}
/**
* 置中 JWindow window
*/
private void centerWin(){
Toolkit toolkit=Toolkit.getDefaultToolkit();
Dimension screenSize=toolkit.getScreenSize();
Dimension labelSize=jLabel1.getPreferredSize();
int labelWidth=labelSize.width;
int labelHeight=labelSize.height;
jWindow1.setLocation(screenSize.width/2-(labelWidth/2),screenSize.height/2-(labelHeight/2));
jWindow1.pack();
}
}
import javax.swing.*;
import javax.swing.event.*;
public class YourClass extends JFrame{
JWindow jWindow1 = new JWindow();
public static void main(String args[]){
YourClass f = new YourClass();
f.setTitle("....");
f.setLocation(...,...);
f.setSize(...,...);
f.setResizable(true);
f.setVisible(true);
}
//构造函数
public YourClass() {
jLabel1.setIcon(new ImageIcon("YourIco.jpg"));
jWindow1.getContentPane().add(jLabel1, BorderLayout.NORTH);
centerWin(); //置中 JWindow window
jWindow1.show();
jWindow1.addMouseListener(new MouseAdapter(){ //鼠标 点击后消失
public void mousePressed(MouseEvent e){
jWindow1.dispose();
}
});
/*
其他构造函数
一定要写在中间
......
*/
jWindow1.dispose(); //构造完后 JWindow window 消失
}
/**
* 置中 JWindow window
*/
private void centerWin(){
Toolkit toolkit=Toolkit.getDefaultToolkit();
Dimension screenSize=toolkit.getScreenSize();
Dimension labelSize=jLabel1.getPreferredSize();
int labelWidth=labelSize.width;
int labelHeight=labelSize.height;
jWindow1.setLocation(screenSize.width/2-(labelWidth/2),screenSize.height/2-(labelHeight/2));
jWindow1.pack();
}
}