gpt4 book ai didi

java - 我可以在代号为 One 的 Dialog.show 中插入图像吗

转载 作者:行者123 更新时间:2023-12-04 10:01:14 24 4
gpt4 key购买 nike

我可以在代号为 One 的 Dialog.show 中插入图像吗?如果是的话,有人可以提供一个例子吗
这就是我想出的:

  img.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Dialog.show("Image:", images(value.toString()), new Command("Cancel"));
}
});

public ImageViewer images(String id) {
int id2 = Integer.parseInt(id);
String img = ServiceTask.getInstance().ReturnImage(id2);
Image placeholder = Image.createImage(getWidth() / 3 - 4, getWidth() / 3 - 4, 0xbfc9d2);
EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
ImageViewer img1 = new ImageViewer(URLImage.createToStorage(encImage, "file" + img,
"http://localhost/projectname/web/images/" + img));

return img1;
}

最佳答案

在您的代码中,您使用了 ImageViewer : 我不确定它是否适合放在 Dialog 中.但是要小心:ImageViewerURLImage如果您想要最高分辨率的照片,则不适合一起使用,因为URLImage总是调整图像大小。

让我们从显示图像的 Dialog 的简单示例开始:

Form hi = new Form("Hi World", BoxLayout.y());
Button btn = new Button("Tap me to show Dialog");
hi.add(btn);
hi.show();

btn.addActionListener(l -> {
Dialog dialog = new Dialog("Example", BoxLayout.y());
dialog.add(new SpanLabel("Example of Dialog including an image"));
dialog.add(new Label(FontImage.createMaterial(FontImage.MATERIAL_RADIO, "Label", 7.0f)));
dialog.setDisposeWhenPointerOutOfBounds(true);
dialog.show();
});

结果:

enter image description here

如果您需要 URLImage ,就像在您的代码中一样,您可以使用它:
Form hi = new Form("Hi World", BoxLayout.y());
Button btn = new Button("Tap me to show Dialog");
hi.add(btn);
hi.show();

btn.addActionListener(l -> {
Dialog dialog = new Dialog("Example", BoxLayout.y());
dialog.add(new SpanLabel("Example of Dialog including an image"));
EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(CN.convertToPixels(30), CN.convertToPixels(30), 0x00ffffff), true);
URLImage meditationIcon = URLImage.createToStorage(placeholder, "gnu_meditation_levitation_small.png",
"https://www.informatica-libera.net/immagini/gnu_meditation_levitation_small.png", URLImage.RESIZE_SCALE_TO_FILL);
meditationIcon.fetch();
dialog.add(FlowLayout.encloseCenter(new Label(meditationIcon)));
dialog.setDisposeWhenPointerOutOfBounds(true);
dialog.show();
});

结果:

enter image description here

然而,在我看来,当 Dialog显示图像应该立即显示,所以 URLImage在这种情况下不是一个好的选择。在类似的情况下,我更喜欢预下载图像,如下面的代码所示。结果是一样的,但是当 Dialog 时图像已经下载了显示(否则不显示):
Form hi = new Form("Hi World", BoxLayout.y());
Button btn = new Button("Tap me to show Dialog");
hi.add(btn);
hi.show();

AsyncResource<Image> myImage = Util.downloadImageToStorage("https://www.informatica-libera.net/immagini/gnu_meditation_levitation_small.png", "myImage.png");

btn.addActionListener(l -> {
Dialog dialog = new Dialog("Example", BoxLayout.y());
dialog.add(new SpanLabel("Example of Dialog including an image"));
try {
// Very small timeout (10 ms), if the Image wasn't pre-downloaded it will not be shown
dialog.add(FlowLayout.encloseCenter(new Label(myImage.get(10).scaledWidth(CN.convertToPixels(30)))));
} catch (InterruptedException ex) {
Log.e(ex);
}
dialog.setDisposeWhenPointerOutOfBounds(true);
dialog.show();
});

您还可以添加“取消”按钮,就像在您的代码中一样:
        Form hi = new Form("Hi World", BoxLayout.y());
Button btn = new Button("Tap me to show Dialog");
hi.add(btn);
hi.show();

AsyncResource<Image> myImage = Util.downloadImageToStorage("https://www.informatica-libera.net/immagini/gnu_meditation_levitation_small.png", "myImage.png");

btn.addActionListener(l -> {
Dialog dialog = new Dialog("Example", BoxLayout.y());
dialog.add(new SpanLabel("Example of Dialog including an image"));
try {
// Very small timeout (10 ms), if the Image wasn't pre-downloaded it will not be shown
dialog.add(FlowLayout.encloseCenter(new Label(myImage.get(10).scaledWidth(CN.convertToPixels(30)))));
} catch (InterruptedException ex) {
Log.e(ex);
}
Button cancel = new Button("Cancel");
cancel.addActionListener(ll -> dialog.dispose());
dialog.add(cancel);
dialog.show();
});

您可以设计您的 Dialog如您所愿,CSS 是一个不错的选择。

关于java - 我可以在代号为 One 的 Dialog.show 中插入图像吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61812115/

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