Javaswing显示图片
时间: 2024-04-12 16:31:17 浏览: 175
要在Java Swing中显示图片,可以使用JLabel组件。下面是一个简单的示例代码:
```java
import javax.swing.*;
import java.awt.*;
public class ImageDisplay extends JFrame {
public ImageDisplay() {
ImageIcon imageIcon = new ImageIcon("path/to/image.jpg"); // 替换为你的图片路径
JLabel label = new JLabel(imageIcon);
getContentPane().add(label);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ImageDisplay());
}
}
```
在上面的示例中,我们创建了一个`JFrame`窗口,并将一个`ImageIcon`对象添加到一个`JLabel`组件中,然后将`JLabel`添加到窗口的内容面板中。最后,我们设置窗口的大小、关闭操作和可见性。
请确保将`"path/to/image.jpg"`替换为你的实际图片路径。启动应用程序后,应该能够看到指定路径下的图片显示在Java Swing窗口中。
阅读全文
相关推荐














