0

我是 SWT 的新手,目前正在努力使Label实例可见。目前,我需要手动调整窗口大小以使其显示。我在 SO 上发现了一个类似的问题,提到了 SWT 的缓存。所以我尝试了建议的调用,shell.layout();但它对我不起作用。这是我的代码:

public class SimpleTextEditorCount {
private Display display = new Display();
private Shell shell = new Shell(this.display);
private StyledText styledText;
private boolean unsaved;
private File file;
private String lastDirectory;
private Label status;

public SimpleTextEditorCount() {
    this.shell.setLayout(new GridLayout());
    this.createMenuBar();
    this.styledText = new StyledText(this.shell, SWT.MULTI | SWT.WRAP
            | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    this.styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
    final Font font = new Font(this.shell.getDisplay(), "Book Antiqua", 12,
            SWT.NORMAL);
    this.styledText.setFont(font);
    this.styledText.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent e) {
            unsaved = true;
        }
    });
    this.shell.setText("Editor");
    this.shell.setSize(400, 300);
    this.createCount();
    this.shell.open();
    while (!this.shell.isDisposed()) {
        if (!this.display.readAndDispatch()) {
            this.display.sleep();
        }
    }
    this.display.dispose();
}

private void createMenuBar() { 
    // Create the File top-level menu
    Menu menu = new Menu(this.shell, SWT.BAR);      
    MenuItem open = new MenuItem(menu, SWT.CASCADE);
    open.setText("File");

    Menu dropMenu = new Menu(this.shell, SWT.DROP_DOWN);
    open.setMenu(dropMenu); 

    // File->Open
    open = new MenuItem(dropMenu, SWT.NULL);
    open.setText("Open...\tCtrl+O");
    open.setAccelerator(SWT.CTRL + 'O');
    open.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            loadText();
        }
    });

    // File->Save
    MenuItem save = new MenuItem(dropMenu, SWT.NULL);
    save.setText("Save\tCtrl+S");
    save.setAccelerator(SWT.CTRL + 'S');
    save.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            saveText();
        }
    });

    // File->Save As
    MenuItem saveAs = new MenuItem(dropMenu, SWT.NULL);
    saveAs.setText("Save As...");
    saveAs.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            System.err.println("NEEDS TO BE IMPLEMENTED");
        }
    });

    // File->Exit
    MenuItem exit = new MenuItem(dropMenu, SWT.SEPARATOR); 
    exit = new MenuItem(dropMenu, SWT.NULL);
    exit.setText("Exit\tAlt+F4");
    exit.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            if (saveChanges()) {
                shell.dispose();
            }
        }
    });

    // Create Edit
    MenuItem edit = new MenuItem(menu, SWT.CASCADE);
    edit.setText("Edit");
    dropMenu = new Menu(shell, SWT.DROP_DOWN);
    edit.setMenu(dropMenu); // Create Edit->Cut
    edit = new MenuItem(dropMenu, SWT.NULL);
    edit.setText("Cut\tCtrl+X");
    edit.setAccelerator(SWT.CTRL + 'X');
    edit.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            styledText.cut();
        }
    });

    // Create Edit->Copy
    MenuItem copy = new MenuItem(dropMenu, SWT.NULL);
    copy.setText("Copy\tCtrl+C");
    copy.setAccelerator(SWT.CTRL + 'C');
    copy.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            styledText.copy();
        }
    });

    // Create Edit->Paste
    MenuItem paste = new MenuItem(dropMenu, SWT.NULL);
    paste.setText("Paste\tCtrl+V");
    paste.setAccelerator(SWT.CTRL + 'V');
    paste.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            styledText.paste();
        }
    });

    // Create Select All
    MenuItem selectAll = new MenuItem(dropMenu, SWT.SEPARATOR); 
    selectAll = new MenuItem(dropMenu, SWT.NULL);
    selectAll.setText("Select All\tCtrl+A");
    selectAll.setAccelerator(SWT.CTRL + 'A');
    selectAll.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            System.err.println("NEEDS TO BE IMPLEMENTED");
        }
    });

    MenuItem undo = new MenuItem(dropMenu, SWT.SEPARATOR); // Create Undo
    undo = new MenuItem(dropMenu, SWT.NULL);
    undo.setText("Undo\tCtrl+Z");
    undo.setAccelerator(SWT.CTRL + 'Z');
    undo.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            System.err.println("NEEDS TO BE IMPLEMENTED");
        }
    });

    // Create Help
    MenuItem help = new MenuItem(menu, SWT.CASCADE);
    help.setText("Help");
    dropMenu = new Menu(shell, SWT.DROP_DOWN);
    help.setMenu(dropMenu); // Create Help->About
    help = new MenuItem(dropMenu, SWT.NULL);
    help.setText("About");
    help.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            System.err.println("NEEDS TO BE IMPLEMENTED");
        }
    });
    this.shell.setMenuBar(menu);
}

