Java 将图片添加到JFrame
我所要做的就是将图片添加到Java 将图片添加到JFrame,java,swing,jlabel,embedded-resource,imageicon,Java,Swing,Jlabel,Embedded Resource,Imageicon,我所要做的就是将图片添加到JFrame 我真的很困惑,不太明白。。。我在这个网站上查找了所有可能的问题,查看了其他java内容,比如论坛。我尽了最大的努力,现在我必须向大家寻求帮助。我希望代码清晰易读。谢谢你的帮助 package zeus; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.ImageIcon; public class Main extends JFrame{ pub
JFrame
我真的很困惑,不太明白。。。我在这个网站上查找了所有可能的问题,查看了其他java内容,比如论坛。我尽了最大的努力,现在我必须向大家寻求帮助。我希望代码清晰易读。谢谢你的帮助
package zeus;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class Main extends JFrame{
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final int SCALE = 1;
public static void Launch(){
JFrame xF = new JFrame("xFrame");
xF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
xF.setResizable(false);
xF.setVisible(true);
xF.setSize(WIDTH*SCALE,HEIGHT*SCALE);
xF.setLocationRelativeTo(null);
xF.add(new JLabel(new ImageIcon("/Clicker/xS/cow.png")));
}
public static void main(String[] args){
Launch();
}
}
非常抱歉造成混淆,eclipse没有显示错误,而且我正在尝试打开一个带有图像的JFrame,我最终可以使用它创建一个按钮来更改int值的值。我能看到的最大问题是
- 从
扩展,但不实际使用它JFrame
- 在并非真正需要时依赖于
静态
- 在实际添加任何内容之前调用
。事实上,通常在添加任何内容之前和显示之后尝试操纵框架属性 公共班机{setVisible
}public static void main(String[] args){ EventQueue.invokeLater(new Runnable() { public void run() { JFrame xF = new JFrame("xFrame"); xF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); xF.add(new JLabel(new ImageIcon("/Clicker/xS/cow.png"))); xF.setResizable(false); xF.setSize(WIDTH*SCALE,HEIGHT*SCALE); xF.setLocationRelativeTo(null); xF.setVisible(true); } } }
但是,由于您从未实际描述过您遇到的问题,因此这些都是猜测…我有一些提示供您参考:
- 如果你知道你的框架大小,那么没有必要把它复杂化
- 尝试使用frame作为JFrame的名称,而不是xF,这样更易于查看
- 重新排列方法,使setVisible(true)可见;在最后
import javax.swing.JFrame;
public class Apollo
{
public static void main(String[] args)
{
Jframe frame = new JFrame("xFrame");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Poseidon());
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
public class Poseidon extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0,0,800,600);
ImageIcon clicker = new ImageIcon("/Clicker/xS/cow.png");
/*The following are two methods for image sizing,
*Use the one that best fits your code:
*
*g.drawImage(clicker.getImage(), x, y, null);
*Fill in the arguments for x and y to locate your upper left corner
*The image will be in it's original size
*
*g.drawImage(clicker.getImage(), x, y, w, h, null);
*Fill in the arguments for w and h to set the width and height of your image
*The image will be in it's scaled size
*/
}
}
面板类
import javax.swing.JFrame;
public class Apollo
{
public static void main(String[] args)
{
Jframe frame = new JFrame("xFrame");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Poseidon());
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
public class Poseidon extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect(0,0,800,600);
ImageIcon clicker = new ImageIcon("/Clicker/xS/cow.png");
/*The following are two methods for image sizing,
*Use the one that best fits your code:
*
*g.drawImage(clicker.getImage(), x, y, null);
*Fill in the arguments for x and y to locate your upper left corner
*The image will be in it's original size
*
*g.drawImage(clicker.getImage(), x, y, w, h, null);
*Fill in the arguments for w and h to set the width and height of your image
*The image will be in it's scaled size
*/
}
}
你可以用
xF.setContentPane(新JLabel(新图像图标(图像路径))您能告诉我们您收到的错误消息是什么吗?请描述您遇到的困难,也请说明您正在尝试做什么。现在,你的问题就要结束了。如果你已经查过了所有可能的问题,你肯定知道如何自己提问。顺便说一句,我编辑了你的问题,以意向声明取代标题——你正在努力实现的目标。以后请不要使用像您原来的标题一样的标题。我怀疑这在很大程度上就是为什么增加了8张反对票。这太傻了。另一个因素可能是,当。。你显然没有。请看一下这个线程,特别是eclipse链接和最后一个链接,它可能会给你整个想法,关于如何去做:-)我所要做的就是在jframe中添加一张图片D:
xF.add(new JLabel(new ImageIcon(“/Clicker/xS/cow.png”)代码>是问题所在。在你的“广泛”研究中,你没有遇到“嵌入式资源”这个术语吗?/Clicker/xS/cow.png
存储在哪里?