Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show .png image in a JFrame?

Tags:

java

image

swing

I am a little stuck. Why wont this work? I just get a error saying:

java.lang.NoSuchMethodError: main

Exception in thread "main"

import java.awt.*; 
import javax.swing.*; 

@SuppressWarnings("serial")
public class ShowPNG extends JFrame
{    

  public void main(String arg) 
  { 
    if (arg == null ) {
        arg = "C:/Eclipse/workspace/ShowPNG/bin/a.png";
    }      
    JPanel panel = new JPanel(); 
    panel.setSize(500,640);
    panel.setBackground(Color.CYAN); 
    ImageIcon icon = new ImageIcon(arg); 
    JLabel label = new JLabel(); 
    label.setIcon(icon); 
    panel.add(label);
    this.getContentPane().add(panel); 
    this.setVisible(true);
  }
  
}
like image 520
djangofan Avatar asked Nov 12 '10 01:11

djangofan


People also ask

How do I add an image to a JFrame?

If you want to add an image, choose the JPictureBox, after that go to Properties and find "icon" property and select an image.

How images are displayed in Java JFrame?

Display an Image in Java Using JLabel. JLabel extends JComponent , and we can attach this component to a JFrame . To read the image file, we use the File class and pass the path of the image. Next we convert the image to a BufferedImage object using ImageIO. read() .

Does JFrame have paintComponent?

JFrame is not a JComponent , it doesn't have a paintComponent method you can override. Instead you could extend a JPanel and add it to the frame.


3 Answers

Your main method should be:

public static void main(String[] args)
like image 140
robert_x44 Avatar answered Oct 28 '22 20:10

robert_x44


main needs to be static, and must have an argument of String[], not String.

To fix this stick everything in a constructor, such as

import java.awt.*; 
import javax.swing.*; 

@SuppressWarnings("serial")
public class ShowPNG extends JFrame
{    
  private ShowPNG(String arg){
      if (arg == null ) {
        arg = "C:/Eclipse/workspace/ShowPNG/bin/a.png";
    }      
    JPanel panel = new JPanel(); 
    panel.setSize(500,640);
    panel.setBackground(Color.CYAN); 
    ImageIcon icon = new ImageIcon(arg); 
    JLabel label = new JLabel(); 
    label.setIcon(icon); 
    panel.add(label);
    this.getContentPane().add(panel); 
  }
  public static void main(String[] args) {
      new ShowPNG(args.length == 0 ? null : args[0]).setVisible(true); 
  }
}
like image 32
Leo Izen Avatar answered Oct 28 '22 19:10

Leo Izen


This was the finished code:

import java.awt.*;  
import javax.swing.*;  

@SuppressWarnings("serial") 
public class ShowPNG extends JFrame {   

  public ShowPNG(String argx) { 
    if ( argx == null ) {
      argx = "a.png";
 }   
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 this.setSize(500,640);
    JPanel panel = new JPanel();  
    //panel.setSize(500,640);
    panel.setBackground(Color.CYAN);  
    ImageIcon icon = new ImageIcon(argx);  
    JLabel label = new JLabel();  
    label.setIcon(icon);  
    panel.add(label); 
    this.getContentPane().add(panel);    
  } 

  public static void main(String[] args) { 
      new ShowPNG(args.length == 0 ? null : args[0]).setVisible(true);
  } 

}
like image 37
djangofan Avatar answered Oct 28 '22 19:10

djangofan