在JFrame中显示.png图像?

编程入门 行业动态 更新时间:2024-10-25 12:17:58
本文介绍了在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图像?

本文发布于:2023-07-27 02:51:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   JFrame   png

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!