java能不能在frame上添加图片?
2个回答
展开全部
能,下面的代码应该能满足你。。
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class frame extends JFrame {
public frame(String sTitle,int iWidth,int iHeight)
{
Dimension dim =Toolkit.getDefaultToolkit().getScreenSize();
ImageIcon image=new ImageIcon("image/1.jpg");
setTitle(sTitle);
setIconImage(image.getImage());
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(iWidth,iHeight);
setVisible(true);
Image im=image.getImage();
ImagePanel ip= new ImagePanel(im);
add(ip);
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
frame mf = new frame("Demo",400,400);
}
}
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(Image img) {
this .img = img;
Dimension size = new Dimension(img.getWidth( null ),img.getHeight( null ));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout( null );
}
public void paintComponent(Graphics g) {
g.drawImage(img, 30,30 , null );
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class frame extends JFrame {
public frame(String sTitle,int iWidth,int iHeight)
{
Dimension dim =Toolkit.getDefaultToolkit().getScreenSize();
ImageIcon image=new ImageIcon("image/1.jpg");
setTitle(sTitle);
setIconImage(image.getImage());
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(iWidth,iHeight);
setVisible(true);
Image im=image.getImage();
ImagePanel ip= new ImagePanel(im);
add(ip);
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
frame mf = new frame("Demo",400,400);
}
}
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(Image img) {
this .img = img;
Dimension size = new Dimension(img.getWidth( null ),img.getHeight( null ));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout( null );
}
public void paintComponent(Graphics g) {
g.drawImage(img, 30,30 , null );
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你试试不就知到了,不过有的手机是可以的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询