---
title: "Swt - Button"
author: "seong"
date: "2011-10-02T00:28:11Z"
tags: ["Button", "SWT", "push", "toggle"]
categories: ["编程语言"]
slug: "swt-button"
draft: false
lastmod: "2011-10-02T00:57:34Z"
id: 1184410
url: "https://seong.iteye.com/blog/1184410"
---

在Swt里 按钮的分类

普通按钮(SWT.PUSH)
单选按钮(SWT.RADIO)
多选按钮(SWT.CHECK)
箭头按钮(SWT.ARROW)
切换按钮(SWT.TOGGLE)

按钮的对齐方式样式

SWT.LEFT
SWT.RIGH
SWT.CENTER

按钮的外观风格的样式

SWT.FLAT
SWT.BORDER

普通按钮(SWT.PUSH) 例:

效果图

![](http://dl.iteye.com/upload/attachment/563675/500baef1-9dde-3208-b145-c940a4d7b7f9.jpg)

代码


```java

package me.iace.swt.ch4;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class HelloWorld {
	
	public static void main(String[] args) {

		Display display = Display.getDefault();

		Shell shell = new Shell(display, SWT.SHELL_TRIM);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		shell.setText("Hello World");
		
		Button bt1 = new Button(shell, SWT.PUSH|SWT.LEFT);
		bt1.setText("PUSH LEFT");
		bt1.pack();
		
		Button bt2 = new Button(shell, SWT.PUSH|SWT.RIGHT);
		bt2.setText("PUSH RIGHT");
		bt2.pack();
		
		Button bt3 = new Button(shell, SWT.PUSH|SWT.CENTER);
		bt3.setText("PUSH CENTER");
		bt3.pack();
		
		Button bt4 = new Button(shell, SWT.PUSH|SWT.FLAT);
		bt4.setText("PUSH FLAT");
		bt4.pack();
		
		Button bt5 = new Button(shell, SWT.PUSH|SWT.BORDER);
		bt5.setText("PUSH LEFT");
		bt5.pack();

		shell.pack();
		shell.open();
		
		while(!shell.isDisposed()){

			if(!display.readAndDispatch()){

				display.sleep();
			}

		}
		
		display.dispose();
	}

}


```


切换按钮(SWT.TOGGLE) 例:
  切换按钮与普通按钮类似，但是当单击后保持按下状态，再次单击时恢复初始状态。

效果图

![](http://dl.iteye.com/upload/attachment/563681/ee0e8d35-1b58-32fd-a7ed-ef3cd6358737.jpg)


代码

```java

package me.iace.swt.ch4;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class HelloWorld {
	
	public static void main(String[] args) {

		Display display = Display.getDefault();

		Shell shell = new Shell(display, SWT.SHELL_TRIM);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		shell.setText("Hello World");
		
		Button bt1 = new Button(shell, SWT.TOGGLE|SWT.LEFT);
		bt1.setText("TOGGLE LEFT");
		bt1.pack();
		
		Button bt2 = new Button(shell, SWT.TOGGLE|SWT.FLAT);
		bt2.setText("TOGGLE FLAT");
		bt2.pack();
		
		Button bt3 = new Button(shell, SWT.TOGGLE|SWT.BORDER);
		bt3.setText("TOGGLE BORDER");
		bt3.pack();
		

		shell.pack();
		shell.open();
		
		while(!shell.isDisposed()){

			if(!display.readAndDispatch()){

				display.sleep();
			}

		}
		
		display.dispose();
	}

}


```


Radio 按钮

效果图

![](http://dl.iteye.com/upload/attachment/563686/5b6a9d04-1de1-38ce-a2d2-d46ed2626772.jpg)


代码


```java

package me.iace.swt.ch4;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;

public class ButtonRadioCase {

	public static void main(String[] args) {
		
		Display display = new Display();
		
		Layout shellLayout = new FillLayout(SWT.HORIZONTAL);
		
		Shell shell = new Shell(display, SWT.SHELL_TRIM);
		shell.setLayout(shellLayout);
		shell.setText("Radio Button");
		
		Layout groupLayout = new FillLayout(SWT.VERTICAL);
		
		Group radioGroup1 = new Group(shell, SWT.SHADOW_ETCHED_OUT);
		radioGroup1.setLayout(groupLayout);
		radioGroup1.setText("First Style");
		radioGroup1.setToolTipText("First Style");
		
		Button bt1 = new Button(radioGroup1, SWT.RADIO|SWT.LEFT);
		bt1.setText("Radio LEFT");
		bt1.setToolTipText("Radio LEFT");
		
		Button bt2 = new Button(radioGroup1, SWT.RADIO|SWT.RIGHT);
		bt2.setText("Radio RIGHT");
		bt2.setToolTipText("Radio RIGHT");
		
		Button bt3 = new Button(radioGroup1, SWT.RADIO|SWT.CENTER);
		bt3.setText("Radio CENTER");
		bt3.setToolTipText("Radio CENTER");
		
		Group radioGroup2 = new Group(shell, SWT.SHADOW_ETCHED_OUT);
		radioGroup2.setLayout(groupLayout);
		radioGroup2.setText("Second Style");
		
		Button bt4 = new Button(radioGroup2, SWT.RADIO|SWT.FLAT);
		bt4.setText("Radio FLAT");
		bt4.setToolTipText("Radio FLAT");
		bt4.setSelection(true);
		
		Button bt5 = new Button(radioGroup2, SWT.RADIO|SWT.BORDER);
		bt5.setText("Radio BORDER");
		bt5.setToolTipText("Radio BORDER");
		
		Button bt6 = new Button(radioGroup2, SWT.RADIO);
		bt6.setText("Radio");
		bt6.setToolTipText("Radio");
		
		shell.pack();
		shell.open();
		
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		
		display.dispose();
	}
}


```