JAVA在JFrame中使用哪个函数加载图片然后在其中的某个版面显示出来?
希望各位高手告知下~谢谢!!

解决方案 »

  1.   

    import java.awt.Graphics;
    import java.awt.Image;import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class Test extends JPanel {
    private JFrame frame = null;

    private Image image = null; public Test() {
    image = new ImageIcon("D:/xxx.jpg").getImage();
    frame = new JFrame("Test");
    frame.getContentPane().add(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setVisible(true);
    }

    public void paint(Graphics g){
    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
    } public static void main(String[] arg) {
    new Test();
    }}
      

  2.   

    JLabel imageCS=new JLabel();
            ImageIcon cs=new ImageIcon("cs.jpg");
            imageCS.setIcon(cs);
            jp.add(imageCS);
            
            //this指那个JFrame
            this.getContentPane().add(jp);