Java SWT-滴定法
我正在尝试使用SWT和JFace创建一个基本对话框类:Java SWT-滴定法,java,swt,jface,Java,Swt,Jface,我正在尝试使用SWT和JFace创建一个基本对话框类: public class AplotBaseDialog extends TitleAreaDialog 我对如何布置对话框感到困惑 在Swing中执行时,我会有一个createDialog方法。然后在该方法中,我将添加JPanel方法的组件。然后将组件添加到我的中心面板。这是基本对话框。每个面板方法都有自己的布局 这是一个非常简单的示例(伪代码) 您可以看到我是如何在方法中创建面板的,而不是将它们作为组件添加到基础面板中 使用titl
public class AplotBaseDialog extends TitleAreaDialog
我对如何布置对话框感到困惑
在Swing中执行时,我会有一个createDialog
方法。然后在该方法中,我将添加JPanel方法的组件。然后将组件添加到我的中心面板。这是基本对话框。每个面板方法都有自己的布局
这是一个非常简单的示例(伪代码)
您可以看到我是如何在方法中创建面板的,而不是将它们作为组件添加到基础面板中
使用
titlearealog
你会怎么做?我还没有使用titlearealog
,但这里有一个简单的对话框,我自己用。它应该能让您了解对话框的内部工作原理。它基本上只是一个带有一些错误消息和复选框的对话框:
public class CheckboxDialog extends Dialog {
private String message = "";
private String checkboxMessage = "";
private boolean checkValue;
private Button checkButton;
/* Constructor, set shell style and set block on open (rest of gui is blocked until closed) */
public CheckboxDialog(Shell parentShell) {
super(parentShell);
setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.OK | SWT.APPLICATION_MODAL);
setBlockOnOpen(true);
}
/* creates the content of the dialog */
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
/* set the layout for the content (gridlayout with 1 column)*/
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = 15;
layout.marginWidth = 30;
composite.setLayout(layout);
/* add a label with some text */
final Label content = new Label(composite, SWT.NONE);
content.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
content.setText(message);
/* add a checkbox button */
checkButton = new Button(composite, SWT.CHECK);
checkButton.setText(checkboxMessage);
checkButton.setSelection(true);
checkButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
return composite;
}
/* create the dialog buttons (in this case, only an OK button) */
protected void createButtonsForButtonBar(Composite parent)
{
createButton(parent, IDialogConstants.OK_ID, "OK", true);
}
/* configure the dialog's shell (set title) */
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("Error");
}
/* this method is executed if the OK button is pressed */
public void okPressed()
{
checkValue = checkButton.getSelection();
close();
}
/* getter and setter methods */
public void setMessage(String message) {
this.message = message;
}
public void setCheckboxMessage(String checkboxMessage) {
this.checkboxMessage = checkboxMessage;
}
public boolean getCheckBoxValue()
{
return checkValue;
}
}
如您所见,SWT中没有add
方法。您只需在每个小部件的构造函数中指定父级
此外,Vogella提供了一个非常好的教程,详细介绍了如何创建JFace对话框。这是另一个关于如何使用标题readialog
的示例,如果我理解正确的话-当您打开对话框时,它就像一个空画布。然后你开始创建你的复合材料。受保护的空白a(复合父项){@jktater是的,一个简单对话框是空的,即包含一个空的composite
。此composite
填充在createDialogArea(复合父项)中
方法,然后返回。当您打开对话框时,会自动调用此方法。您不必自己执行此操作。如果我理解正确,当您打开对话框时,就像一个空画布。然后开始创建复合。受保护的void a(复合父项){layout manager}受保护的void b(复合父对象){layout manager}因此a和b复合对象都位于对话框窗口中显示的父复合对象中,并且它们的布局来自方法中的布局管理器
public class CheckboxDialog extends Dialog {
private String message = "";
private String checkboxMessage = "";
private boolean checkValue;
private Button checkButton;
/* Constructor, set shell style and set block on open (rest of gui is blocked until closed) */
public CheckboxDialog(Shell parentShell) {
super(parentShell);
setShellStyle(SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.OK | SWT.APPLICATION_MODAL);
setBlockOnOpen(true);
}
/* creates the content of the dialog */
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
/* set the layout for the content (gridlayout with 1 column)*/
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = 15;
layout.marginWidth = 30;
composite.setLayout(layout);
/* add a label with some text */
final Label content = new Label(composite, SWT.NONE);
content.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
content.setText(message);
/* add a checkbox button */
checkButton = new Button(composite, SWT.CHECK);
checkButton.setText(checkboxMessage);
checkButton.setSelection(true);
checkButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
return composite;
}
/* create the dialog buttons (in this case, only an OK button) */
protected void createButtonsForButtonBar(Composite parent)
{
createButton(parent, IDialogConstants.OK_ID, "OK", true);
}
/* configure the dialog's shell (set title) */
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("Error");
}
/* this method is executed if the OK button is pressed */
public void okPressed()
{
checkValue = checkButton.getSelection();
close();
}
/* getter and setter methods */
public void setMessage(String message) {
this.message = message;
}
public void setCheckboxMessage(String checkboxMessage) {
this.checkboxMessage = checkboxMessage;
}
public boolean getCheckBoxValue()
{
return checkValue;
}
}