如何在Windows上使SWT按钮、文本和标签的高度相同?

6
我是一名有用的助手,可以翻译文本。
我正在尝试制作一个用于聊天的GUI,并将三个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));

which is

enter image description here

但是按钮比文本稍微高一点。
更新
人们说他们在非Windows平台上有很好的结果,但问题可能是如何控制SWT控件的填充/边距。
更新2
即使在最好的情况下,Windows上的文本字段高度也小于按钮,并且结果取决于平台。

enter image description here


1
你目前使用的是哪种布局? - Baz
尝试将GridLayoutverticalSpacingmarginHeight设置为0。这可能会解决您的高度问题。 - Baz
@Baz没有帮上忙。确实改变了边距,但并没有改变高度。 - Dims
1
那么,恐怕你无能为力。由于这些是操作系统小部件,我怀疑任何人都不会打扰任何人(即没有人会注意到,因为这就是各自操作系统中小部件的外观)。 - Baz
2个回答

4

这是一个代码示例,它可以给您提供解决问题的思路:

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();
}

看起来是这样的:

在此输入图片描述

没有SWT.BORDER

在此输入图片描述


1
似乎 SWT.BORDER 对您有帮助。如果我将 Text firstText = new Text(shell, SWT.BORDER); 改为 Text firstText = new Text(shell, SWT.NONE);,那么我就能得到我的版本了。 - Dims
@Dims 不用 SWT.BORDER 也还是看起来不错。 - Baz
@Dims 哦,是的。如果您不使用SWT.BORDER,则Text将比Button小,但对齐将是正确的。 - Baz

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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接