English 中文(简体)
Java SWT 添加的标签不可见
原标题:Java SWT added Label not visible

我新到SWT, 目前正努力让 < code> Label 实例可见。 目前我需要手动调整窗口大小才能显示它。 我发现一个类似的 < href=> https:// stackoverflow.com/ questions/62218/ swt- control- noth-showing- text- not-shell- resize > > 问题 < / a >, 它提到了 SWT 的缓存 。 所以我尝试了 < code>shell.layout (); < /code>, 但对我没用。 这是我的代码 :

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...	Ctrl+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	Ctrl+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	Alt+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	Ctrl+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	Ctrl+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	Ctrl+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	Ctrl+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	Ctrl+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("	Chars: ");
    buf.append(this.styledText.getCharCount());
    buf.append("	Line: ");
    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("
");
        时 时
        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();
时 时

时 时

你有什么建议吗?

Edit: I 将代码片断替换为整个文件。 我发现, 奇怪的行为似乎是由菜单栏触发的。 如果我评论 this. createMenuBar (); 在构建器中创建 create, 标签会被正确视觉化( 由 createCount () 方法创建 ) 。

Edit: 我无法找到正确的解决方案。 因此,在窗口被打开以强制调整大小后,我添加了一个 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

我做了一个屏幕镜头,没有加填的包和调整大小。标签是看不见的。

加上两行:

""https://i.sstatic.net/hEGCB.png" alt="此处输入图像描述"/"

谢谢!

最佳回答

我认为这不一定与缓存有关,请检查:

  1. setting correct layout manager on shell
  2. setting wrong GridData on widgets.
  3. You re not being very specific. Assuming you construct your layout, open your shell and the label is not visible, it might occur that you have set a too small window size. Maybe a simple call of shell.pack() solves the problem?
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签