问题
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)
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);
}
}
回答1:
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"
回答2:
I can't see where you move the ball, but the basic problem is probably, that you use the size of the frame instead of the one of the contentpane of the frame. The size of a frame includes the bar on top of the frame (with the minimizebutton, the titles, etc.) aswell.
来源:https://stackoverflow.com/questions/29127120/jpanel-smaller-than-jframe