gpt4 book ai didi

java - 当我导出 Java 程序时,为什么出现框架而不出现面板?

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:00 28 4
gpt4 key购买 nike

我想将我的游戏导出到 .jar 文件中。它导出;当我运行它时,框架出现了,但面板没有加载。我的框架和面板位于两个不同的类文件中,但我认为这没有什么区别。而且,它在 Eclipse 中完全可以工作。我在 Runnable Jar File 下导出了 .jar 文件,并将所需的库打包到生成的 jar 中。在 SlashRunnerPanel 中,我导入了许多图像并调整了它们的大小。这会是问题所在吗?

这是我的框架代码:

package baseFiles;

import javax.swing.*;

public class SlashRunnerFrame {
public static void main(String args[]) {
JFrame frame = new JFrame(); // Create new JFrame (the window)

frame.setSize(1000, 800); // Set it to 1000x800 pixels
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Quit on red X

frame.setResizable(false); // Disallow resizing of window

// Change this line if you want to use a different panel
SlashRunnerPanel panel = new SlashRunnerPanel();
frame.add(panel); // Staple the panel to the JFrame
frame.setVisible(true); // So we can see our window
panel.mainLoop();
}
}

这是我的面板代码:

package baseFiles;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import javax.imageio.*;
import java.io.File;
import player.Player;
import surroundings.Obstacle;

