【问题标题】:Error in a Hello World CodenameOne Java programHello World CodenameOne Java 程序中的错误
【发布时间】:2020-07-20 10:24:25
【问题描述】:

我正在尝试使用 Java 中的 CodenameOne 运行一个简单的 Hello World 程序。我正在尝试向应用程序添加一个按钮。我得到两个编译器错误,它们都是:
java: 找不到符号
符号:类按钮

代码如下:

//ORIGIONAL CODE
package CodenameOneHelloWorld;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class MyApplication {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.add(new Label("Hi World"));
        Button b = new Button("Show Dialog"); // LINE HAND-TYPED ACCORDING TO TUTORIAL
        hi.add(b); // LINE HAND-TYPED ACCORDING TO TUTORIAL
        b.addActionListener((e) -> Dialog.show("Dialog Title", "Hi", "OK", null)); // LINE HAND-TYPED ACCORDING TO TUTORIAL
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}

按照编译器的指示,我得到了以下新代码:

// CODE MODIFIED BY SUGGESTION FROM COMPILER
package CodenameOneHelloWorld;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;

import java.awt.*;  // ADDED IMPORT LINE
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class MyApplication {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.add(new Label("Hi World"));
        Button b = new Button("Show Dialog");
        hi.add(String.valueOf(b)); // CHANGED LINE AS SUGGESTED BY COMPILER
        b.addActionListener((e) -> Dialog.show("Dialog Title", "Hi", "OK", null));
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}

新代码编译干净,但在模拟器中运行时,应用显示错误消息而不是按钮。

有人知道如何让按钮在 Hello World 应用中正确显示吗?

【问题讨论】:

    标签: codenameone


    【解决方案1】:

    AWT 在这里不起作用。当您尝试导入时,IDE 提供了多个选项,另一个选项应该是 import com.codename1.ui.Button;。您可以在此处查看 JavaDocs 中支持的类的完整列表:https://www.codenameone.com/javadoc/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 2016-12-03
      相关资源
      最近更新 更多