-1

我会设计类似于那些 Eclipse 但没有成功的 TitleAreaDialog,下面有两个屏幕截图,Eclipse 表单和我自己的表单,

我在获取顶部和底部分隔符时遇到问题,如果您注意到,Eclipse 表单没有提供边距,并且工作区有边距。

以我自己的形式,我的分隔符有一个边距

Eclipse 表格: http: //farm8.staticflickr.com/7437/9075688435_d5c49770fa_b.jpg

我自己的表格 http://farm4.staticflickr.com/3688/9077919366_8c25c40a79_b.jpg

这是我班级的源代码

public class CompteDialog extends TitleAreaDialog {

private Text idCompteText;
private Text libelleCompteText;
private Text ribCompteText;
private Text agenceCompteText;

public CompteDialog(Shell parentShell) {
    super(parentShell);

}

@Override
public void create() {
    super.create();
    // Set the title
    setTitle("Fiche comptes");
    // Set the message
    setMessage("Saisissez les informations relatives au compte",
            IMessageProvider.INFORMATION);

}

@Override
protected Control createDialogArea(Composite parent) {

    setTitleImage(ResourceManager.getPluginImage(
            "dz.iaityahia.cieptalcars.matresorerie", "icons/wallet.png"));

    GridLayout layout = new GridLayout();

    layout.numColumns = 2;
    parent.setLayout(layout);

    // Champs ID
    Label idCompteLabel = new Label(parent, SWT.NONE);
    idCompteLabel.setText("Compte ID");

    GridData gridData = new GridData(GridData.BEGINNING,
            GridData.BEGINNING, true, false, 1, 1);

    idCompteText = new Text(parent, SWT.BORDER);
    idCompteText.setLayoutData(gridData);
    idCompteText.setTextLimit(20);

    gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false,
            1, 1);

    // Champs libelle
    Label libelleCompteLabel = new Label(parent, SWT.NONE);
    libelleCompteLabel.setText("Libellé compte");

    libelleCompteText = new Text(parent, SWT.BORDER);
    libelleCompteText.setLayoutData(gridData);

    // Champs Rib
    Label ribCompteLabel = new Label(parent, SWT.NONE);
    ribCompteLabel.setText("R.I.B");

    gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false,
            1, 1);

    ribCompteText = new Text(parent, SWT.BORDER);
    ribCompteText.setLayoutData(gridData);

    gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false,
            1, 1);
    // Champs libelle
    Label agenceCompteLabel = new Label(parent, SWT.NONE);
    agenceCompteLabel.setText("Agence bancaire");

    agenceCompteText = new Text(parent, SWT.BORDER);
    agenceCompteText.setLayoutData(gridData);

    // Create a bottom separator
    Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
    line.setLayoutData(new GridData(SWT.FILL, SWT.END, true, true, 2, 1));

    return parent;
}

@Override
protected Control createButtonBar(Composite parent) {

    final Composite buttonBar = new Composite(parent, SWT.NONE);

    final GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    layout.makeColumnsEqualWidth = false;
    layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
    buttonBar.setLayout(layout);

    GridData gridData = new GridData(GridData.FILL, GridData.END, true,
            true, 2, 1);

    buttonBar.setLayoutData(gridData);

    /*
     * org.eclipse.swt.graphics.Color magenta =
     * Display.getDefault().getSystemColor(SWT.COLOR_MAGENTA);
     * buttonBar.setBackground(magenta);
     */

    // Create Add button
    // Own method as we need to overview the SelectionAdapter

    createOkButton(buttonBar, OK, "Add", true);
    // Add a SelectionListener

    // Create Cancel button
    Button cancelButton = createButton(buttonBar, CANCEL, "Cancel", false);
    // Add a SelectionListener
    cancelButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            setReturnCode(CANCEL);
            close();
        }
    });
    return buttonBar;
}

protected Button createOkButton(Composite parent, int id, String label,
        boolean defaultButton) {

    Button button = new Button(parent, SWT.PUSH);

    button.setText(label);
    button.setFont(JFaceResources.getDialogFont());
    button.setData(new Integer(id));
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            if (isValidInput()) {
                okPressed();
            }
        }
    });
    if (defaultButton) {
        Shell shell = parent.getShell();
        if (shell != null) {
            shell.setDefaultButton(button);
        }
    }
    setButtonLayoutData(button);
    return button;
}

