该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package hello;
import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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 org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Color;
public class SWTtrainning{
public static void main (String[] args) {
Display display=new Display();
Shell shell=new Shell();
Button button=new Button(shell, SWT.PUSH);//在shell上添加一个按钮,并选中按钮类型为PUSH。
shell.setVisible(true);//窗口可见
shell.setBounds(250,0, 900,800);//设置窗口位置和长宽
shell.setText("实验窗口");
button.setBounds(20, 20,20, 20);//设置按钮大小
shell.setBackground(new Color(Display.getCurrent(),80,200,51));//设置窗口背景颜色。
Text text1 = new Text(shell, SWT.BORDER );//shell中添加文本框,并设置文本框格式。
text1.setBackground(new Color(Display.getCurrent(),255,255,0));//设置文本框背景颜色。
text1.setText("");
text1.setBounds(50,50,400,50);
text1.setVisible(true);
Label label1 = new Label(shell, SWT.SINGLE);
label1.setText("red");
label1.setBounds(20, 50, 20, 50);
SelectionListener listener = null ;
button.addSelectionListener(listener);;//按钮被选中时,改变窗口背景颜色。
while (button.getSelection()) {
listener=new SelectionAdapter(){
public void widgetSelected( SelectionEvent event ) {
int a =(int) Math.random()*224;
int b =(int) Math.random()*224;
int c =(int) Math.random()*224;
Color color =new Color(Display.getCurrent(),a,b,c) ;
shell.setBackground(color);;
text1.setText("hello"); }
};
};
while(!shell.isDisposed()) {
}
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}}