SWT实现下拉(Combo)和单选框(Button)

效果如图:
在这里插入图片描述
实现代码:

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;


/**
 * 
 * @author jg
 * 2022年2月17日
 */
public class PictureFormatDialog extends TitleAreaDialog {
	private String info;
	private IProject obj;
	private String[] pictureFormats = { "iRGBA8888", "iRGB888", "iRGB565", "iBGRA8888", "iBGR888", "iBGR565" };
	private Button radio1;
	private Button radio2;
	private Combo combo;
	private String selectFormat = "iRGBA8888";
	private boolean isCreateFile;

	public PictureFormatDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	public PictureFormatDialog(Shell parentShell, String info, IProject obj) {
		super(parentShell);
		// TODO Auto-generated constructor stub
		setHelpAvailable(false);
		setShellStyle(getShellStyle() | SWT.RESIZE);
		this.info = info;
		this.obj = obj;
	}

	@Override
	protected Point getInitialSize() {
		// TODO Auto-generated method stub
		return new Point(400, 300);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		//设置对话框信息Messages.getString("setPictureFormat")可替换为其他字符串
		setMessage(Messages.getString("setPictureFormat"));
		getShell().setText(Messages.getString("setPictureFormat"));
		Composite container = new Composite(parent, SWT.None);
		container.setLayout(new GridLayout(2, false));
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		container.setBackground(parent.getBackground());
		container.setFont(parent.getFont());
		GridData gd = new GridData(SWT.FILL);
		gd.grabExcessHorizontalSpace = true;
		gd.widthHint = 100;
		gd.heightHint = 25;
		Label actCmd = new Label(container, SWT.None);
		actCmd.setText(Messages.getString("selectPictureFormat") + " : ");
		combo = new Combo(container, SWT.READ_ONLY);
		combo.setItems(pictureFormats);
		combo.setFocus();
		combo.setLayoutData(gd);
		if (StringUtils.isEmpty(info)) {
			combo.setText(pictureFormats[0]);
		} else {
			String formate=info.split(",")[0];
			combo.setText(formate);
			selectFormat=formate;
		}			
		combo.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				selectFormat = combo.getText();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
			}
		});
		Label isGenerateFile = new Label(container, SWT.None);
		isGenerateFile.setText(Messages.getString("IsGenerateFile") + " : ");
		//占一个空位置
		Label placeholder = new Label(container, SWT.None);
		placeholder.setText("");
		radio1 = new Button(container, SWT.RADIO);
		radio1.setText(Messages.getString("YES") + " : ");// 设置按钮上的文字
		radio2 = new Button(container, SWT.RADIO);
		radio2.setText(Messages.getString("NO") + " : ");
		if (StringUtils.isNotEmpty(info)) {
			if ("1".equals(info.split(",")[1])) {
				radio1.setSelection(true);// 设置按钮处于选择状态
			}else if ("0".equals(info.split(",")[1])) {
				radio2.setSelection(true);// 设置按钮处于选择状态
			}
		}else {
			radio1.setSelection(true);// 设置按钮处于选择状态
		}
		return container;
	}

	
	@Override
	protected void okPressed() {
		if (radio1.getSelection()) {
			isCreateFile=true;
		}else if (radio2.getSelection()) {
			isCreateFile=false;
		}
		//selectFormat = combo.getText();
		super.okPressed();
	}

	public String getSelectPictureFormat() {

		return selectFormat;
	}

	public boolean isCreateFile() {
		return isCreateFile;
	}

	public void setCreateFile(boolean isCreateFile) {
		this.isCreateFile = isCreateFile;
	}
}

Button 样式参数图解:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值