public class SlashRunnerPanel extends JPanel implements KeyListener,
ActionListener, MouseListener
{
public static boolean mailbox1 = false;
Color skin = new Color(255, 223, 196);
boolean goo = true;

BufferedImage origMailBox;
BufferedImage house;
BufferedImage ReplaceableBackgroundPic;
BufferedImage origPlayerBack;
BufferedImage origPlayerFront;
BufferedImage origletter;
BufferedImage blackScreen;
BufferedImage exclamation;
BufferedImage borderRocks;
BufferedImage pause;
Image ReplaceableBackground;
Image resizedHouse;
Image mailbox;
Image PlayerBack;
Image PlayerFront;
Image letter;
Image Exclamation;

ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
Obstacle mailboxRegion;
Player player;
Obstacle Beginning;
String ps = System.getProperty("file.separator");

//Animation for walking
boolean aniUp = true;
boolean aniDown;
boolean aniRight;
boolean aniLeft;
boolean lastAni;

//mail
boolean openMail = false;
boolean isMailbox;
boolean mailcontinue;
boolean arrow = true;
boolean showMailRegion;

//Scene
boolean begin = true;
int cmX;
int cmY;
boolean obstaclesInBeginning = true;
boolean showObstInBegin;

//Keys
boolean showObstacles;
boolean stats;
boolean pausebool;

boolean play = true;

public SlashRunnerPanel()
{
try{
borderRocks = ImageIO.read(new File("src" + ps + "Images" + ps + "Boundary.png"));
} catch(Exception e) {System.out.println(e.getMessage());}
try{
exclamation = ImageIO.read(new File("src" + ps + "Images" + ps + "Exclamation.png"));
} catch(Exception e) {System.out.println(e.getMessage());}
try{
origletter = ImageIO.read(new File("src" + ps + "Images" + ps + "Scroll.png"));
} catch(Exception e) {System.out.println(e.getMessage());}
try{
blackScreen = ImageIO.read(new File("src" + ps + "Images" + ps + "BlackScreen.png"));
} catch(Exception e) {e.printStackTrace();}
try{
house = ImageIO.read(new File("src" + ps + "Images" + ps + "House.png"));
} catch(Exception e) {e.printStackTrace();}
try{
ReplaceableBackgroundPic = ImageIO.read(new File("src" + ps + "Images" + ps + "ReplaceableBackground.png"));
} catch(Exception e) {e.printStackTrace();}
try{
origMailBox = ImageIO.read(new File("src" + ps + "Images" + ps + "SmallMailbox.png"));
} catch(Exception e) {e.printStackTrace();}
try{
origPlayerBack = ImageIO.read(new File("src" + ps + "Images" + ps + "WarriorBack.png"));
} catch(Exception e) {e.printStackTrace();}
try{
origPlayerFront = ImageIO.read(new File("src" + ps + "Images" + ps + "WarriorFront.png"));
} catch(Exception e) {e.printStackTrace();}
try{
pause = ImageIO.read(new File("src" + ps + "Images" + ps + "Pause.png"));
} catch(Exception e) {e.printStackTrace();}
}

public void mainLoop()
{

obstacles.add(new Obstacle(140, 170, 300, 545));
obstacles.add(new Obstacle(560, 170, 350, 500));
obstacles.add(new Obstacle(500, 900, 800, 100));

this.setFocusable(true);
addKeyListener(this);
addMouseListener(this);
Exclamation = exclamation.getScaledInstance(20, 20, Image.SCALE_FAST);
letter = origletter.getScaledInstance(500, 500, Image.SCALE_FAST);
ReplaceableBackground = ReplaceableBackgroundPic.getScaledInstance(1600, 2336, Image.SCALE_FAST);
resizedHouse = house.getScaledInstance((int) (getWidth() / 5), (int) (getHeight() / 4), Image.SCALE_FAST);
mailbox = origMailBox.getScaledInstance((int)(getWidth() / 6), (int) (getHeight() / 6), Image.SCALE_FAST);
Image PlayerBack = origPlayerBack.getScaledInstance((int)(getWidth() / 30), (int) (getHeight() / 12), Image.SCALE_FAST);
Image PlayerFront = origPlayerFront.getScaledInstance((int)(getWidth() / 30), (int) (getHeight() / 12), Image.SCALE_FAST);

player = new Player(585, 725, (int)(getWidth() / 30), (int) (getHeight() / 12), false, PlayerFront, PlayerBack);
mailboxRegion = new Obstacle(795, 750, 50, 50);
Beginning = new Obstacle(450, 750, 100, 100);

while(goo)
{
if(player.intersects(Beginning) && obstaclesInBeginning)
player.playerX += 10;
if(!player.dead)
player.move(obstacles);
updateCamera();
if(player.experience == 100)
{
player.experience = 0;
player.experienceLv++;
}
checkCondition();
repaint();
wait(100);
}

}

public void wait(int milsec)
{
try{
Thread.sleep(milsec);
} catch(Exception e) {System.out.println(e.getMessage());}
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);


if(begin && !pausebool && play) //For beginning
{
g.drawImage(ReplaceableBackground, 150 - cmX, -620 - cmY, null);
g.drawImage(resizedHouse, 600 - cmX, 563 - cmY, null);
g.drawImage(mailbox, 720 - cmX, 670 - cmY, null);
g.drawImage(pause, 900, 10, null);
if(obstaclesInBeginning)
g.drawImage(borderRocks,523 - cmX,755 - cmY,null);
}

if(pausebool)
{
g.drawRect(0, 0, 1000, 1000);
}

if(arrow )
{
g.drawImage(Exclamation, 790 - cmX, 665 - cmY, null);
}
player.drawTo(g, 500, 450);


if(isMailbox)
{
g.setColor(Color.magenta);
g.setFont(new Font("AlgerianBasD", Font.BOLD, 15));
g.drawString("Press 'Q' to open the mailbox!", 600, 300);
}

if(showObstInBegin)
{
obstBegin(g);
}

if(play)
{
g.setColor(Color.black);
g.fillRect(738, 35, 250, 30);
g.fillRect(738, 90, 250, 30);
g.setColor(Color.red);
g.fillRect(738, 35, (int)(player.playerHealth * 2.5), 30);
g.setFont(new Font("AlgerianBasD", Font.BOLD, 20));
g.drawString("Health:", 738, 30);
g.setColor(new Color(128, 255, 210));
g.drawString("Experience:", 738, 85);
g.fillRect(738, 90, (int)(player.experience * 2.5), 30);
g.setColor(new Color(10, 133, 255));
g.drawString("" + player.experienceLv, 850, 110);
}

if(showObstacles)
{
drawObst(g, obstacles);
}

if(showMailRegion)
{
drawMailRegion(g);
}

if(stats)
{
g.setColor(Color.yellow);
g.drawRect(1, 600, 200, 300);
g.setFont(new Font("AlgerianBasD", Font.PLAIN, 15));
g.setColor(Color.black);
g.drawString("Stats:", 50, 620);
g.setColor(new Color(140, 0, 0));
g.drawString("Overall Strength: " + player.completeStrength, 3, 650);
g.setColor(new Color(100, 88, 115));
g.drawString("Armor: " + player.playerArmor, 3, 680);
g.setColor(Color.yellow);
g.drawString("X: " + player.playerX, 3, 710);
g.drawString("Y: " + player.playerY, 3, 740);
}

if(openMail && !mailcontinue)
{
g.drawImage(blackScreen,0, 0, null);
g.drawImage(letter, 250, 200, null);
}


}

