[code="java"]
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class UI {
public static final String VERSION = "1.0";
private static final Display display = new Display();
private static final Shell shell = new Shell(display);
public static void main(String[] args) {
shell.setText("综合工具箱v"+VERSION);
shell.setSize(new Point(400,320));
shell.setLayout(new GridLayout(1, true));
initMail163();
createConsole();
center(shell);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
private static void initMail163() {
Composite content = new Composite(shell,SWT.BORDER);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
content.setLayoutData(gd);
content.setLayout(new RowLayout());
final Button mailOp = new Button(content, SWT.BORDER);
mailOp.setText("自动领积分");
mailOp.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
mailOp.setEnabled(false);
mockOutputToConsole();
mailOp.setEnabled(true);
}
private void mockOutputToConsole() {
while(true){
System.out.println("test....");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
private static void center(Composite comp){
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;
comp.setLocation(x, y);
}
private static void createConsole(){
Composite control = new Composite(shell,SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
control.setLayoutData(gd);
control.setToolTipText("控制台");
control.setSize(new Point(600,400));
control.setLayout(new FillLayout());
Text outputPanel = new Text(control, SWT.MULTI | SWT.V_SCROLL | SWT.WRAP );
MyPrintStream mps = new MyPrintStream(System.out, outputPanel);
System.setOut(mps);
System.setErr(mps);
}
}
import java.io.OutputStream;
import java.io.PrintStream;
import org.eclipse.swt.widgets.Text;
import java.io.OutputStream;
import java.io.PrintStream;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
public class MyPrintStream extends PrintStream{
private Text text;
public MyPrintStream(OutputStream out, Text text) {
super(out);
this.text = text;
}
/**
* 在这里重截,所有的打印方法都要调用的方法
*/
public void write(final byte[] buf, final int off, final int len) {
if(text != null && !text.isDisposed()){
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
text.append(new String(buf, off, len));
}
});
}else{
super.write(buf, off, len);
}
}
}
[/code]