public void createCount() {
    this.status = new Label(this.shell, SWT.BORDER);
    this.status.setText("test");
    this.status.setLayoutData(new GridData(GridData.FILL,
            GridData.BEGINNING, true, false, 2, 1));
    this.shell.layout();
    styledText.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent arg0) {
            updateStatus();
        }
    });
    styledText.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent arg0) {
        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            updateStatus();
        }
    });
}

private void updateStatus() {
    StringBuffer buf = new StringBuffer();
    buf.append("Offset: ");
    buf.append(this.styledText.getCaretOffset());
    buf.append("\tChars: ");
    buf.append(this.styledText.getCharCount());
    buf.append("\tLine: ");
    buf.append(this.styledText.getLineAtOffset(this.styledText
            .getCaretOffset()) + 1);
    buf.append(" of ");
    buf.append(this.styledText.getLineCount());
    this.status.setText(buf.toString());
}

boolean saveChanges() {
    if (!this.unsaved) {
        return true;
    }
    final MessageBox box = new MessageBox(this.shell, SWT.ICON_WARNING
            | SWT.YES | SWT.NO | SWT.CANCEL);
    box.setMessage("save changes? ");
    box.setText("Editor");
    final int condition = box.open();
    if (condition == SWT.YES) {
        return this.saveText();
    } else if (condition == SWT.NO) {
        return true;
    } else {
        return false;
    }
}

boolean loadText() {
    final FileDialog dialog = new FileDialog(this.shell, SWT.OPEN);
    if (this.lastDirectory != null) {
        dialog.setFilterPath(this.lastDirectory);
    }
    final String selectedFile = dialog.open();
    if (selectedFile == null) {
        System.out.println("File is not opened");
        return false;
    }
    this.file = new File(selectedFile);
    this.lastDirectory = this.file.getParent();

    try {
        final BufferedReader reader = new BufferedReader(new FileReader(
                this.file));
        final StringBuffer buffer = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
            buffer.append("\r\n");
        }
        this.styledText.setText(buffer.toString());
        return true;
    } catch (final IOException e) {
    }
    return false;
}

boolean saveText() {
    if (this.file == null) {
        final FileDialog fileDialog = new FileDialog(this.shell, SWT.SAVE);
        if (this.lastDirectory != null) {
            fileDialog.setFilterPath(this.lastDirectory);
        }
        final String selectedFile = fileDialog.open();
        if (selectedFile == null) {
            System.out.println("File is not saved");
            return false;
        }
        this.file = new File(selectedFile);
        this.lastDirectory = this.file.getParent();
    }
    try {
        final FileWriter writer = new FileWriter(this.file);
        writer.write(this.styledText.getText());
        writer.close();
        this.unsaved = false;
        return true;
    } catch (final IOException e) {
    }
    return false;
}

public static void main(String[] args) {
    new SimpleTextEditorCount();
}

}

你有什么建议吗?谢谢!

编辑:我用整个文件替换了代码片段。我发现奇怪的行为似乎是由菜单栏触发的。如果我在构造函数中注释掉this.createMenuBar();创建,标签将被正确可视化(由createCount()方法创建)。

编辑:我无法找到正确的解决方案。所以我pack()在打开窗口后添加了一个调用来强制调整大小。以下是我对构造函数所做的更改:

//... code 
this.shell.setSize(400, 300);
this.shell.open();
this.shell.pack();
this.shell.setSize(400, 300);
while (!this.shell.isDisposed()) {
    if (!this.display.readAndDispatch()) {
        this.display.sleep();
    }
}
// ... further code

我在没有添加包和调整大小调用的情况下制作了屏幕截图。标签不可见。

并添加了两行:

在此处输入图像描述

谢谢!

4

1 回答 1

0

我认为这不一定与缓存有关。检查以下内容:

  1. 在 shell 上设置正确的布局管理器
  2. 在小部件上设置错误的 GridData。
  3. 你说的不是很具体。假设您构建布局,打开外壳并且标签不可见,则可能是您设置的窗口尺寸过小。也许一个简单的调用shell.pack()可以解决问题?
于 2012-05-26T19:14:28.337 回答