Show .png image in a JFrame?

前端 未结 3 1791
不知归路
不知归路 2021-01-19 00:01

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

java.lang.NoSuchMethodError: main

Exception in thread "main"

<
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-19 00:25

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

提交回复
热议问题