Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 [转载]Java程序实现Logo画面的编程
scottding

十三部落酋长

CJSDN高级会员


发贴: 1054
积分: 101
于 2003-01-16 15:44 user profilesend a private message to usersend email to scottdingsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有些程序需要初始化的时间比较长。程序只有在初始化之后才能够给出界面,所以在程序初始化的时候,一般的软件都会给出一个画面,告诉用户程序正在启动当中。尤其是Java的界面启动一般需要耗费时间,如果没有Logo画面会让用户搞不清楚程序是否正在运行。实际上,很多商用程序,比如JBuilder都会给出一个很友好的Logo画面。

原理

在程序开始运行的时候,Logo画面其实是程序的一个线程,而且Logo是一个图,所以显示Logo画面的窗口必须像一个“白板”一样,也就是说,这个窗口里面只容纳了这个图片,其它的最大、最小化按钮等都没有。程序的界面显示后,就结束这个Logo画面的线程。

以上的这些Logo画面的基本要求,用Java语言实现起来都非常的方便。下面将介绍一个实现Logo画面的类。

实现

首先,这个类必须继承JWindow,因为JWindow能够实现没有边框的窗口的效果。其次,这个类必须实现Java语言的Runnable接口,因为这个类本身是一个线程。同时这个类还需要提供传入所显示图片的构造函数,还应该包含一个让线程退出的函数。

该类的代码如下:

import javax.swing.*;
import java.lang.Runnable;
import java.awt.*;
public class Logo extends JWindow implements Runnable {
String filename; //Logo图像文件的文件名
public Logo(String name) {
filename = name;
}
public void run()
{
ImageIcon ig = new ImageIcon(filename);
JButton btn = new JButton(ig); //将图片给JButton显示
getContentPane().add(btn); //将显示图片的btn加到JPanel里
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize(); //获得屏幕的大小
setLocation(screenSize.width/4,
screenSize.height/4);//将Logo窗口显示在屏幕宽的1/4,高的1/4处
setSize(ig.getIconWidth(), ig.getIconHeight()); //将Logo窗口大小设成图像的大小
toFront(); //将Logo窗口显示为最前面的窗口
setVisible(true); //显示该窗口
}
public void setNotVisible()
{
setVisible(false); //不显示该窗口
}
}


使用

这个类的使用方法是很方便的。对于一般的界面程序,都是在程序开始的时候初始化界面。现在假设程序的主窗体类名为mainFrame,那么在没有加入到Logo画面以前的程序为:

public class programMain {
public static void main(String args[])
{
mainFrame frame1 = new mainFrame();
}
}


现在需要加入Logo画面,那么只需要加入几行代码,将原来的程序改成如下代码:

public class programMain {
public static void main(String args[])
{
Logo lg =new Logo("Logo.gif");
lg.run();
mainFrame frame1 = new mainFrame();
lg.setNotVisible();
}
}


在程序的界面初始化完成后,该Logo画面消失,程序的主界面出现。

扩展

基于上面的程序,我们还可以扩展出很多种Logo画面的方式,比如用一个进度条来替换这个图像,只需要显示出进度条,然后在界面的初始化程序里面控制进度条的进度就可以了。用这个思想,Logo画面的做法是多种多样的。

http://developer.ccidnet.com/pub/disp/Article?columnID=322&articleID=36237&pageNO=1



作者 Re:[转载]Java程序实现Logo画面的编程 [Re:scottding]
rox





发贴: 61
积分: 30
于 2003-01-16 16:39 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢,我原先的公司有个java程序也是用了logo。
我还一直以为是它的外包VC程序做的。
呵呵!



作者 Re:[转载]Java程序实现Logo画面的编程 [Re:scottding]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-09-30 12:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
This article is totally wrong!

http://developer.ccidnet.com/pub/disp/Article?columnID=322&articleID=36237&pageNO=1



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:[转载]Java程序实现Logo画面的编程 [Re:scottding]
reddream



发贴: 0
积分: 0
于 2003-11-27 14:55 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
The SwingSet2 from demo of j2sdk also shows how to create a logo


作者 Re:[转载]Java程序实现Logo画面的编程 [Re:scottding]
reddream



发贴: 0
积分: 0
于 2003-11-27 14:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
scottding wrote:

...
lg.run();
...


should be
lg.start();



作者 Re:[转载]Java程序实现Logo画面的编程 [Re:scottding]
Ispy



发贴: 0
积分: 0
于 2003-12-09 21:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
好文。我已经按照他的方法实现了。:)




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923