📄 alert.java
字号:
package scheduler;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.sf.pim.URLUtil;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
/**
* alert window call by jcrontab
* @author levin
*
*/
public class Alert {
//同类型提醒堆栈
private static Map<String,Shell> old=new HashMap<String,Shell>();
public static void main(final String[] args) {
final Display display = Display.getDefault();
if (display.getThread() != Thread.currentThread()) {
display.asyncExec(new Runnable() {
public void run() {
show(display, args);
}
});
} else {
show(display, args);
}
}
public static void show(final Display display, final String[] args) {
final Shell shell = new Shell(display);
//同一标题,只出现一个提醒
if(old.get(args[0]) != null)
old.get(args[0]).dispose();
old.put(args[0], shell);
shell.setMinimumSize(200, 90);
shell.setText(args[0]);
Image image = ImageDescriptor.createFromURL(
URLUtil.getResourceURL("icons/alert.gif")).createImage(
shell.getDisplay());
shell.setImage(image);
Monitor primary = display.getPrimaryMonitor();
Rectangle bounds = primary.getBounds();
Rectangle rect = shell.getBounds();
int x = bounds.x + (bounds.width - rect.width) / 2;
int y = bounds.y + (bounds.height - rect.height) / 2;
shell.setLocation(x, y);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
shell.setLayout(layout);
Label label = new Label(shell, SWT.NONE);
label.setText(args[1]);
GridData labelData = new GridData();
labelData.horizontalAlignment = GridData.FILL;
labelData.grabExcessHorizontalSpace = true;
labelData.horizontalSpan=2;
label.setLayoutData(labelData);
Button ok = new Button(shell, SWT.PUSH);
ok.setText("知道了!");
ok.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.dispose();
}
});
GridData okData = new GridData();
okData.horizontalAlignment = GridData.FILL;
okData.grabExcessHorizontalSpace = true;
if(args.length == 2)
okData.horizontalSpan =2;
ok.setLayoutData(okData);
//设置缺省按钮
shell.setDefaultButton(ok);
//三个参数,再增加一个邮件的按钮
if(args.length == 3){
Button mail=new Button(shell,SWT.PUSH);
mail.setText("查看邮件...");
mail.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.dispose();
try {
Runtime.getRuntime().exec(args[2]);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
GridData mailData = new GridData();
mailData.horizontalAlignment = GridData.FILL;
mailData.grabExcessHorizontalSpace = true;
mail.setLayoutData(mailData);
}
// shell.setDefaultButton(ok);
shell.setFocus();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
// display.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -