使用 SWT 更改文本时标签被切断

Label cut off when changing text using SWT

所以我构建了一个如下所示的 shell

点击更改路径按钮后,我提示输入路径,然后设置文本。问题是这样做之后,如果新文本太长,它就会被切断。

我正在使用标签来显示路径,而我在有关 Laebl 的侦听器中使用的唯一方法是 setText() 方法。有没有办法避免这种情况发生?我正在使用 SWT 并且更喜欢保持网格布局,这样我就可以拥有 2 列。任何信息都会有帮助。谢谢。

这是代码

String unit[] = {"Pixil", "Inch"};

                    final CustomAttribute realWidth = new CustomAttribute(String.valueOf(obj.getGraph().getWidth(null)));
                    final CustomAttribute realHeight = new CustomAttribute(String.valueOf(obj.getGraph().getHeight(null)));

                    double[] sizes = obj.convertToSizes();
                    if(sizes != null){
                        realWidth.setValue(sizes[0]);
                        realHeight.setValue(sizes[1]);
                    }

                    final Shell shell = new Shell(d);
                    shell.setImage(ApplicationPlugin.getImage(ApplicationPlugin.QUATTRO_ICON));
                    shell.setLayout(new org.eclipse.swt.layout.GridLayout(2,true));
                    shell.setText(EDIT_IMG_WINDOW_TITLE);

                    //width info
                    final Label labelWidth = new Label(shell, SWT.HORIZONTAL);
                    final Text textWidth = new Text(shell,SWT.SINGLE | SWT.BORDER);

                    //height info
                    final Label labelHeight = new Label(shell, SWT.HORIZONTAL);
                    final Text textHeight = new Text(shell,SWT.SINGLE | SWT.BORDER);

                    //units info
                    final Combo unitCombo = new Combo (shell, SWT.READ_ONLY);
                    final Button ratioBox = new Button (shell, SWT.CHECK);
                    ratioBox.setText(EDIT_IMG_RADIO);

                    //path info
                    final Button pathButton = new Button (shell, SWT.PUSH);
                    pathButton.setText(EDIT_IMG_PATH);
                    final Label pathText = new Label(shell, SWT.HORIZONTAL);
                    pathText.setText(obj.getTextData()[0]);

                    Button change = new Button (shell, SWT.PUSH);
                    change.setText(EDIT_IMG_SAVE_BUTTON);
                    ModifyListener heightListener = new ModifyListener(){
                        public void modifyText(ModifyEvent e) {
                            if(realImgListen && textHeight.getText() != ""){
                                realImgListen = false;
                                try{
                                    double oldHeight = realHeight.getDouble();
                                    double newHeight = Double.parseDouble(textHeight.getText());
                                    double oldWidth = Double.parseDouble(textWidth.getText());
                                    if(unitCombo.getSelectionIndex() == 1){
                                        newHeight = newHeight * designer.getPixilPerInch();
                                        oldWidth = oldWidth * designer.getPixilPerInch();
                                    }   
                                    realHeight.setValue(newHeight);
                                    double[] sizes = obj.convertToSizes();
                                    if(sizes != null)
                                        realWidth.setValue(sizes[0]);
                                    else
                                        realWidth.setValue(oldWidth);

                                    if(realHeight.getDouble() > SheetCanvas.sheetYSize)
                                        realHeight.setValue(SheetCanvas.sheetYSize);
                                    if(realHeight.getDouble() < 1)
                                        realHeight.setValue(1);

                                    if(ratioBox.getSelection() == true){
                                        double scale = Double.parseDouble(realHeight.getValue()) / oldHeight;
                                        realWidth.setValue(String.valueOf(realWidth.getDouble()*scale));
                                        if(unitCombo.getSelectionIndex() == 0)
                                            textWidth.setText(String.valueOf((int)Math.round(Double.parseDouble(realWidth.getValue()))));
                                        else
                                            textWidth.setText(String.valueOf(Double.parseDouble(realWidth.getValue())/designer.getPixilPerInch()));
                                    }
                                    obj.storeSizingInfo(realWidth.getDouble(), realHeight.getDouble(), 0);
                                    realImgListen = true;
                                }
                                catch(NumberFormatException e2){
                                    realImgListen = true;
                                }
                            }
                        }
                    };
                    ModifyListener widthListener = new ModifyListener(){
                        public void modifyText(ModifyEvent e) {
                            if(realImgListen && textHeight.getText() != ""){
                                realImgListen = false;
                                try{
                                    double oldWidth = realWidth.getDouble();
                                    double newWidth = Double.parseDouble(textWidth.getText());
                                    double oldHeight = Double.parseDouble(textHeight.getText());
                                    if(unitCombo.getSelectionIndex() == 1){
                                        newWidth = newWidth * designer.getPixilPerInch();
                                        oldHeight = oldHeight * designer.getPixilPerInch();
                                    }   
                                    realWidth.setValue(newWidth);
                                    double[] sizes = obj.convertToSizes();
                                    if(sizes != null)
                                        realHeight.setValue(sizes[1]);
                                    else
                                        realHeight.setValue(oldHeight);

                                    if(realWidth.getDouble() > SheetCanvas.sheetYSize)
                                        realWidth.setValue(SheetCanvas.sheetYSize);
                                    if(realWidth.getDouble() < 1)
                                        realWidth.setValue(1);

                                    if(ratioBox.getSelection() == true){
                                        double scale = Double.parseDouble(realWidth.getValue()) / oldWidth;
                                        realHeight.setValue(String.valueOf(realHeight.getDouble()*scale));
                                        if(unitCombo.getSelectionIndex() == 0)
                                            textHeight.setText(String.valueOf((int)Math.round(Double.parseDouble(realHeight.getValue()))));
                                        else
                                            textHeight.setText(String.valueOf(Double.parseDouble(realHeight.getValue())/designer.getPixilPerInch()));
                                    }
                                    obj.storeSizingInfo(realWidth.getDouble(), realHeight.getDouble(), 0);
                                    realImgListen = true;
                                }
                                catch(NumberFormatException e2){
                                    realImgListen = true;
                                }
                            }
                        }
                    };
                    textHeight.addModifyListener(heightListener);
                    textWidth.addModifyListener(widthListener);
                    unitCombo.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            realImgListen = false;
                            if(unitCombo.getSelectionIndex() == 0){
                                textWidth.setText(String.valueOf((int)Math.rint(realWidth.getDouble())));
                                textHeight.setText(String.valueOf((int)Math.rint(realHeight.getDouble())));
                            }
                            else{
                                textWidth.setText(String.valueOf(realWidth.getDouble()/designer.getPixilPerInch()));
                                textHeight.setText(String.valueOf(realHeight.getDouble()/designer.getPixilPerInch()));
                            }
                            realImgListen = true;
                        }
                    });
                    change.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {

                            Double width = realWidth.getDouble();
                            Double height = realHeight.getDouble();

                            String line1;
                            if(obj.getTextData() != null)
                                line1 = obj.getTextData()[0];
                            else
                                line1 = null;
                            String[] textData = {line1,null};
                            obj.setTextData(textData);
                            obj.storeSizingInfo(Math.rint(width), Math.rint(height),0);
                            designer.updateDataFromSource(settings);
                            designer.repaint();
                            updatePanelButtons();
                            shell.close();
                        }
                    });
                    pathButton.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            //path button
                            String path = null;
                            path = getPathFromBrowser(FILE_CHOOSER_IMAGE_TITLE_STR,DEFAULT_PATH,acceptedImgFormats,null,SWT.OPEN);
                            pathText.setText(path);
                            if(path != null){
                                String[] paths = settings.getCustomImagePath();
                                int pathIndex = 0;
                                for(int i = 0; i < paths.length; i++){
                                    if(obj.getTextData()[0].equals(paths[i]))
                                        pathIndex = i;
                                }
                                paths[pathIndex] = path;
                                settings.setCustomImagePath(paths);
                                paths = settings.getCustomImageChoices();
                                paths[pathIndex] = getFileNameFromPath(path);
                                settings.setCustomImageChoices(paths);
                                designer.updateDataFromSource(settings);
                                String[] textData = obj.getTextData();
                                textData[0] = path;
                                if(textData.length > 1)
                                    textData[1] = null;
                                obj.setTextData(textData);
                            }
                        }
                    });

                    textWidth.setTextLimit(7);
                    textHeight.setTextLimit(7);
                    labelWidth.setText(EDIT_IMG_WIDTH);
                    labelHeight.setText(EDIT_IMG_HEIGHT);
                    unitCombo.setItems(unit);
                    unitCombo.select(0);
                    ratioBox.setSelection(true);
                    org.eclipse.swt.graphics.Rectangle clientArea = shell.getClientArea();
                    unitCombo.setBounds(clientArea.x, clientArea.y, 300, 200);
                    shell.pack();
                    shell.open();
                    Shell shellTemp = SWT_AWT.new_Shell(d, designer);
                    Monitor primary = d.getPrimaryMonitor();
                    org.eclipse.swt.graphics.Rectangle bounds = primary.getBounds();
                    org.eclipse.swt.graphics.Rectangle rect = shell.getBounds();
                    int x = bounds.x + (bounds.width - rect.width) / 2;
                    int y = bounds.y + (bounds.height - rect.height) / 2;
                    shell.setLocation(x, y);
                    shell.moveAbove(shellTemp);
                    shellTemp.dispose();
                    shell.addListener(SWT.Close, new Listener() {
                        @Override  
                        public void handleEvent(Event event) {
                            editingImg();
                          }
                        });
                    realImgListen = false;
                    textWidth.setText(String.valueOf((int)Double.parseDouble(realWidth.getValue())));
                    textHeight.setText(String.valueOf((int)Double.parseDouble(realHeight.getValue())));
                    realImgListen = true;

您似乎正在重新绘制更改路径标签组件,而没有包装标签。 这就是为什么在重绘时部分文本不可见的原因。

使用变形或者你也可以增加屏幕尺寸

您的列的宽度当前设置为最宽控件的宽度 - 可能 'Maintain Image Ratio'。如果您将路径文本设置为比该长度更长的任何内容,它将被截断。

您可以在路径控件上设置宽度提示以设置其大小:

GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
data.widthHint = 100;  // You choose the width
pathText.setLayoutData(data);

请注意,您已指定列的宽度相等,因此这将向第一列添加类似的 space。您可能想切换到不等宽的列。

或者,您可以要求 Shell 在设置新文本后重新计算大小。只需致电:

 shell.pack();