gpt4 book ai didi

java - Java中如何弹出图片

转载 作者:行者123 更新时间:2023-11-30 03:28:32 30 4
gpt4 key购买 nike

我正在用java制作一个项目,其中在if语句中弹出图像。我在网上查了大约一个小时,我发现的任何内容都给了我一个错误,我不知道如何修复。这是一个例子,Java, how can I popup a dialog box as only an image? 。使用威士忌蜘蛛建议的代码并导入 java.awt.image.BufferedImage;
javax.swing.ImageIcon;
javax.swing.JLabel;
后我在 ImageIo 处收到错误和File在第一行。如果您能以任何方式帮助我,请告诉我我正在使用 netbeans 并且我有 javax.swing.JOptionPane;已经导入了,所以如果这对你有帮助的话(抱歉,听起来我懒得研究东西,因为我只有 12 岁,我的注意力持续时间不是最长的)

最佳答案

这就是你 looking for 。它将向您展示如何创建仅包含图像的对话框。

但是,如果您不想在那里阅读,我已在下面输入了代码片段供您查看。

(经过尝试和测试)

<小时/>
import javax.swing.JOptionPane; //imports
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.awt.Dimension;

public class ProjectileSim{

public static void main(String[] args){
JFrame f = new JFrame(); //creates jframe f

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //this is your screen size

f.setUndecorated(true); //removes the surrounding border

ImageIcon image = new ImageIcon("IMAGEURL.jpg"); //imports the image

JLabel lbl = new JLabel(image); //puts the image into a jlabel

f.getContentPane().add(lbl); //puts label inside the jframe

f.setSize(image.getIconWidth(), image.getIconHeight()); //gets h and w of image and sets jframe to the size

int x = (screenSize.width - f.getSize().width)/2; //These two lines are the dimensions
int y = (screenSize.height - f.getSize().height)/2;//of the center of the screen

f.setLocation(x, y); //sets the location of the jframe
f.setVisible(true); //makes the jframe visible
}
}

这将解决您的问题。请记得将您想要显示的图片粘贴到编译文件夹中,以便“ImageIcon”可以轻松找到它。还要记住指定正确的图片扩展名! (即 .jpg、.png、.gif)

祝一切顺利:)

请让我知道结果。

祝你好运!

关于java - Java中如何弹出图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636217/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com