if(underline) { logFontClass.getField("lfUnderline").set(logFont, new Byte((byte)1)); } } } catch(Throwable e) { System.err.println("Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e); } }
font = new Font(Display.getCurrent(), fontData); m_FontMap.put(fontName, font); }
楼主【still_rain】截止到2008-06-27 20:53:45的历史汇总数据(不包括此帖):
发帖数:8 发帖分:200
结贴数:8 结贴分:200
未结数:0 未结分:0
结贴率:100.00% 结分率:100.00%
敬礼!
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;import com.swtdesigner.SWTResourceManager;public class MM
{
private Shell shell;
private MMD mmd;
public static void main(String[] args)
{
try
{
MM window = new MM();
window.open();
}
catch(Exception e)
{
e.printStackTrace();
}
} public void open()
{
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
} display.dispose();
} protected void createContents()
{
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application"); final Button btnShow = new Button(shell, SWT.NONE);
btnShow.setText("Show");
btnShow.setBounds(39, 60, 249, 53); btnShow.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(final SelectionEvent e)
{
mmd = MMD.getInstance(shell);
if(mmd.shell == null || mmd.shell.isDisposed())
{
mmd.open();
}
}
});
shell.addControlListener(new ControlAdapter()
{
public void controlMoved(final ControlEvent e)
{
if(mmd != null && !mmd.shell.isDisposed())
{
mmd.location();
}
}
}); final Label label = new Label(shell, SWT.NONE);
label.setFont(SWTResourceManager.getFont("Courier New", 36, SWT.BOLD));
label.setText("我看看");
label.setBounds(299, 275, 193, 67);
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import com.swtdesigner.SWTResourceManager;public class MMD extends Dialog
{
public Shell shell; private static MMD instance; private MMD(Shell parent)
{
super(parent, SWT.NONE);
} public void open()
{
createContents();
shell.layout();
shell.open();
showSlowly();
Display display = getParent().getDisplay(); while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
} public static MMD getInstance(Shell parent)
{
if(instance == null)
{
instance = new MMD(parent);
} return instance;
} private void createContents()
{
shell = new Shell(getParent(), SWT.NO_TRIM);
shell.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
//设置透明度,0到255
shell.setAlpha(200);
shell.setSize(170, 0);
location();
shell.setText("SWT Dialog"); final Button btnX = new Button(shell, SWT.NONE);
btnX.setText("X");
btnX.setBounds(151, 0, 19, 15); btnX.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(final SelectionEvent e)
{
shell.dispose();
instance = null;
}
}); final Label label = new Label(shell, SWT.WRAP);
label.setForeground(SWTResourceManager.getColor(255, 255, 255));
label.setFont(SWTResourceManager.getFont("Courier New", 12, SWT.BOLD));
label.setText("想放什么就放什么喽");
label.setBounds(10, 21, 150, 69);
label.setBackground(shell.getBackground());
} private void showSlowly()
{
Thread thread = new Thread()
{
public void run()
{
Display.getDefault().syncExec(new Runnable()
{
public void run()
{
while(shell.getSize().y < 100)
{
shell.setSize(shell.getSize().x, shell.getSize().y + 5);
location(); try
{
sleep(50);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
});
}
}; thread.setDaemon(true);
thread.start();
} public void location()
{
shell.setLocation(getParent().getLocation().x + getParent().getSize().x - shell.getSize().x - 3, getParent().getLocation().y +
getParent().getSize().y - shell.getSize().y - 3);
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;import com.swtdesigner.SWTResourceManager;public class MM
{
private Shell shell;
private MMD mmd;
private Button btnShow;
private Label label;
public static void main(String[] args)
{
try
{
MM window = new MM();
window.open();
}
catch(Exception e)
{
e.printStackTrace();
}
} public void open()
{
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
} display.dispose();
} protected void createContents()
{
shell = new Shell();
shell.setSize(500, 375);
shell.setText("Test"); btnShow = new Button(shell, SWT.NONE);
btnShow.setText("Show");
btnShow.setBounds(39, 60, 249, 53);
label = new Label(shell, SWT.NONE);
label.setFont(SWTResourceManager.getFont("Courier New", 36, SWT.BOLD));
label.setText("我看看");
label.setBounds(299, 275, 193, 67); btnShow.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(final SelectionEvent e)
{
mmd = MMD.getInstance(shell);
mmd.open();
}
});
shell.addControlListener(new ControlAdapter()
{
public void controlMoved(final ControlEvent e)
{
if(mmd != null)
{
mmd.show();
}
}
});
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;import com.swtdesigner.SWTResourceManager;public class MMD extends Dialog
{
private Shell shell; private Button btnX; private Label label; private static MMD instance; private int tempHeight = 0; private MMD(Shell parent)
{
super(parent, SWT.NONE);
} public void open()
{
if(shell == null)
{
createContents();
shell.layout();
shell.open();
showSlowly();
Display display = getParent().getDisplay(); while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
}
} public static MMD getInstance(Shell parent)
{
if(instance == null)
{
instance = new MMD(parent);
} return instance;
} private void createContents()
{
shell = new Shell(getParent(), SWT.NO_TRIM);
shell.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
// 设置透明度,0到255
shell.setAlpha(200);
shell.setSize(170, 0);
show();
shell.setText("SWT Dialog"); btnX = new Button(shell, SWT.NONE);
btnX.setText("X");
btnX.setBounds(151, 0, 19, 15); label = new Label(shell, SWT.WRAP);
label.setForeground(SWTResourceManager.getColor(255, 255, 255));
label.setFont(SWTResourceManager.getFont("Courier New", 12, SWT.BOLD));
label.setText("想放什么就放什么喽");
label.setBounds(10, 21, 150, 69);
label.setBackground(shell.getBackground()); btnX.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(final SelectionEvent e)
{
shell.dispose();
instance = null;
}
});
} private void showSlowly()
{
Thread thread = new Thread()
{
public void run()
{
while(tempHeight < 100)
{
Display.getDefault().syncExec(new Runnable()
{
public void run()
{
shell.setSize(shell.getSize().x, (tempHeight = tempHeight + 2));
show();
}
});
} try
{
sleep(60);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}; thread.setDaemon(true);
thread.start();
} public void show()
{
if(shell != null && !shell.isDisposed())
{
shell.setLocation(getParent().getLocation().x + getParent().getSize().x - shell.getSize().x - 4,
getParent().getLocation().y + getParent().getSize().y - shell.getSize().y - 4);
}
}
}
package com.swtdesigner;import java.util.HashMap;import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;public class SWTResourceManager
{
private static HashMap<RGB, Color> m_ColorMap = new HashMap<RGB, Color>(); private static HashMap<String, Font> m_FontMap = new HashMap<String, Font>(); public static Color getColor(int r, int g, int b)
{
return getColor(new RGB(r, g, b));
} public static Color getColor(RGB rgb)
{
Color color = m_ColorMap.get(rgb); if(color == null)
{
Display display = Display.getCurrent();
color = new Color(display, rgb);
m_ColorMap.put(rgb, color);
} return color;
} public static Font getFont(String name, int height, int style)
{
return getFont(name, height, style, false, false);
} public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline)
{
String fontName = name + '|' + size + '|' + style + '|' + strikeout + '|' + underline;
Font font = m_FontMap.get(fontName);
if(font == null)
{
FontData fontData = new FontData(name, size, style);
if(strikeout || underline)
{
try
{
Class<?> logFontClass = Class.forName("org.eclipse.swt.internal.win32.LOGFONT");
Object logFont = FontData.class.getField("data").get(fontData);
if(logFont != null && logFontClass != null)
{
if(strikeout)
{
logFontClass.getField("lfStrikeOut").set(logFont, new Byte((byte)1));
}
if(underline)
{
logFontClass.getField("lfUnderline").set(logFont, new Byte((byte)1));
}
}
}
catch(Throwable e)
{
System.err.println("Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e);
}
}
font = new Font(Display.getCurrent(), fontData);
m_FontMap.put(fontName, font);
}
return font;
}
}
相信大家都使用过MSN,QQ这样的即时聊天类软件,对于它们的好友上线提示功能并不陌生吧?从屏幕右下角弹出一个小界面,慢慢上升,最后消失。我们能不能在自已的程序中也做出相同的功能呢?能!笔者现用JAVA和eclipse的SWT用户界面组件实现这个功能。 什么是SWT呢? SWT原来是eclipse项目组为开发eclipse IDE所编写的图形界面API,运行时,其先判断本机是否有相同的界面元素,如果有则直接调用显示,如没有才进行模拟显示。其运行机制使速度比AWT,SWING快很多。 了解更多请看:http://www.eclipse.org/swt 编写思路 先取得用户屏幕大小,用屏幕高度减去popup界面的高度计算出popup界面在屏幕显示的最高位置(当界面移动到此位置时就停止移动)。
Rectangle area = Display.getDefault().getClientArea();
int upPosition = area.height - 100; 用屏幕高度加上popup界面的高度就计算出popup界面的初始位置(初始化时不可见,然后慢慢上移到upPosition点后停止移动,再显示若干秒后消失)。
int downPosition = area.height + 100; 移动位置我们用线程实现,当初始化界面后,调用start()方法运行此线程,在线程中循环判断downPosition的大小是否小于upPosition,如果小于的话说明还未到停止的时候,设置popup界面的边框为downPosition,并暂停10毫秒,如果downPosition大于upPosition的,说明popup界面已移动到了最高位置。调用sleep()暂停5秒钟后关闭界面并退出程序。就这么简单,ok, Let's go! 下面给出整个程序代码: 描述: (Test为主界面,点击上面的button后,调用Popup在右下角显示像MSN和QQ一样的popup界面。) 图一为源代码中的实现,图二为修改过后的界面(和QQ的有点像吧。)
图一 图二 源代码:
// Test.java
//主界面,其中只有一个button,当点击时调用Popup在右下角显示像MSN和QQ一样的popup界面。import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;public class Test { public static void main(String[] args) { final Display display = new Display();
Shell shell = new Shell();
shell.setText("aaa");
shell.setSize(250, 150); final Button button = new Button(shell, SWT.NONE);
button.setBounds(50, 20, 100, 25);
button.setText("button");
//监听button的事件,当用户点击时调用Popup类显示popup界面。
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
//实例化popup类,构造函数为popup界面中出现的提示信息。
Popup popup = new Popup("您的好友xxx上线了。");
popup.start();
}
}); shell.open(); while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}// Popup.java
//实现像MSN,QQ一样的好友上线通知popupimport org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;public class Popup extends Thread { Shell shell; protected int moveStep = 2; //每次移动的pixel
protected int upPosition; //能移动到的最上面坐标
protected int downPosition; //当前popup的边框坐标
protected int leftPosition; //popup左边边框坐标 public Popup(final String message) { shell = new Shell(SWT.ON_TOP);
Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
text.setBounds(10, 20, 180, 80);
text.setBackground(shell.getBackground());
text.setText(message); //取屏莫大小
Rectangle area = Display.getDefault().getClientArea(); upPosition = area.height - 100;//计算出popup界面在屏幕显示的最高位置
downPosition = area.height + 100;//计算出popup界面的初始位置
leftPosition = area.width - 180; shell.setSize(180, 100); //初始化popup位置
shell.setLocation(leftPosition, downPosition); shell.open();}public void run() { Display display = shell.getDisplay();
while (true) {
try {
Thread.sleep(10); //判断当前位置是否小于能出现的最高位置,小于的话就说明还可以向上移动。
if ((downPosition - moveStep) > upPosition) {
display.asyncExec(new Runnable() {
public void run() {
shell.setLocation(leftPosition, downPosition- moveStep);
downPosition -= moveStep;
}
});
//此时已经移动到了最高位置,显示5秒钟后,关闭窗口并退出。
} else {
Thread.sleep(5000);
display.asyncExec(new Runnable() {
public void run() {
shell.dispose();
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}