java - 我可以在代号 One 的 Dialog.show 中插入图像吗
问题描述
我可以在 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
. 但是请注意:如果您想要最大分辨率的照片ImageViewer
,URLImage
则不适合一起使用,因为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"));
dialog.add(new Label(FontImage.createMaterial(FontImage.MATERIAL_RADIO, "Label", 7.0f)));
dialog.setDisposeWhenPointerOutOfBounds(true);
dialog.show();
});
结果:
如果您需要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();
});
结果:
但是,在我看来,当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 是一个不错的选择。
推荐阅读
- javascript - 为什么函数无限期地以角度运行?
- python - Keras:整个预测的自定义损失函数
- python - 用于 TTS 的 Pyttsx 的替代品?
- angular - 如何在 Angular 6 中创建子类?
- fortran - 分配具有单一形状的多个数组
- python-3.x - 在 python 3 中使用 urllib 抓取图像时出现 HTTP 错误
- scala - Spark 根据第一个数据集中的值更新第二个数据集中的值
- elasticsearch - Kafka Connect 在 30 万条记录后停止
- swift - 等待两个不同的 REST/网络调用完成
- r - 尝试执行逐步前向回归,但出现错误?