gpt4 book ai didi

codenameone - 我在 Codename One Hello World 程序中缺少一些导入

转载 作者:行者123 更新时间:2023-12-04 07:25:11 24 4
gpt4 key购买 nike

我想我缺少一些导入,尤其是从 CodenameOne.com 网页导入 CodenameOne Hello World 程序的 Button。请告诉我我需要向该程序添加什么才能将按钮放在程序上并对按钮单击使用react。程序编译并运行,但不显示按钮。
这是代码:

package CodenameOneHelloWorld;


import static com.codename1.ui.CN.*;

import com.codename1.ui.*;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;

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"));
hi.show();
Button b = new Button("Show Dialog");
hi.add(b);
b.addActionListener(e -> Dialog.show("Dialog Title", "Hi", "OK", null));
}

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

public void destroy() {
}

}

最佳答案

如果您忘记了一些导入,代码将无法编译。
按钮未显示,因为您在 hi.show() 之后添加了它.
要使您的代码正常工作,您有两种方法。首先是添加hi.revalidate()最后,像这样:

Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
Button b = new Button("Show Dialog");
hi.add(b);
b.addActionListener(e -> Dialog.show("Dialog Title", "Hi", "OK", null));
hi.revalidate();
第二种方式是在 hi.show()之前添加按钮, 像这样:
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
Button b = new Button("Show Dialog");
hi.add(b);
b.addActionListener(e -> Dialog.show("Dialog Title", "Hi", "OK", null));
hi.show();
原因在开发者指南 ( https://www.codenameone.com/developer-guide.html ) 的“7.1. Layout Reflow”部分有解释:

«Layout in tools such as HTML is implicit, when you add something intothe UI it is automatically placed correctly. Other tools such asCodename One use explicit layout, that means you have to explicitlyrequest the UI to layout itself after making changes! [...] Whenadding a component to a UI that is already visible, the component willnot show by default. [...] That is why, when you add components to aform that is already showing, you should invoke revalidate() oranimate the layout appropriately. [...]»

关于codenameone - 我在 Codename One Hello World 程序中缺少一些导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68241868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com