【问题标题】:Show .png image in a JFrame?在 JFrame 中显示 .png 图像?
【发布时间】:2010-11-19 05:47:27
【问题描述】:

我有点卡住了。为什么这行不通?我只是收到一条错误消息:

java.lang.NoSuchMethodError: main

线程“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);
  }
  
}

【问题讨论】:

  • s/public void main(String arg)/public static void main(String [] arg)

标签: java image swing


【解决方案1】:

你的主要方法应该是:

public static void main(String[] args)

【讨论】:

    【解决方案2】:

    main 需要是静态的,并且必须有一个 String[] 的参数,而不是 String。

    在构造函数中修复这个棒的一切,例如

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

    【讨论】:

    • RD 是对的,主要方法不正确,但 Leo Izen 得到了这个答案,因为他提供了所有代码。这段代码虽然不太好用……您必须在 JFrame 上使用 this.setSize 才能使其正确调整大小。不管怎么说,还是要谢谢你!实际答案见我下面的帖子。
    • 你总是可以使用 Frame.pack();
    【解决方案3】:

    这是完成的代码:

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

    【讨论】:

    • this.setSize(500,640); .. this.getContentPane().add(panel); 更改为.. this.getContentPane().add(panel); this.pack();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2014-01-07
    相关资源
    最近更新 更多