我正在尝试在BufferedImage
上显示一个字符串和JFrame
,它们都是来自方法的输出。我无法将String
与图片分开,因此我需要将两者都添加到JFrame
。
这是我做过的代码,没有显示任何内容。非常感谢您的帮助。
String path = getUserInfo("abc123"); <-- method that returns a string and a buffered image
BufferedImage image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException ex) {
Logger.getLogger(InstagramClient.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame f = new JFrame();
f.setSize(400,400);
f.setVisible(true);
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "icon", -1);
答案 0 :(得分:3)
答案 1 :(得分:3)
基本上,您可以使用标签的setText
方法为标签提供文本值。
您还需要将标签“添加”到框架中,否则它将不会显示任何内容。
String path = getUserInfo("abc123"); <-- method that returns a string and a buffered image
BufferedImage image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException ex) {
Logger.getLogger(InstagramClient.class.getName()).log(Level.SEVERE, null, ex);
}
JFrame f = new JFrame();
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel("This is some text", icon, JLabel.CENTER);
f.add(label);
f.setVisible(true);
答案 2 :(得分:0)
Hovercraft Full Of Eels实际上回答了我的问题。我想要返回String和BufferedImage,但现在我知道这是不可能的。谢谢大家的帮助:))