public void checkCondition()
{
if(player.intersects(mailboxRegion))
{
isMailbox = true;

} if( isMailbox && !player.intersects(mailboxRegion))
{
isMailbox = false;
}
}

public void drawObst(Graphics g, ArrayList<Obstacle> obst)
{
for(int i = 0; i < obst.size(); i++)
{
g.setColor(Color.magenta);
g.fillRect(obst.get(i).getX() - cmX, obst.get(i).getY() - cmY, obst.get(i).getWidth(), obst.get(i).getHeight());
}

}

public void drawMailRegion(Graphics g)
{
g.setColor(Color.cyan);
g.fillRect(mailboxRegion.getX() - cmX, mailboxRegion.getY() - cmY, mailboxRegion.getWidth(), mailboxRegion.getHeight());
}

public void obstBegin(Graphics g)
{
g.setColor(Color.ORANGE);
g.fillRect(Beginning.getX() - cmX, Beginning.getY() - cmY, Beginning.getWidth(), Beginning.getHeight());
}
public void updateCamera()
{
cmX = player.playerX - getWidth() / 2;
cmY = player.playerY - getHeight() / 2;
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------

public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_L)
{
if(stats == true)
{
stats = false;
} else
stats = true;
}

if(isMailbox && !openMail && key == KeyEvent.VK_Q)
{

openMail = true;
isMailbox = false;
arrow = false;
obstaclesInBeginning = false;

}

else if(openMail && key == KeyEvent.VK_Q)
{
obstaclesInBeginning = false;
openMail = false;
repaint();
}

if(key == KeyEvent.VK_EQUALS)
{
if(pausebool)
pausebool = false;
else
pausebool = true;
}

if(key == KeyEvent.VK_BACK_SLASH)
{
if(showObstacles == true)
{
showMailRegion = false;
showObstacles = false;
showObstInBegin = false;
} else {
showObstacles = true;
showMailRegion = true;
showObstInBegin = true;
}
}

if(key == KeyEvent.VK_W)
{
player.up = true;
player.aniUp = true;
player.aniDown = false;
player.aniRight = false;
player.aniLeft = false;

}
if(key == KeyEvent.VK_S)
{
player.down = true;
player.aniDown = true;
player.aniUp = false;
player.aniRight = false;
player.aniLeft = false;

}
if(key == KeyEvent.VK_A)
{
player.left = true;
}
if(key == KeyEvent.VK_D)
{
player.right = true;
}
repaint();
}

public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_W)
{
player.up = false;
}
if(key == KeyEvent.VK_S)
{
player.down = false;
}
if(key == KeyEvent.VK_A)
{
player.left = false;
}
if(key == KeyEvent.VK_D)
{
player.right = false;
}
}

public void keyTyped(KeyEvent e)
{

}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------

public void mousePressed(MouseEvent e)
{

}

public void mouseReleased(MouseEvent e)
{

}

public void mouseEntered(MouseEvent e)
{

}

public void mouseClicked(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{

}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
}
}

最佳答案

尝试在 Event Dispatch Thread 上运行游戏。 Swing 不是线程安全的;如果您尝试在事件调度线程之外创建和修改控件,那么事情可能不会按您的预期工作;控件不显示是很常见的结果。

EventQueue.invokeLater是在 EDT 上运行任务的最常见方法,如下所示: 打包基本文件;

import javax.swing.*;

public class SlashRunnerFrame
{
public static void main(String args [])
{
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame(); // Create new JFrame (the window)

frame.setSize(1000, 800); // Set it to 1000x800 pixels
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Quit on red X

frame.setResizable(false); // Disallow resizing of window

// Change this line if you want to use a different panel
SlashRunnerPanel panel = new SlashRunnerPanel();
frame.add(panel); // Staple the panel to the JFrame
frame.setVisible(true); // So we can see our window
panel.mainLoop();
}
});
}
}

如果这不能解决您的问题,那么几乎可以肯定您的问题出在 SlashRunnerPanel 的代码中,而不是此代码中。

关于java - 当我导出 Java 程序时,为什么出现框架而不出现面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32080191/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com