Java 试图从一个方法返回两个值
我运行一个GUI来显示MySQL数据库中的每个图像,在单击“下一步”按钮时显示图像之前,它工作正常,现在我不仅要返回图像,还要返回同一方法中的id,这就是我从“图像”更改的原因键入PAIR,但我不知道如何划分该方法返回的两个变量: 配对示例:Java 试图从一个方法返回两个值,java,javafx,Java,Javafx,我运行一个GUI来显示MySQL数据库中的每个图像,在单击“下一步”按钮时显示图像之前,它工作正常,现在我不仅要返回图像,还要返回同一方法中的id,这就是我从“图像”更改的原因键入PAIR,但我不知道如何划分该方法返回的两个变量: 配对示例: public Pair<Integer,Image> image2()throws SQLException { int id; try {
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 Pair image2()引发SQLException
{
int-id;
试一试{
布尔anyResults=false;
如果(rs.next())
{
anyResults=true;
Blob Blob=rs.getBlob(“图像”);
id=rs.getInt(“id”);
InputStream in=blob.getBinaryStream(1,blob.length());
BuffereImage image=ImageIO.read(in);
Image image1=SwingFXUtils.toFXImage(Image,null);
返回新的一对(id,image1);
}
else如果(!anyResults)
{
showMessageDialog(null,“未找到”);
}
}捕获(例外e)
{
e、 printStackTrace();
}
返回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);
}
public void NextButtomClicked()引发SQLException
{
//这就是我之前使用的=>Image-image1=sql.image2();
Pair image1=sql.image2();
this.imageView.setImage(image1);
}
Pair Pair=sql.image2();
整数id=pair.getKey();
Image=pair.getValue();
//现在,使用id和图像执行您想要的操作
您可能还应该处理
null
大小写,因为您的方法可以返回它。如果您同时得到这两个值,那么这对值没有问题
之后,尽管您将imageView.image
设置为不起作用的对
您的image1
不再是Image
类型,而是Pair
类型
您的NextButtomClicked()
应该如下所示:
public void NextButtomClicked() throws SQLException {
Pair<Integer, Image> pair = sql.image2();
this.imageView.setImage(pair.getValue());
this.imageView.setId(pair.getKey());
}
public void NextButtomClicked()引发SQLException{
Pair Pair=sql.image2();
this.imageView.setImage(pair.getValue());
this.imageView.setId(pair.getKey());
}
pair.getValue()
返回对的第二个参数。在您的例子中,图像
pair.getKey()
返回对的第一个参数。在您的例子中,整数
(ID)。一个是键,另一个是值。也许你需要的是一个复合对象,而不是这个。无论哪种方式,您都试图将图像(仅此而已)放入Pair类型,这似乎是问题所在。您的Pair
类的定义是什么?
public void NextButtomClicked() throws SQLException {
Pair<Integer, Image> pair = sql.image2();
this.imageView.setImage(pair.getValue());
this.imageView.setId(pair.getKey());
}