代码之家  ›  专栏  ›  技术社区  ›  djangofan

在JFrame中显示.png图像?

  •  5
  • djangofan  · 技术社区  · 15 年前

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

    java.lang.nosuchmethoderror:主

    线程“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);
      }
    
    }
    
    3 回复  |  直到 15 年前
        1
  •  10
  •   Leo Izen    15 年前

    main必须是静态的,并且必须有string[]的参数,而不是string。

    要在构造函数中修复此Stick的所有内容,例如

    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); 
      }
    }
    
        2
  •  11
  •   robert_x44    15 年前

    你的主要方法应该是:

    public static void main(String[] args)
    
        3
  •  4
  •   djangofan    15 年前

    这是完成的代码:

    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);
      } 
    
    }
    
    推荐文章