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

Java 使用SWT更改文本时标签被切断,java,shell,label,swt,Java,Shell,Label,Swt,因此,我构建了一个如下所示的shell 单击“更改路径”按钮后,我会提示输入路径,然后设置文本。问题是这样做之后,如果新文本太长,就会被删除。 我使用一个标签来显示路径,我在侦听器中使用的关于Laebl的唯一方法是setText()方法。有没有办法避免这种情况发生?我使用的是SWT,我更喜欢保持网格布局,这样我就可以拥有2列。任何信息都会有帮助。多谢各位 这是密码 String unit[] = {"Pixil", "Inch"}; final Cu

因此,我构建了一个如下所示的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;
字符串单位[]={“Pixil”,“Inch”};
final CustomAttribute realWidth=新的CustomAttribute(String.valueOf(obj.getGraph().getWidth(null));
final CustomAttribute realHeight=新的CustomAttribute(String.valueOf(obj.getGraph().getHeight(null));
double[]size=obj.convertToSizes();
如果(大小!=null){
realWidth.setValue(大小[0]);
realHeight.setValue(大小[1]);
}
最终壳体=新壳体(d);
setImage(ApplicationPlugin.getImage(ApplicationPlugin.QUATTRO_图标));
setLayout(neworg.eclipse.swt.layout.GridLayout(2,true));
shell.setText(编辑窗口标题);
//宽度信息
最终标签标签宽度=新标签(外壳,SWT水平);
最终文本文本宽度=新文本(shell,SWT.SINGLE | SWT.BORDER);
//高度信息
最终标签高度=新标签(外壳,SWT水平);
最终文本文本高度=新文本(shell,SWT.SINGLE | SWT.BORDER);
//单位信息
最终组合unitCombo=新组合(shell,SWT.READ_ONLY);
最终按钮比率OBOX=新按钮(外壳,SWT.检查);
ratioBox.setText(编辑IMG\u收音机);
//路径信息
最终按钮路径按钮=新按钮(外壳,SWT.PUSH);
设置文本(编辑图像路径);
最终标签路径文本=新标签(外壳,SWT.水平);
setText(obj.getTextData()[0]);
按钮更改=新按钮(外壳,SWT.按钮);
change.setText(编辑IMG保存按钮);
ModifyListener heightListener=新建ModifyListener(){
公共无效修改文本(修改事件e){
if(realImgListen&&textHeight.getText()!=“”){
realImgListen=假;
试一试{
double-oldHeight=realHeight.getDouble();
double newHeight=double.parseDouble(textHeight.getText());
double-oldWidth=double.parseDouble(textWidth.getText());
如果(unitCombo.getSelectionIndex()==1){
newHeight=newHeight*designer.getPixilPerInch();
oldWidth=oldWidth*designer.getPixilPerInch();
}   
realHeight.setValue(newHeight);
double[]size=obj.convertToSizes();
如果(大小!=null)
realWidth.setValue(大小[0]);
其他的
realWidth.setValue(oldWidth);
if(realHeight.getDouble()>SheetCanvas.sheetYSize)
realHeight.setValue(SheetCanvas.sheetYSize);
if(realHeight.getDouble()<1)
realHeight.setValue(1);
if(ratioBox.getSelection()==true){
双刻度=double.parseDouble(realHeight.getValue())/oldHeight;
setValue(String.valueOf(realWidth.getDouble()*scale));
如果(unitCombo.getSelectionIndex()==0)
textWidth.setText(String.valueOf((int)Math.round(Double.parseDouble(realWidth.getValue())));
其他的
textWidth.setText(String.valueOf(Double.parseDouble(realWidth.getValue())/designer.getPixilPerInch());
}
对象storeSizingInfo(realWidth.getDouble(),realHeight.getDouble(),0);
真实的英语=真实的;
}
捕获(NumberFormatException e2){
真实的英语=真实的;
}
}
}
};
ModifyListener widthListener=新建ModifyListener(){
公共无效修改文本(修改事件e){
if(realImgListen&&textHeight.getText()!=“”){
realImgListen=假;
试一试{
double-oldWidth=realWidth.getDouble();
double newWidth=double.parseDouble(textWidth.getText());
double-oldHeight=double.parseDouble(textHeight.getText());
如果(unitCombo.getSelectionIndex()==1){
newWidth=newWidth*designer.getPixilPerInch();
oldHeight=oldHeight*设计
GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
data.widthHint = 100;  // You choose the width
pathText.setLayoutData(data);
 shell.pack();