【问题标题】:Label cut off when changing text using SWT使用 SWT 更改文本时标签被切断
【发布时间】:2017-04-02 13:35:55
【问题描述】:

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

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

我使用标签来显示路径,我在监听器中使用的关于 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;

【问题讨论】:

  • 文字比你的标签高,你可以增加你的标签的大小这可以帮助你
  • 你能把你的代码放上来吗?
  • 你在用Layouts吗?
  • 我使用的是网格布局

标签: java shell label swt


【解决方案1】:

列的宽度当前设置为最宽控件的宽度 - 可能是“保持图像比例”。如果您将路径文本设置为超过该长度的任何内容,它将被截断。

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

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

请注意,您已指定列的宽度相同,因此这将为第一列添加相似的空间。您可能想要切换到不等宽的列。

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

 shell.pack();

【讨论】:

    【解决方案2】:

    看起来您正在重绘更改路径标签组件并且您没有包装标签。 这就是为什么在重绘部分文本时不可见的原因。

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

    【讨论】:

    • 屏幕大小对裁切没有影响,什么是翘曲?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    相关资源
    最近更新 更多