eclipse SWT插件写JAVA 窗口程序想实现一个按钮,一个文本框,点下按钮后文本框每毫秒自动加一在点按钮暂
展开全部
给你一组代码,放在你本地可以直接运行,我已经调试过了,祝你好运
(PS:粘贴进来没有格式了,看着比较费劲,自己放在Eclipse里面格式化一下代码)
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
public class Counter {
private Shell shell;
private Text text;
private Button button;
private boolean isRunning = false; //记录当前是否正在计数
private long interval = 100; //间隔时间,这里表示0.1秒
public static void main(String[] args) {
try {
Counter window = new Counter();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打开运行窗口
*/
public void open() {
Display display = Display.getDefault();
createContents();
registerListener();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* 创建窗口内容
*/
private void createContents() {
shell = new Shell();
shell.setSize(282, 121);
shell.setText("计数器");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
Label label = new Label(composite, SWT.NONE);
label.setText("计数器:");
text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
text.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
button = new Button(composite, SWT.NONE);
button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true, 1, 1));
button.setText("开始");
}
/**
* 为按钮注册侦听
*/
private void registerListener() {
button.addMouseListener(new MouseAdapter() {
public void mouseUp(MouseEvent e) {
isRunning = !isRunning;
if(isRunning) {
startCount();
shell.getDisplay().asyncExec(new Runnable() {
public void run() {
button.setText("暂停");
}
});
} else {
shell.getDisplay().asyncExec(new Runnable() {
public void run() {
text.setText("0");
button.setText("开始");
}
});
}
}
});
}
/**
* 启动计数线程
*/
private void startCount() {
Thread thread = new Thread(new Runnable() {
public void run() {
int num = 0;
while(isRunning) {
num ++;
final int val = num;
shell.getDisplay().asyncExec(new Runnable() { //启动UI线程,刷新数据
public void run() {
text.setText(val+"");
}
});
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
}
(PS:粘贴进来没有格式了,看着比较费劲,自己放在Eclipse里面格式化一下代码)
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
public class Counter {
private Shell shell;
private Text text;
private Button button;
private boolean isRunning = false; //记录当前是否正在计数
private long interval = 100; //间隔时间,这里表示0.1秒
public static void main(String[] args) {
try {
Counter window = new Counter();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打开运行窗口
*/
public void open() {
Display display = Display.getDefault();
createContents();
registerListener();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* 创建窗口内容
*/
private void createContents() {
shell = new Shell();
shell.setSize(282, 121);
shell.setText("计数器");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
Label label = new Label(composite, SWT.NONE);
label.setText("计数器:");
text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
text.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
button = new Button(composite, SWT.NONE);
button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true, 1, 1));
button.setText("开始");
}
/**
* 为按钮注册侦听
*/
private void registerListener() {
button.addMouseListener(new MouseAdapter() {
public void mouseUp(MouseEvent e) {
isRunning = !isRunning;
if(isRunning) {
startCount();
shell.getDisplay().asyncExec(new Runnable() {
public void run() {
button.setText("暂停");
}
});
} else {
shell.getDisplay().asyncExec(new Runnable() {
public void run() {
text.setText("0");
button.setText("开始");
}
});
}
}
});
}
/**
* 启动计数线程
*/
private void startCount() {
Thread thread = new Thread(new Runnable() {
public void run() {
int num = 0;
while(isRunning) {
num ++;
final int val = num;
shell.getDisplay().asyncExec(new Runnable() { //启动UI线程,刷新数据
public void run() {
text.setText(val+"");
}
});
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询