Code is pretty simple. I get a URL string from an arraylist. I am then trying to draw an image in my JPanel. If I don't initialize a layout in the code, it returns an error. If I do initialize a layout, there's no error, but the icon doesn't display either. Am I missing something? Thank you!
代码非常简单。我从arraylist获取了一个URL字符串。然后我尝试在我的JPanel中绘制图像。如果我没有在代码中初始化布局,则会返回错误。如果我初始化布局,则没有错误,但图标也不显示。我错过了什么吗?谢谢!
public class CharacterPage extends JFrame {
private JPanel imagesPanel;
private WikiDB wikiDB;
public CharacterPage(WikiDB db) throws IOException {
super("Character Page");
setContentPane(rootPanel);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setSize(new Dimension(500,500));
imagesPanel.setLayout(new BorderLayout());
this.wikiDB = db;
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ArrayList<String> characterImages =
wikiDB.searchImages(characterID);
//array contains this string: http://cosplayidol.otakuhouse.com/wp-content/uploads/2012/06/s-1-1.jpg
for (int x = 0; x < characterImages.size(); x++){
String imageURL = characterImages.get(x);
Graphics g = getGraphics();
try {
URL url = new URL(imageURL);
//icon.paintIcon(imagesPanel,g,300,100);
JLabel wIcon = new JLabel(new ImageIcon(url));
imagesPanel.setVisible(true);
imagesPanel.add(wIcon);
} catch (MalformedURLException mue){
mue.printStackTrace();
}
}
});
}
Below is a separate program I used to read my URL. Using the code below, I can read my URL without returning any errors, but it still doesn't display in the GUI.
下面是我用来读取URL的单独程序。使用下面的代码,我可以读取我的URL而不返回任何错误,但它仍然不会显示在GUI中。
public class SimpleGUI extends JFrame {
private JPanel imagesPanel;
public SimpleGUI() throws IOException {
super("GUI Page");
setContentPane(imagesPanel);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setSize(new Dimension(500, 500));
imagesPanel.setLayout(new GridLayout());
BufferedImage img1 = null;
try
{
URL url1 = new URL("http://cosplayidol.otakuhouse.com/wp-content/uploads/2012/06/s-1-1.jpg");
URLConnection conn1 = url1.openConnection();
conn1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
InputStream in1 = conn1.getInputStream();
img1 = ImageIO.read(in1);
} catch (IOException e)
{
e.printStackTrace();
}
JLabel wIcon = new JLabel(new ImageIcon(img1));
imagesPanel.add(wIcon);
}
}
EDIT: I am getting closer in that I can now pop up a separate frame containing the picture using this code:
编辑:我越来越接近我现在可以使用此代码弹出一个包含图片的单独框架:
public class SimpleGUI extends JFrame {
private JPanel imagesPanel;
private JFrame mainFrame;
public SimpleGUI() throws IOException {
super("GUI Page");
setContentPane(imagesPanel);
pack();
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(new Dimension(500, 500));
mainFrame = new JFrame();
imagesPanel.setLayout(new GridLayout());
imagesPanel.setBounds(0,0,200,200);
mainFrame.add(imagesPanel);
mainFrame.setVisible(true);
I can't seem to find the original frame in my GUI form, so I can add the imagesPanel to that instead. I am using IntelliJ's GUI form builder.
我似乎无法在我的GUI表单中找到原始帧,因此我可以将imagesPanel添加到其中。我正在使用IntelliJ的GUI表单构建器。
1 个解决方案
#1
One problem, you're trying to add multiple components to a BorderLayout using JPanel from within a for loop and by doing this, you'll over-write all that was added before with anything added new. Use a much more appropriate layout such as a GridLayout instead.
一个问题是,你试图在for循环中使用JPanel将多个组件添加到BorderLayout,并且通过这样做,你将覆盖之前添加的所有添加新内容的所有内容。使用更合适的布局,例如GridLayout。
Another problem, where do you add the imagesPanel to anything? Calling setVisible(true)
on it will do nothing of use, but adding it to the JFrame or to a JPanel that is ultimately held by the JFrame will do a lot.
另一个问题,你在哪里添加imagesPanel?在它上面调用setVisible(true)将无济于事,但将它添加到JFrame或最终由JFrame持有的JPanel将会做很多事情。
Also, you need to do some serious debugging usually before coming here -- does your reading in of the image work at all? Create a simple GUI, one that reads in the Image (use ImageIO.read(URL url) for this), creates an ImageIcon and displays the Icon in a JOptionPane. If this works, then the problem is elsewhere. If it doesn't then work on correcting the URL address.
此外,在来到这里之前,您需要进行一些认真的调试 - 您对图像的阅读是否有效?创建一个简单的GUI,一个读入Image(使用ImageIO.read(URL url)),创建一个ImageIcon并在JOptionPane中显示Icon。如果这样可行,则问题出在其他地方。如果它没有那么工作纠正URL地址。
#1
One problem, you're trying to add multiple components to a BorderLayout using JPanel from within a for loop and by doing this, you'll over-write all that was added before with anything added new. Use a much more appropriate layout such as a GridLayout instead.
一个问题是,你试图在for循环中使用JPanel将多个组件添加到BorderLayout,并且通过这样做,你将覆盖之前添加的所有添加新内容的所有内容。使用更合适的布局,例如GridLayout。
Another problem, where do you add the imagesPanel to anything? Calling setVisible(true)
on it will do nothing of use, but adding it to the JFrame or to a JPanel that is ultimately held by the JFrame will do a lot.
另一个问题,你在哪里添加imagesPanel?在它上面调用setVisible(true)将无济于事,但将它添加到JFrame或最终由JFrame持有的JPanel将会做很多事情。
Also, you need to do some serious debugging usually before coming here -- does your reading in of the image work at all? Create a simple GUI, one that reads in the Image (use ImageIO.read(URL url) for this), creates an ImageIcon and displays the Icon in a JOptionPane. If this works, then the problem is elsewhere. If it doesn't then work on correcting the URL address.
此外,在来到这里之前,您需要进行一些认真的调试 - 您对图像的阅读是否有效?创建一个简单的GUI,一个读入Image(使用ImageIO.read(URL url)),创建一个ImageIcon并在JOptionPane中显示Icon。如果这样可行,则问题出在其他地方。如果它没有那么工作纠正URL地址。