给你个<java与模式>中的例子看看吧: 1.Client.java import java.awt.*; import javax.swing.*;public class Client extends JFrame { private static final int IMAGE_WIDTH = 270; private static final int IMAGE_HEIGHT = 380; private Icon imageIconProxy = null; public static void main(String[] args) { Client app = new Client(); app.show(); } public Client() { super("Virtual Proxy Client"); imageIconProxy = new ImageIconProxy("c:/hongyan.jpg", IMAGE_WIDTH, IMAGE_HEIGHT); setBounds(100, 100, IMAGE_WIDTH + 10, IMAGE_HEIGHT + 30); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g) { super.paint(g); Insets insets = getInsets(); imageIconProxy.paintIcon(this, g, insets.left, insets.top); } }2.ImageIconProxy.java import java.awt.*; import javax.swing.*;public class ImageIconProxy implements Icon { private ImageIcon realIcon = null; private String imageName; private int width; private int height; private boolean isIconCreated = false; public ImageIconProxy(String imageName, int width, int height) { this.imageName = imageName; this.width = width; this.height = height; } public int getIconHeight() { return realIcon.getIconHeight(); } public int getIconWidth() { return realIcon.getIconWidth(); } // The proxy's paint() method is overloaded to draw a border // and a message "Loading author's photo.." while the image // loads. After the image has loaded, it is drawn. Notice // that the proxy does not load the image until it is // actually needed. public void paintIcon(final Component c, Graphics g, int x, int y) { if (isIconCreated) { realIcon.paintIcon(c, g, x, y); g.drawString("Java and Patterns by Jeff Yan, Ph.D", x + 20, y + 370); } else { g.drawRect(x, y, width - 1, height - 1); g.drawString("Loading author's photo...", x + 20, y + 20); // The image is being loaded on another thread. synchronized(this) { SwingUtilities.invokeLater( new Runnable() { public void run() { try { // Slow down the image-loading process. Thread.currentThread().sleep(2000); // ImageIcon constructor creates the image. realIcon = new ImageIcon(imageName); isIconCreated = true; } catch (InterruptedException ex) { ex.printStackTrace(); } // Repaint the icon's component after the // icon has been created. c.repaint(); } }); } } } }
1.Client.java
import java.awt.*;
import javax.swing.*;public class Client extends JFrame
{
private static final int IMAGE_WIDTH = 270;
private static final int IMAGE_HEIGHT = 380; private Icon imageIconProxy = null; public static void main(String[] args)
{
Client app = new Client();
app.show();
} public Client()
{
super("Virtual Proxy Client");
imageIconProxy = new ImageIconProxy("c:/hongyan.jpg", IMAGE_WIDTH, IMAGE_HEIGHT); setBounds(100, 100, IMAGE_WIDTH + 10, IMAGE_HEIGHT + 30);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void paint(Graphics g)
{
super.paint(g);
Insets insets = getInsets();
imageIconProxy.paintIcon(this, g, insets.left, insets.top);
}
}2.ImageIconProxy.java
import java.awt.*;
import javax.swing.*;public class ImageIconProxy implements Icon
{
private ImageIcon realIcon = null;
private String imageName;
private int width;
private int height;
private boolean isIconCreated = false; public ImageIconProxy(String imageName, int width, int height)
{
this.imageName = imageName;
this.width = width;
this.height = height;
} public int getIconHeight()
{
return realIcon.getIconHeight();
} public int getIconWidth()
{
return realIcon.getIconWidth();
} // The proxy's paint() method is overloaded to draw a border
// and a message "Loading author's photo.." while the image
// loads. After the image has loaded, it is drawn. Notice
// that the proxy does not load the image until it is
// actually needed.
public void paintIcon(final Component c, Graphics g, int x, int y)
{
if (isIconCreated)
{
realIcon.paintIcon(c, g, x, y);
g.drawString("Java and Patterns by Jeff Yan, Ph.D", x + 20, y + 370);
}
else
{
g.drawRect(x, y, width - 1, height - 1);
g.drawString("Loading author's photo...", x + 20, y + 20); // The image is being loaded on another thread.
synchronized(this)
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
try
{
// Slow down the image-loading process.
Thread.currentThread().sleep(2000); // ImageIcon constructor creates the image.
realIcon = new ImageIcon(imageName);
isIconCreated = true;
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
// Repaint the icon's component after the
// icon has been created.
c.repaint();
}
});
}
}
}
}
联完后再Fire一个关掉窗口的Event
随便写了个Demol:
import java.sql.*;
import sun.jdbc.odbc.JdbcOdbcDriver;
public class LinkDataBaseThread extends Thread{
boolean notLinkErr = true;
String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL="";
String userName = "";
String password = "";
int outTime = 100000; Connection conn = null;
public LinkDataBaseThread(String url,String user,String passoword ){
dbURL = url;
userName = user;
this.password = password;
} public void run(){
notLinkErr = linkDataBase(); } private boolean linkDataBase(){
try {
Class.forName(dbDriver);
DriverManager.setLoginTimeout(outTime);
try {
conn = DriverManager.getConnection(dbURL,userName,password);
try {
Thread.sleep(5000); //模拟远程服务器的连接,进行延时.
}
catch (InterruptedException ex) {
}
}
catch (SQLException ex) {
System.out.println("不能连接数据库!");
ex.printStackTrace();
return false;
}
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到jdbc驱动");
ex.printStackTrace();
return false;
}
return true;
} public Connection getConnection(){
return conn;
} public boolean isNotLinkErr(){
return notLinkErr ;
} public void setOutTime(int outTime){
this.outTime = outTime;
} public static void main(String[] args){
//test
javax.swing.JOptionPane.showMessageDialog(null,"正在连接,请稍后","正在连接,请稍后",javax.swing.JOptionPane.WARNING_MESSAGE);//改为自己的提示窗口,登录成功后再隐藏
LinkDataBaseThread lbt = new LinkDataBaseThread("jdbc:odbc:test","sa","");
Connection con= null;
lbt.start();
while(lbt.isNotLinkErr()){
if(null==(con=lbt.getConnection()) ){
System.out.print(".");
try {
Thread.sleep(500);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
}else{
System.out.println("");
System.out.println("连接成功!con:"+con);
break;
}
}
}
}