我想将我的游戏导出为.jar文件。它出口;当我跑它时,框架出现了,但面板没有加载。我的框架和面板有两个不同的类文件,但我认为这没有什么区别。此外,它完全适用于Eclipse。我将我的.jar文件导出到Runnable Jar File下,并将所需的库打包到生成的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
}
}
答案 0 :(得分:0)
尝试在Event Dispatch Thread上运行游戏。 Swing不是线程安全的;如果您尝试创建和修改事件调度线程的控件,那么事情可能无法按预期工作;控件不显示是一个非常常见的结果。
EventQueue.invokeLater
是在EDT上运行任务的最常用方法,如下所示:
package baseFiles;
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
的代码中,而不是此代码。
答案 1 :(得分:0)
尝试设置panel.setPreferredSize(new Dimension(someHeight,someWidth)),然后设置frame.pack()
编辑:
试试这个: package baseFiles;
import javax.swing.*;
import java.awt.Dimension;
public class SlashRunnerFrame {
JFrame frame = new JFrame();
SlashRunnerPanel panel = new SlashRunnerPanel();
public SlashRunnerFrame(){
// Create new JFrame (the window)
panel.mainLoop();
panel.setPreferredSize(new Dimension(1000, 800));
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
frame.add(panel); // Staple the panel to the JFrame
frame.setVisible(true); // So we can see our window
frame.pack();
}
public static void main(String args[]) {
new SlashRunnerFrame();
}
}
看看它是否有效
在将面板添加到框架之前修改面板。
答案 2 :(得分:0)
我明白了。当它加载到.jar文件中的图像时,它试图访问我桌面上的文件夹而不是jar文件中的文件夹。所以它发送了一个错误,因为它试图访问一个超出.jar文件的文件。我的解决方案是取代:
try{
yourImageVariableName = ImageIO.read(new File("yourImagePath1" + ps + "yourImagePath2" + ps + "yourImage.png"));
} catch(Exception e) {e.printStackTrace();}
使用:
try{
yourImageVariableName = ImageIO.read(yourClassName.class.getResourceAsStream("/yourImagePath2/yourImage.png"));
} catch(Exception e) {e.printStackTrace();}