在 JFrame 中使用 imageIcon,但是在框架中我们可以使用什么来将图像添加到框架中?我们可以在 Frame 中的 awt 标签中附加图像吗?
最佳答案
你可以试试这个。将 myImage.jpg 更改为您想要的图像。确保此图像与此源文件位于同一位置。
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
public class Test extends Frame {
ImageIcon a;
WindowListener wl = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
public Test() {
a = new ImageIcon("myImage.jpg");
setSize(a.getIconWidth(), a.getIconHeight());
setLocationRelativeTo(null);
addWindowListener(wl);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(a.getImage(), 0, 0, this);
}
public static void main(String[] args) {
Test myTest = new Test();
}
}
关于java - 我们可以在java中的awt框架中添加图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18215587/