简体   繁体   中英

Image in Jframe

I am using netbeans IDE 7.4. I have created a new Java swing jframe.java in new project. i created a class. But the image is not displaying neither in design view nor after compilation. image is at same directory. How can we display image in java swing jframe which have a design view. My code is as follows. Actually i wrote two classes one is builtin jframe class which is default with design view. and one is created named images which is use to creat jpanel for an image now i want to show that jpanel into jframe builtin class.

  package javaapplication18;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
/**
*
* @author Dell
*/
public class Images extends JPanel{



  private static Image img;


  public Images(Image a) {
    img = a;
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
    setPreferredSize(size);
    setMinimumSize(size);
    setMaximumSize(size);
    setSize(size);
    setLayout(null);
  }

  @Override
  public void paintComponent(Graphics g) {
    g.drawImage(img, 0, 0, null);
  }

}

    //Now in the main class when i o to source view from design view and 

    static Images panel = new Images(new ImageIcon("helloo.png").getImage());

 public static void main(String args[]) {




    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            MainForm hh = new MainForm();
            hh.setVisible(true);
            hh.panel.setVisible(true);

            hh.add(panel);

            hh.setSize(500,500);


        }
    });
 }

只需将JLabel添加到您的JFrame并将图像添加到其中即可!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM