0
好的,这可能被标记为重复问题,但请首先听到我的声音。我已经浏览了所有的互联网,并尝试过各种方式在JFrame
中显示图像,但没有任何工作适合我。有没有简单,万无一失!如何在JFrame
中显示图像?因为如果它不是万无一失的,我肯定要搞砸了:/如何在JFrame中显示图像
好的,这可能被标记为重复问题,但请首先听到我的声音。我已经浏览了所有的互联网,并尝试过各种方式在JFrame
中显示图像,但没有任何工作适合我。有没有简单,万无一失!如何在JFrame
中显示图像?因为如果它不是万无一失的,我肯定要搞砸了:/如何在JFrame中显示图像
你可以试试这个,我评论过我做了什么,在哪里试图使它可以理解的,包括一些关于从this后调整。看你如何得到:)
public class SO2 {
public static void main(String[] args) {
try {
//Step 1, read in image using javax.ImageIO
BufferedImage img = ImageIO.read(new File("D:/Users/user/Desktop/Tree.jpeg"));
//Optional, if you want to resize image this is an effective way of doing it
Image scaled = img.getScaledInstance(200, 200, Image.SCALE_SMOOTH);
//Step 2 create frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Step 3 add image to Frame
frame.add(new JLabel(new ImageIcon(scaled)));
//Step 4 Pack frame which sizes it around it's contents, then Show
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
圣杯!非常感谢!有用!我花了大约24小时在线寻找解决方案,我终于找到了它! *欢乐的泪水*非常感谢! – Coarse
向我们展示你的代码和你试过的东西。其实你没有尝试任何东西。 [首先击中谷歌](http://stackoverflow.com/questions/18027833/adding-image-to-jframe)。 – pzaenger
是的,我没有尝试从字面上理解所有东西,但是我尝试了这些......并且像Google的下5页一样。 – Coarse
_“因为如果它不是万无一失的话,我一定会搞砸的:/”_ - 我喜欢诚实:-) –