2014-02-10 29 views
5

我正在嘗試爲聊天做一個GUI,我將三個SWT控件放在一行中。如何在Windows上製作SWT按鈕,文字和標籤高度相同?

不幸的是,我找不到他們美麗的路線。

以下代碼

Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new GridLayout(3, false)); 

    Label firstLabel = new Label(shell, SWT.NONE); 
    firstLabel.setText("PROMPT:"); 
    firstLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); 

    Text firstText = new Text(shell, SWT.NONE); 
    firstText.setText("hello"); 
    firstText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 

    Button firstButton = new Button(shell, SWT.PUSH); 
    firstButton.setText("Say"); 
    firstButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); 

    shell.pack(); 
    shell.open(); 
    shell.setSize(400, 300); 

    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 

給出

enter image description here

即文本字段是作爲按鈕一樣高,但文本不裏面垂直居中。

如果代碼文本以下方式

Text firstText = new Text(shell, SWT.NONE); 
    firstText.setText("hello"); 
    firstText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); 

我會得到

enter image description here

最美麗的結果將被

Text firstText = new Text(shell, SWT.BORDER); 
    firstText.setText("hello"); 
    firstText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); 

這是

給出

enter image description here

但是一個按鈕再次比文字稍高。

UPDATE

人說,他們對非Windows平臺的好成績,但問題可能是如何控制SWT控件的填充/利潤率。

更新2

即使在最好的情況下,文本字段高度較小該按鈕的Windows上,其結果是依賴於平臺的。

enter image description here

+1

您目前使用哪種佈局? – Baz

+0

嘗試將'GridLayout'的'verticalSpacing'和'marginHeight'設置爲'0'。這可能會解決你的身高問題。 – Baz

+0

@巴茲沒有幫助。它真的改變了利潤率,但不是高度 – Dims

回答

3

下面是應該給你一個想法如何解決你的問題的代碼示例:

public static void main(String[] args) 
{ 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new GridLayout(3, false)); 

    Label firstLabel = new Label(shell, SWT.NONE); 
    firstLabel.setText("PROMPT:"); 
    firstLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); 

    Text firstText = new Text(shell, SWT.BORDER); 
    firstText.setText("hello"); 
    firstText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); 

    Button firstButton = new Button(shell, SWT.PUSH); 
    firstButton.setText("Say"); 
    firstButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); 

    Label secondLabel = new Label(shell, SWT.NONE); 
    secondLabel.setText("PROMPT:"); 
    secondLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, true)); 

    Text secondText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP); 
    secondText.setText("hello"); 
    secondText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

    Button secondButton = new Button(shell, SWT.PUSH); 
    secondButton.setText("Say"); 
    secondButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, true)); 

    shell.pack(); 
    shell.open(); 
    shell.setSize(400, 300); 

    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 

是這樣的:

enter image description here

沒有SWT.BORDER

enter image description here

+1

看起來像SWT.BORDER幫助你。如果我從'Text firstText = new Text(shell,SWT.BORDER);'更改爲'Text firstText = new Text(shell,SWT.NONE);'我將獲得我的版本 – Dims

+0

@Dims Nope, SWT.BORDER'。 – Baz

+0

所以這是平臺相關的。查看我的更新。 – Dims

0

我使用一個自定義標籤來封裝組合中的實際標籤。

package org.treez.core.swt; 

import org.eclipse.jface.layout.GridDataFactory; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.graphics.Color; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.graphics.Rectangle; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.ui.forms.widgets.FormToolkit; 

/** 
* A label composite that vertically aligns with Text 
* 
*/ 
public class CustomLabel extends Composite { 

    // #region ATTRIBUTES 

    /** 
    * The wrapped label 
    */ 
    Label label; 

    // #end region 

    // #region CONSTRUCTORS 

    /** 
    * Constructor 
    * @param toolkit 
    * 
    * @param parent 
    * @param text 
    */ 
    public CustomLabel(FormToolkit toolkit, Composite parent, String text) { 
     super(parent, SWT.NONE);   
     setGridLayoutWithoutMargins(this); 
     Composite verticalAlignmentContainer = createContainer(parent, toolkit); 
     label = toolkit.createLabel(verticalAlignmentContainer, text);  
    } 

    // #end region 

    // #region METHODS 

    /** 
    * Creates a wrapping container 
    * 
    * @param parent 
    * @param toolkit 
    * @return 
    */ 
    private static Composite createContainer(Composite parent, 
      FormToolkit toolkit) { 
     Composite container = toolkit.createComposite(parent,SWT.NONE);  
     setGridLayoutWithoutMargins(container);   
     return container; 
    } 

    /** 
    * Sets the grid layout 
    * 
    * @param container 
    */ 
    private static void setGridLayoutWithoutMargins(Composite container) { 
     org.eclipse.swt.layout.GridLayout gridLayout = new org.eclipse.swt.layout.GridLayout(1,false); 
     gridLayout.horizontalSpacing = 0;  
     gridLayout.verticalSpacing = 0; 
     gridLayout.marginHeight = 0; 
     gridLayout.marginWidth = 0;  
     container.setLayout(gridLayout); 

     GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false); 
     container.setLayoutData(gridData); 
    } 

    /** 
    * Sets a preferred width for short labels 
    * 
    * @param preferredWidth 
    */ 
    public void setPrefferedWidth(int preferredWidth) { 

     int labelWidth = label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; 
     if (labelWidth < preferredWidth) { 
      GridDataFactory.fillDefaults().hint(preferredWidth, SWT.DEFAULT) 
        .applyTo(label);    
     }    
    } 

    // #end region 

    // #region ACCESSORS 

    /** 
    * Sets the text 
    * 
    * @param text 
    */ 
    public void setText(String text) { 
     label.setText(text); 
    } 

    /** 
    * Returns the text 
    * 
    * @return 
    */ 
    public String getText() { 
     return label.getText(); 
    } 

    /** 
    * Sets the background color 
    */ 
    @Override 
    public void setBackground(Color color) { 
     super.setBackground(color); 
     label.setBackground(color); 
    } 

    /** 
    * Return the size 
    */ 
    @Override 
    public Point getSize(){ 
     return label.getSize(); 
    } 

    /** 
    * Return the bounds 
    */ 
    @Override 
    public Rectangle getBounds(){ 
     return label.getBounds(); 
    } 

    // #end region 

} 
相關問題