怎么在Java界面上加图片?
展开全部
设计一个类继承一个面板JPanel,然后重写其paintComponent(Graphics g)方法,在其内绘制一个图片,然后把它加到窗口中就可以了
大概做了下,图片直接绘制,没有缩放,你加的时候把图片和窗口大小弄一样吧
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawImage
{
public static void main(String[] args)
{
JFrame f = new JFrame("drawImage");
f.setSize(600, 400);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container cp = f.getContentPane();
cp.add(new Ib());
f.setVisible(true);
}
}
class Ib extends JPanel
{
public Ib()
{
ii = new ImageIcon("E:\\zhaopian\\luo\\196.jpg");
}
public void paintComponent(Graphics g)
{
Image i = ii.getImage();
g.drawImage(i, 0, 0, this);
}
ImageIcon ii = null;
}
大概做了下,图片直接绘制,没有缩放,你加的时候把图片和窗口大小弄一样吧
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawImage
{
public static void main(String[] args)
{
JFrame f = new JFrame("drawImage");
f.setSize(600, 400);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container cp = f.getContentPane();
cp.add(new Ib());
f.setVisible(true);
}
}
class Ib extends JPanel
{
public Ib()
{
ii = new ImageIcon("E:\\zhaopian\\luo\\196.jpg");
}
public void paintComponent(Graphics g)
{
Image i = ii.getImage();
g.drawImage(i, 0, 0, this);
}
ImageIcon ii = null;
}
展开全部
设置一个JLabel,然后用JLabel的Icon方法把需要的图片加载上去
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(800, 650));
setTitle("登陆界面");
ima1.setBounds(new Rectangle(0,0,800,650));
sryonghuming.setBorder(null);
sryonghuming.setBounds(new Rectangle(354, 313, 175, 20));
srmima.setBorder(null);
srmima.setEchoChar('●');
srmima.setBounds(new Rectangle(354, 361, 175, 20));
denglu.setBackground(Color.white);
denglu.setBounds(new Rectangle(306, 448, 88, 22));
denglu.setBorder(null);
ImageIcon img2=new ImageIcon("img//登录按钮.jpg");
ima2.setIcon(img2);
denglu.setIcon(img2);
denglu.addActionListener(new LoginFrame_denglu_actionAdapter(this));
ImageIcon img3=new ImageIcon("img//取消按钮.jpg");
tuichu.setBackground(Color.white);
tuichu.setBounds(new Rectangle(426, 448, 88, 22));
tuichu.setBorder(null);
tuichu.setIcon(img3);
tuichu.addActionListener(new LoginFrame_tuichu_actionAdapter(this));
contentPane.add(ima1);
contentPane.add(denglu);
contentPane.add(sryonghuming);
contentPane.add(srmima);
contentPane.add(tuichu);
ImageIcon img1=new ImageIcon("img//登录.jpg");
ima1.setIcon(img1);
this.getContentPane().add(ima1);
}
img是我放图片的文件夹//图片名
..//代表返回上一级
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(800, 650));
setTitle("登陆界面");
ima1.setBounds(new Rectangle(0,0,800,650));
sryonghuming.setBorder(null);
sryonghuming.setBounds(new Rectangle(354, 313, 175, 20));
srmima.setBorder(null);
srmima.setEchoChar('●');
srmima.setBounds(new Rectangle(354, 361, 175, 20));
denglu.setBackground(Color.white);
denglu.setBounds(new Rectangle(306, 448, 88, 22));
denglu.setBorder(null);
ImageIcon img2=new ImageIcon("img//登录按钮.jpg");
ima2.setIcon(img2);
denglu.setIcon(img2);
denglu.addActionListener(new LoginFrame_denglu_actionAdapter(this));
ImageIcon img3=new ImageIcon("img//取消按钮.jpg");
tuichu.setBackground(Color.white);
tuichu.setBounds(new Rectangle(426, 448, 88, 22));
tuichu.setBorder(null);
tuichu.setIcon(img3);
tuichu.addActionListener(new LoginFrame_tuichu_actionAdapter(this));
contentPane.add(ima1);
contentPane.add(denglu);
contentPane.add(sryonghuming);
contentPane.add(srmima);
contentPane.add(tuichu);
ImageIcon img1=new ImageIcon("img//登录.jpg");
ima1.setIcon(img1);
this.getContentPane().add(ima1);
}
img是我放图片的文件夹//图片名
..//代表返回上一级
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询