在JFrame中使用了imageIcon,但在框架中我们可以使用什么来将图像添加到帧中?我们可以在框架中的awt标签中附加图像吗?
答案 0 :(得分:1)
你可以试试这个。将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();
}
}
答案 1 :(得分:0)
只需在新的JLabel上设置一个imagIcon,然后将这个新的JLabel添加到你想要的框架中。