我运行一个 GUI 来显示 MySQL 数据库中的每个图像,在单击下一个按钮时显示图像没有任何问题之前,它工作正常,现在我不仅要返回图像,还要返回来自的 id同样的方法,这就是为什么我从“Image”类型更改为 PAIR 的原因,但我不明白如何划分从该方法返回的两个变量:
配对示例:
public Pair<Integer,Image> image2()throws SQLException
{
int id;
try {
boolean anyResults = false;
if (rs.next())
{
anyResults = true;
Blob blob = rs.getBlob("image");
id = rs.getInt("id");
InputStream in = blob.getBinaryStream(1, blob.length());
BufferedImage image = ImageIO.read(in);
Image image1 = SwingFXUtils.toFXImage(image,null);
return new Pair<>(id, image1);
}
else if (!anyResults)
{
JOptionPane.showMessageDialog(null, "Not Found");
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
单击下一个按钮的方法示例:
public void NextButtomClicked() throws SQLException
{
// this is what i used before => Image image1 = sql.image2();
Pair<Integer, Image> image1 = sql.image2();
this.imageView.setImage(image1);
}
最佳答案
Pair<Integer, Image> pair = sql.image2();
Integer id = pair.getKey();
Image image = pair.getValue();
// now do what you want with id and image
您可能还应该处理 null
情况,因为您的方法可以返回它。
关于java - 尝试从方法返回两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50527350/