JPanel 比 JFrame 小
JPanel Smaller than JFrame
我从未真正创建过 JPanel
,但我的 JFrame
中似乎存在问题。我的 "fball" 对象不时从屏幕上消失,我这辈子都弄不明白为什么。我实际上并没有创建 JPanel
并使用方法来设置它,并且不知道该怎么做,因为扩展 JPanel
的 class 只创建了一个图像。如果你帮助我,我会永远爱你。 (对于我在java中缺乏知识,我深表歉意)
这是我的代码 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);
}
}
我看不到你把球移到哪里了,但基本问题可能是你使用了框架的大小而不是框架的内容窗格之一。框架的大小包括框架顶部的栏(带有最小化按钮、标题等)。
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);
为什么要将 "fball" 宽度设置为 (location + size),将高度设置为 (size + size)?您不会为 "banner"
执行此操作
我从未真正创建过 JPanel
,但我的 JFrame
中似乎存在问题。我的 "fball" 对象不时从屏幕上消失,我这辈子都弄不明白为什么。我实际上并没有创建 JPanel
并使用方法来设置它,并且不知道该怎么做,因为扩展 JPanel
的 class 只创建了一个图像。如果你帮助我,我会永远爱你。 (对于我在java中缺乏知识,我深表歉意)
这是我的代码 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);
}
}
我看不到你把球移到哪里了,但基本问题可能是你使用了框架的大小而不是框架的内容窗格之一。框架的大小包括框架顶部的栏(带有最小化按钮、标题等)。
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);
为什么要将 "fball" 宽度设置为 (location + size),将高度设置为 (size + size)?您不会为 "banner"
执行此操作