图片放入jFrame中
时间: 2024-03-16 09:45:05 浏览: 29
要将图片放入JFrame中,可以使用JLabel和ImageIcon来实现。首先,需要创建一个JLabel对象,然后将其添加到JFrame中。接着,通过ImageIcon类将图片加载到JLabel中即可。
以下是一个简单的示例代码,可以将名为“image.jpg”的图片添加到JFrame中:
```java
import javax.swing.*;
public class ImageFrame extends JFrame {
public ImageFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
// 创建JLabel对象
JLabel label = new JLabel();
// 加载图片到JLabel中
label.setIcon(new ImageIcon("image.jpg"));
// 将JLabel添加到JFrame中
getContentPane().add(label);
setVisible(true);
}
public static void main(String[] args) {
new ImageFrame();
}
}
```
在上述代码中,我们首先创建了一个JLabel对象,并通过ImageIcon将图片加载到JLabel中。然后,将该JLabel添加到JFrame的内容面板中。最后,设置JFrame可见并运行程序即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)