简体   繁体   English

Jframe中的图像

[英]Image in Jframe

I am using netbeans IDE 7.4. 我正在使用netbeans IDE 7.4。 I have created a new Java swing jframe.java in new project. 我在新项目中创建了一个新的Java swing jframe.java。 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. 我们如何在具有设计视图的java swing jframe中显示图像。 My code is as follows. 我的代码如下。 Actually i wrote two classes one is builtin jframe class which is default with design view. 实际上我写了两个类,一个是内置的jframe类,在设计视图中是默认的。 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. 并创建了一个名为images的图像,该图像用于为图像创建jpanel,现在我想将该jpanel显示为jframe内置类。

  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并将图像添加到其中即可!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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