JPanel比JFrame小 [英] JPanel Smaller than JFrame

查看:173
本文介绍了JPanel比JFrame小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未真正创建过JPanel,但是在我的JFrame内部似乎有问题.我的球"对象随机出现在屏幕外,我无法弄清楚为什么会导致我的一生.我实际上并没有创建JPanel并使用方法对其进行设置,并且不知道如何执行此操作,因为扩展了JPanel的类仅创建图像.如果您帮助我,我将永远爱您. (我对我缺乏Java知识深表歉意)

I never actually create a JPanel, but I seem to be having issues with it inside of my JFrame. my "fball" object goes off the screen at random parts and I cannot figure out why for the life of me. I don't actually create a JPanel and use the methods to set it up, and dont know how to do that since my class that extends JPanel only creates an image. If you help me I will love you forever. (I apologize for my lacking of knowledge in java)

这是我的Window Class的代码:

Here is the code for my Window Class:

    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);
    }
    }

推荐答案

我的"fball"对象随机出现在屏幕外,我无法弄清楚为什么会成为我的生命

my "fball" object goes off the screen at random parts and I cannot figure out why for the life of me

fball.setBounds(fball.xLoc, fball.yLoc, fball.xLoc + fball.xSize, fball.ySize + fball.ySize);

为什么要将球"的宽度设置为(位置+尺寸),将高度设置为(尺寸+尺寸)?您不为横幅"做此事

Why are you setting the "fball" width to be (location + size) and height to be (size + size)? You don't do this for the "banner"

这篇关于JPanel比JFrame小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