Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中获得一张快乐的脸_Java_Swing_Embedded Resource_Imageicon - Fatal编程技术网

在Java中获得一张快乐的脸

在Java中获得一张快乐的脸,java,swing,embedded-resource,imageicon,Java,Swing,Embedded Resource,Imageicon,在Java GUI中显示图像文件src/happyFace.gif时遇到问题。目标是显示一张笑脸的图像,它似乎以一定角度滑过程序窗口,从窗口边缘反弹 我认为我的问题在于src/rebombpanel.java中的image变量(类型ImageIcon),因为ImageIcon类可能与将来的swing版本不兼容(根据Oracle文档:)。如果这是真的,我认为ImageIcon类可能无法被swing库支持。我不知道如何检查我的swing库 src/happyFace.gif 我的输出窗口 src

在Java GUI中显示图像文件src/happyFace.gif时遇到问题。目标是显示一张笑脸的图像,它似乎以一定角度滑过程序窗口,从窗口边缘反弹

我认为我的问题在于src/rebombpanel.java中的image变量(类型ImageIcon),因为ImageIcon类可能与将来的swing版本不兼容(根据Oracle文档:)。如果这是真的,我认为ImageIcon类可能无法被swing库支持。我不知道如何检查我的swing库

src/happyFace.gif

我的输出窗口

src/responder.java:

//********************************************************************
// Rebound.java Java Foundations
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Rebound{
//-----------------------------------------------------------------
// Displays the main frame of the program.
//-----------------------------------------------------------------
    public static void main (String[] args){
        JFrame frame = new JFrame ("Rebound");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ReboundPanel());
        frame.pack();
        frame.setVisible(true);
    }
}
src/panel.java:

//********************************************************************
// ReboundPanel.java Java Foundations
//
// Represents the primary panel for the Rebound program.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ReboundPanel extends JPanel{
    private final int WIDTH = 300, HEIGHT = 100;
    private final int DELAY = 20, IMAGE_SIZE = 35;
    private ImageIcon image;
    private Timer timer;
    private int x, y, moveX, moveY;
    //-----------------------------------------------------------------
    // Sets up the panel, including the timer for the animation.
    //-----------------------------------------------------------------
    public ReboundPanel(){
        timer = new Timer(DELAY, new ReboundListener());
        image = new ImageIcon ("happyFace.gif");
        x = 0;
        y = 40;
        moveX = moveY = 3;
        setPreferredSize (new Dimension(WIDTH, HEIGHT));
        setBackground (Color.black);
        timer.start();
    }
    //-----------------------------------------------------------------
    // Draws the image in the current location.
    //-----------------------------------------------------------------
    public void paintComponent (Graphics page){
        super.paintComponent (page);
        image.paintIcon (this, page, x, y);
    }
    //*****************************************************************
    // Represents the action listener for the timer.
    //*****************************************************************
    private class ReboundListener implements ActionListener{
        //-----------------------------------------------------------------
        // Updates the position of the image and possibly the direction
        // of movement whenever the timer fires an action event.
        //-----------------------------------------------------------------
        public void actionPerformed (ActionEvent event){
            x += moveX;
            y += moveY;
            if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
                moveX = moveX * -1;
            if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
                moveY = moveY * -1;
            repaint();
        }
    }
}
//********************************************************************
//java.java基础
//
//表示回弹程序的主面板。
//********************************************************************
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类面板扩展JPanel{
专用最终整型宽度=300,高度=100;
私有最终整数延迟=20,图像大小=35;
私有图像图标图像;
私人定时器;
私有整数x,y,moveX,moveY;
//-----------------------------------------------------------------
//设置面板,包括动画的计时器。
//-----------------------------------------------------------------
公共事务委员会(){
计时器=新计时器(延迟,新侦听器());
image=新图像图标(“happyFace.gif”);
x=0;
y=40;
moveX=moveY=3;
setPreferredSize(新尺寸(宽度、高度));
挫折背景(颜色:黑色);
timer.start();
}
//-----------------------------------------------------------------
//在当前位置绘制图像。
//-----------------------------------------------------------------
公共组件(图形页){
super.paintComponent(第页);
image.paintIcon(此,第页,x,y);
}
//*****************************************************************
//表示计时器的操作侦听器。
//*****************************************************************
私有类侦听器实现ActionListener{
//-----------------------------------------------------------------
//更新图像的位置,可能还更新方向
//每当计时器触发动作事件时的移动次数。
//-----------------------------------------------------------------
已执行的公共无效操作(操作事件){
x+=moveX;
y+=moveY;
如果(x=宽度图像大小)
moveX=moveX*-1;
if(y=高度-图像大小)
moveY=moveY*-1;
重新油漆();
}
}
}

面板
类中

更改
image=newimageicon(“happyFace.gif”)

image=newimageicon(“src/happyFace.gif”)


请注意,这种解决方案只能用于测试目的。正如在的评论中所述,存储和加载图像的正确方法是在
面板
类中使用。

更改
image=newimageicon(“happyFace.gif”)

image=newimageicon(“src/happyFace.gif”)


请注意,这种解决方案只能用于测试目的。正如在的评论中所述,存储和加载图像的正确方法是使用。

“我认为我的问题是”,具体是什么?然后请用a看你的问题。你能详细说明你的问题吗。图标是否未显示,或者动画/交互部分是否已损坏?哦,对不起,图标甚至未显示。我将在当前窗口中添加一个屏幕截图我不知道如何显示图片,但屏幕截图链接在“我的输出窗口”中。我还详细阐述了我认为问题所在。提示:添加@AxelH(或任何人,
@
很重要)以通知此人新的评论。“我最终还是在URL对象中出现了笑脸。”很高兴你把它分类了。:)“我想我的问题是”,到底是什么?然后请用a看你的问题。你能详细说明你的问题吗。图标是否未显示,或者动画/交互部分是否已损坏?哦,对不起,图标甚至未显示。我将在当前窗口中添加一个屏幕截图我不知道如何显示图片,但屏幕截图链接在“我的输出窗口”中。我还详细阐述了我认为问题所在。提示:添加@AxelH(或任何人,
@
很重要)以通知此人新的评论。“我最终还是在URL对象中出现了笑脸。”很高兴你把它分类了。:)