新更新
现在已经做了另一件事,并且一次在JFrame中显示了多个图像,但是重要的是,我如何将这些图像水平地均匀放置?
package test;
import java.net.URL;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
String[] list = {"test/Spidey.jpg","test/mandarin.jpg","test/jaws.jpg"};
JFrame window = new JFrame();
for(int i=0; i<list.length; i++) {
URL myurl = Test.class.getClassLoader().getResource(list[i]);
ImageIcon img = new ImageIcon(myurl);
JLabel l = new JLabel(img);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(l);
window.pack();
window.setVisible(true);
}
}
}
更新:
让它像这样工作:
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame window = new JFrame();
ClassLoader theClass = spidey.class.getClassLoader();
URL myurl = theClass.getResource("test/Spidey.jpg");
ImageIcon img = new ImageIcon(myurl);
JLabel l = new JLabel(img);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(l);
window.pack();
window.setVisible(true);
}
}
与以前几乎相同,但是这次添加了ImageIcon
答案 0 :(得分:2)
要在JLabel中显示图像,必须使用JLabel(Icon i);但是在这里,您使用的getFile()方法将仅返回文件的名称。 尝试以下
sig_data
答案 1 :(得分:1)
问题出在以下几行:
ClassLoader theClass = spidey.class.getClassLoader();
应该是
ClassLoader theClass = Test.class.getClassLoader();
您也可以将其写成一行,如下所示:
URL myurl = Test.class.getClassLoader().getResource("resources/IMG_20191230_153117.jpg");
答案 2 :(得分:0)
根据您发布的代码,文件Reference <XTextFieldsSupplier> xTextFieldsSupplier (xTextDoc, UNO_QUERY);
if (!xTextFieldsSupplier.is())
return { };
Reference<XNameAccess> xTextFieldsInfo = xTextFieldsSupplier->getTextFieldMasters();
if (!xTextFieldsInfo.is())
return { };
Sequence<OUString> xTextFieldsNames = xTextFieldsInfo->getElementNames();
Any any;
for (::rtl::OUString* field = xTextFieldsNames.begin();
field != xTextFieldsNames.end();
field++) {
std::stringstream field_string;
field_string << *field;
QString fieldName = QString::fromStdString(field_string.str());
any = xTextFieldsInfo->getByName(*field);
Reference< XTextField > xField(any, UNO_QUERY);
// other code to work with xField
}
必须与文件Spidey.jpg
位于同一目录中。
方法spidey.class
返回null。因此,变量getResource()
为空。因此,这部分代码将抛出imageURL
:
NullPOinterException
imageURL.getFile()
请参考 Java教程
中的How to Use Icons