Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JFrame中显示.png图像?_Java_Image_Swing - Fatal编程技术网

Java 在JFrame中显示.png图像?

Java 在JFrame中显示.png图像?,java,image,swing,Java,Image,Swing,我有点困了。为什么这样不行?我只是得到一个错误,说: java.lang.NoSuchMethodError:main 线程“main”中出现异常 您的主要方法应该是: public static void main(String[] args) main需要是静态的,并且必须具有字符串[]的参数,而不是字符串 要修复此问题,请在构造函数中粘贴所有内容,例如 import java.awt.*; import javax.swing.*; @SuppressWarnings("seria

我有点困了。为什么这样不行?我只是得到一个错误,说:

java.lang.NoSuchMethodError:main

线程“main”中出现异常


您的主要方法应该是:

public static void main(String[] args)

main需要是静态的,并且必须具有字符串[]的参数,而不是字符串

要修复此问题,请在构造函数中粘贴所有内容,例如

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); 
  }
}

这是完成的代码:

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);
  } 

}

s/public-void-main(String-arg)/public-static-void-main(String[]arg)RD是正确的,main方法不正确,但是Leo-Izen得到了这个答案,因为他提供了所有的代码。但是,这段代码不太管用。。。您必须在JFrame上使用this.setSize才能使其正确调整大小。无论如何谢谢你!有关实际答案,请参阅下面的帖子。您可以始终使用Frame.pack();更改此.setSize(500640)。。this.getContentPane().add(面板)用于
。。this.getContentPane().add(面板);这个包()
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);
  } 

}