package netchat; import org.eclipse.swt. * ; import org.eclipse.swt.widgets. * ; import org.eclipse.swt.events. * ; import org.eclipse.swt.graphics.Color; public class ButtonDemo ... { private Display display; private Shell shell; private Composite composite; private Button noneButton; private Button flatButton; private Button borderButton; private Button toggleButton; private Button radioButton; private Button checkButton; private Button arrowButton; private Button arrow2Button; private Color backgroundColor; private Text myText; public ButtonDemo() ...{ display = Display.getDefault(); // 创建没有显示 // display=Display.getCurrent(); //获取当前显示 shell = new Shell(display, SWT.MAX | SWT.MIN | SWT.RESIZE | SWT.CLOSE); shell.setBackground(new Color(display, 65, 61, 250)); // shell=new Shell(display,SWT.SHELL_TRIM); //与上面的功能一样 composite = new Composite(shell, SWT.NONE); composite.setBounds(5, 5, 475, 315); backgroundColor = new Color(display, 105, 118, 209); composite.setBackground(backgroundColor); noneButton = new Button(composite, SWT.NONE); noneButton.setBounds(10, 10, 80, 40); noneButton.setText("NONE"); noneButton.addSelectionListener(new SelectionAdapter() ...{ public void widgetSelected(SelectionEvent e) ...{ myText.append("You have selected None Button:" + noneButton.getText() + ' '); myText.setBackground(new Color(display, 100, 221, 11)); } }); flatButton = new Button(composite, SWT.FLAT); flatButton.setBounds(110, 10, 80, 40); flatButton.setText("FLAT"); flatButton.addSelectionListener(new SelectionAdapter() ...{ public void widgetSelected(SelectionEvent e) ...{ myText.append("You have selected Flat Button:" + flatButton.getText() + ' '); myText.setBackground(new Color(display, 216, 91, 16)); } }); borderButton = new Button(composite, SWT.BORDER); borderButton.setBounds(10, 70, 80, 40); borderButton.setText("BORDER"); borderButton.addSelectionListener(new SelectionAdapter() ...{ public void widgetSelected(SelectionEvent e) ...{ myText.append("You have selected Border Button:" + borderButton.getText() + ' '); myText.setBackground(new Color(display, 235, 70, 253)); } }); toggleButton = new Button(composite, SWT.TOGGLE); toggleButton.setBounds(110, 70, 80, 40); toggleButton.setText("TOGGLE"); toggleButton.addSelectionListener(new SelectionAdapter() ...{ public void widgetSelected(SelectionEvent e) ...{ myText.append("You have selected Toggle Button:" + toggleButton.getText() + ' '); myText.setBackground(new Color(display, 92, 183, 231)); } }); radioButton = new Button(composite, SWT.RADIO); radioButton.setBounds(10, 120, 80, 40); radioButton.setText("RADIO"); radioButton.addSelectionListener(new SelectionAdapter() ...{ public void widgetSelected(SelectionEvent e) ...{ myText.append("You have selected Radio Button:" + radioButton.getText() + ' '); myText.setBackground(new Color(display, 214, 63, 67)); } }); checkButton = new Button(composite, SWT.CHECK); checkButton.setBounds(110, 120, 80, 40); checkButton.setText("CHECK"); checkButton.addSelectionListener(new SelectionAdapter() ...{ public void widgetSelected(SelectionEvent e) ...{ myText.append("You have selected Check Button:" + checkButton.getText() + ' '); myText.setBackground(new Color(display, 166, 166, 74)); } }); arrowButton = new Button(composite, SWT.ARROW | SWT.LEFT); arrowButton.setBounds(10, 180, 80, 40); arrowButton.addSelectionListener(new SelectionAdapter() ...{ public void widgetSelected(SelectionEvent e) ...{ myText.append("You have selected Left Arrow Button:" + arrowButton.getText() + ' '); myText.setBackground(new Color(display, 203, 73, 102)); } }); arrow2Button = new Button(composite, SWT.ARROW | SWT.RIGHT); arrow2Button.setBounds(110, 180, 80, 40); arrow2Button.addSelectionListener(new SelectionAdapter() ...{ public void widgetSelected(SelectionEvent e) ...{ myText.append("You have selected Right Arrow Button:" + arrow2Button.getText() + ' '); myText.setBackground(new Color(display, 116, 121, 160)); } }); myText = new Text(composite, SWT.WRAP); myText.setBackground(new Color(display, 235, 196, 12)); myText.setBounds(210, 10, 220, 210); shell.setText("按钮实例"); shell.setSize(480, 320); shell.layout(true); shell.open(); while (!shell.isDisposed()) ...{ if (!display.readAndDispatch()) display.sleep(); } } public static void main(String[] args) ...{ new ButtonDemo(); }}