在JFrame中显示.png图像? [英] Show .png image in a JFrame?
本文介绍了在JFrame中显示.png图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有点卡住了。为什么不这样做?我只是得到一个错误说:
I am a little stuck. Why wont this work? I just get a error saying:
java.lang.NoSuchMethodError:main
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);
}
}
推荐答案
main需要是静态的,并且必须有一个String []的参数,而不是String。
main needs to be static, and must have an argument of String[], not String.
要修复这个在构造函数中的所有内容,例如
To fix this stick everything in a constructor, such as
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);
}
}
这篇关于在JFrame中显示.png图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文