JFrame上的String和BufferedImage

时间:2013-03-13 03:11:05

标签: java swing jframe bufferedimage

我正在尝试在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);

3 个答案:

答案 0 :(得分:3)

您可以使用drawString()在图片上呈现字符串,如图here所示。

或者,您可以使用label.setText()并依赖标签的横向&amp;用于定位的垂直对齐,如图here所示。

答案 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,但现在我知道这是不可能的。谢谢大家的帮助:))