private boolean isValidInput() {
    boolean valid = true;
    if (idCompteText.getText().length() == 0) {
        setErrorMessage("Veuillez saisir le compte ID");
        valid = false;
    }
    if (libelleCompteText.getText().length() == 0) {
        setErrorMessage("Veuillez saisir le libellé du compte");
        valid = false;
    }
    return valid;
}

@Override
protected boolean isResizable() {
    return true;
}
}

有人能给我一个想法如何让 TiteAreaDialog 类似于 Eclipse 吗?

非常感谢

4

1 回答 1

0

终于自己解决了这个问题,仔细查看了TitleAreaDilog类的代码,发现没有办法直接做到这一点。

稍微想象一下,我发现了诀窍,有必要创建两个嵌套的复合材料,没有边距的父复合材料将包含两个分隔符,子复合材料有边距并将包含其他控件。

正如我们在下面的示例中看到的那样。

我希望这对像我这样的初学者有帮助

package dz.iaityahia.cieptalcars.matresorerie.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;

public class CompteDialog extends TitleAreaDialog {

 private Text idCompteText;
 private Text libelleCompteText;
 private Text ribCompteText;
 private Text agenceCompteText;

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

@Override
  public void create() {
    super.create();
    // Set the title
    setTitle("Fiche comptes");
    // Set the message
    setMessage("Saisissez les informations relatives au compte", 
        IMessageProvider.INFORMATION);
    setTitleImage(ResourceManager.getPluginImage("dz.iaityahia.cieptalcars.matresorerie", "icons/wallet.png"));


  }

@Override
 protected Control createDialogArea(Composite parent) {

    // cr�ation du Layout du la boite de dialog
    GridLayout parentLayout = new GridLayout();
    parentLayout.numColumns=1;
    parentLayout.marginWidth=0;
    parentLayout.marginHeight=0;
    parent.setLayout(parentLayout);

//Cr�ation du s�parateur sup�rieur  
 Label linetop = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
 linetop.setLayoutData(new GridData (SWT.FILL,SWT.TOP,true,false));

//Cr�ation du composite contenu     
Composite workArea = new Composite(parent, SWT.NONE);
workArea.setLayoutData(new GridData (SWT.FILL,SWT.FILL,true,true));





    GridLayout layout = new GridLayout();

    layout.numColumns = 2;
    layout.marginWidth=10;
    layout.marginHeight=10;
    workArea.setLayout(layout);





   // Champs ID 
   Label idCompteLabel = new Label(workArea,SWT.NONE);
   idCompteLabel.setText("Compte ID");

   idCompteText = new Text(workArea, SWT.BORDER);
   idCompteText.setLayoutData(new GridData(GridData.BEGINNING, GridData.BEGINNING, true,false,1, 1));
   idCompteText.setTextLimit(20);




   // Champs libelle
   Label libelleCompteLabel = new Label(workArea,SWT.NONE);
   libelleCompteLabel.setText("Libell� compte");

   libelleCompteText = new Text(workArea, SWT.BORDER);
   libelleCompteText.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true,false,1, 1));

   // Champs Rib
   Label ribCompteLabel = new Label(workArea,SWT.NONE);
   ribCompteLabel.setText("R.I.B");


   ribCompteText = new Text(workArea, SWT.BORDER);
   ribCompteText.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true,false, 1, 1));

   // Champs Agence bancaire
   Label agenceCompteLabel = new Label(workArea,SWT.NONE);
   agenceCompteLabel.setText("Agence bancaire");

   agenceCompteText = new Text(workArea, SWT.BORDER);
   agenceCompteText.setLayoutData( new GridData(GridData.FILL, GridData.BEGINNING, true,false, 1, 1));




    return parent; 
 }

 @Override
  protected Control createButtonBar(Composite parent) {

     Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
     line.setLayoutData(new GridData (SWT.FILL,SWT.TOP,true,false));


     final Composite buttonBar = new Composite(parent, SWT.NONE);
     buttonBar.setLayoutData(new GridData (SWT.FILL,SWT.TOP,true,false));

     final GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        layout.makeColumnsEqualWidth = false;
        layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
        buttonBar.setLayout(layout);



    // Create Add button
    // Own method as we need to overview the SelectionAdapter

    createOkButton(buttonBar, OK, "Add", true);
    // Add a SelectionListener

    // Create Cancel button
    Button cancelButton = 
        createButton(buttonBar, CANCEL, "Cancel", false);
    // Add a SelectionListener
    cancelButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        setReturnCode(CANCEL);
        close();
      }
    });
    return buttonBar;
  }
于 2013-06-20T19:43:35.783 回答