我有点卡住了.为什么不这样做?我只是得到一个错误说:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
rob*_*x44 11
你的主要方法应该是:
public static void main(String[] args)
Run Code Online (Sandbox Code Playgroud)
Leo*_*zen 10
main需要是static,并且必须有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);
}
}
Run Code Online (Sandbox Code Playgroud)