Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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 JPanel小于JFrame_Java_Swing_Jframe_Jpanel - Fatal编程技术网

Java JPanel小于JFrame

Java JPanel小于JFrame,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我从来没有真正创建过JPanel,但在我的JFrame中我似乎遇到了问题。我的“fball”对象在屏幕上随机出现,我一辈子都不知道为什么。实际上,我没有创建JPanel并使用方法来设置它,也不知道如何进行设置,因为扩展JPanel的类只创建了一个图像。如果你帮助我,我将永远爱你。(我为我缺乏java知识而道歉) 下面是我的窗口类的代码: package game.thirdTry; import java.awt.BorderLayout; import java.awt.Graphi

我从来没有真正创建过
JPanel
,但在我的
JFrame
中我似乎遇到了问题。我的“fball”对象在屏幕上随机出现,我一辈子都不知道为什么。实际上,我没有创建
JPanel
并使用方法来设置它,也不知道如何进行设置,因为扩展
JPanel
的类只创建了一个图像。如果你帮助我,我将永远爱你。(我为我缺乏java知识而道歉)

下面是我的窗口类的代码:

    package game.thirdTry;

import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.JFrame;

public class Window extends JFrame {

    private static Window instance;
    public static Window getInstance() {
        if(instance == null) {
            instance = new Window("Game");
        }
        return instance;
    }
    private Window(String name) {
        super(name);
        setSize(1200, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(null);
        addKeyListener(new UserInput());

        WindowStructure banner = new WindowStructure("Beatles Logo.jpg", 0, 0, getWidth(), 75);
        //WindowStructure fball = new WindowStructure("fireball.100x100.png", 100, 100, 100, 100);

        WindowStructure fball = WindowStructure.getInstanceF();

        System.out.println("Fball.xSize: " + fball.xSize + ", Fball.ySize: " + fball.ySize);
        System.out.println("Fball.xLoc: " + fball.xLoc + ", Fball.yLoc: " + fball.yLoc);
        banner.setBounds(banner.xLoc, banner.yLoc, banner.xSize, banner.ySize);
        fball.setBounds(fball.xLoc, fball.yLoc, fball.xLoc + fball.xSize, fball.ySize + fball.ySize);

        add(fball, null);
        add(banner, null);

        setVisible(true);

        while(true){
            System.out.println("Fball.xLoc: " + fball.xLoc + ", Fball.yLoc: " + fball.yLoc);
            repaint();
            try{
            Thread.sleep(100);
            }catch (Exception e){

            }
        }
    }
/*
    public void paint(Graphics g) {
        super.paintComponents(g);
    }
*/
}

Image Creation Classs (extends JPanel):

    package game.thirdTry;

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.net.URL;

    import javax.swing.ImageIcon;
    import javax.swing.JPanel;

    public class WindowStructure extends JPanel {

    private static WindowStructure fball;

    public static WindowStructure getInstanceF(){
        if(fball == null){
            fball = new WindowStructure("fireball.100x100.png", 300, 100, 100, 100);
        }
        return fball;
    }
    ImageIcon imageIcon;
    int xLoc, yLoc, xSize, ySize;

    public WindowStructure(String bannerImg, int xLoc, int yLoc, int xSize, int ySize){
        URL bannerImgURL = getClass().getResource(bannerImg);
        imageIcon = new ImageIcon(bannerImgURL);
        this.xLoc = xLoc;
        this.yLoc = yLoc;
        this.xSize = xSize;
        this.ySize = ySize;
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(imageIcon.getImage(), xLoc, yLoc, xSize, ySize, null);
    }
    }

我看不出球移动到了哪里,但基本问题可能是,使用的是框架的大小,而不是框架的contentpane。框架的大小还包括框架顶部的条(带有最小化按钮、标题等)

我的“fball”对象在随机的部分离开屏幕,我不知道为什么我的生活


为什么要将“fball”宽度设置为(位置+大小)和高度设置为(大小+大小)?您不会为“横幅”这样做。

在回答您的问题时,不要忘记“接受”答案:横幅对象从不移动,因此我从未遇到过需要设置其边界的问题。fball是这样的,因此边界是(位置,位置+维度)等等,你说得对。我修好了,现在问题解决了。新问题:只有最后一个对象,我给“挫折”渲染和显示up@nicknicknick111,面板是不透明的,因此添加的最后一个面板将指向第一个面板的顶部。您需要使用
setOpaque(…)
方法使面板不透明。不要忘了在这篇帖子和其他帖子中接受解决问题的答案。@nicknicknick111,在答案旁边有一个“复选标记”,你可以点击它来帮助你。我该如何使用contentpaneJFrame。getContentPane()返回框架的contentpane
fball.setBounds(fball.xLoc, fball.yLoc, fball.xLoc + fball.xSize, fball.ySize + fball.ySize);