效果如图:
实现代码:
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 样式参数图解: