- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想将我的游戏导出到 .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/
我有一个 Bootstrap 面板,里面有另一个面板。两者在面板标题中都有一个字形图标。默认情况下,面板是折叠的。当面板折叠时,将设置 glyphicon-unchecked。当我点击这个字形图标时,
使用 Grafana 时,我们会在某些电视上使用它。有谁知道制作它的方法,如果有人添加面板,更改布局等,它不仅会自动刷新数据,还会自动刷新实际仪表板? 我们真的厌倦了不断要求具有远程访问权限的人登录并
基本上,我试图让屏幕底部有 3 个按钮,然后在屏幕中间有一个包含文字的标签。但是,我似乎无法同时在 GUI 中同时拥有按钮和标签。我是一名初学者,对布局了解不多(即使我已经阅读过它们),因此任何帮助/
作为学校的类(class)作业,我们必须提出一个自定义项目,我选择了一个原本打算在 ActionScript 上编写的游戏创意,但决定在 Java 上插入它。最初进展顺利,游戏代码正常运行, Spri
我正在尝试使用 Java Graphics 制作一个生命模拟游戏,但是当运行我的代码时,屏幕的左侧三分之一是灰色的。我希望整个屏幕是白色的,黑色方 block 代表生命方 block 。我对所有 ja
下面是动态驱动的一个不错的“下拉面板”。 http://www.dynamicdrive.com/dynamicindex17/dddropdownpanel.htm 如您所见,它是一个面板,在打开时
所以我有这个函数 onDisplayError ,如果请求失败,每次都会调用它。这意味着如果用户按下保存按钮并且 3 个请求失败,我当前会收到 3 个弹出消息。我的目标是此函数检查我的弹出窗口是否已打
我正在尝试为我的一个类(class)制作一款游戏,但在实现一些我想做的事情时遇到了麻烦。本质上,我希望在同一个图形用户界面窗口中包含多个不同的部分。 据我了解,我会使用 JPanel 来创建这些不同的
我目前正在测试这种类型的面板 jquery: http://codyhouse.co/gem/css-slide-in-panel/ 但是当我想检测从顶部滚动以显示一个 div 时:没办法:( 一种检
我可能正在搜索错误的问题,但我找不到这个问题的答案。 我有一个 AutoScroll 设置为 true 的面板。控件动态添加到面板。我需要在滚动条可见时触发一个事件,但我找不到这样的事件。 如有任何建
我有一堆 Bootstrap 面板,其中一些是相互关联的。有点像 Panel A -> Panel B -> Panel C 当用户单击面板 B 时,将显示面板 C,当单击面板 C 时,将显示面板 D
我正在尝试开发一种带有指标的下拉列表,并相应地针对应该出现在仪表板上的选定指标特定面板。反之亦然,如果未选择指标,则面板应隐藏。 我找到了链接 http://search-devops.com/m/k
我有一个窗口,窗口里面有面板。我动态地向面板添加组件。这些组件采用“hbox”布局,以便它们水平排列。单击按钮后,我将在“hbox”布局中再向面板添加一行类似组件。这里的问题是我想在第一行下面添加第二
我使用 jQuery Accordion 将表单拆分为多个面板,并使用 jQuery 验证来检查所需字段。只要验证字段位于打开的面板中,它就可以很好地显示已验证字段中的错误。 举个例子。假设我有三个
我正在尝试检查我的面板是打开还是关闭。 我尝试过这样的: $(document).on('open', '.ui-panel', function(){ console.log('open');
我有一个面板,我想将其 float 在所有窗口之上并出现在所有空间中。这可以通过 轻松完成 [self.panel setLevel:kCGUtilityWindowLevel]; [self.win
我想在格子中的单个面板上叠加多个组,并且想要独立的回归线。 获得多个面板相当容易,每个面板都有一条使用条件因子的回归线: xyplot( Petal.Width ~ Petal.Length |
Delphi 有滑动(动画)面板组件吗? 例如可以在 Raize Components 中找到它(带有“热点”或隐藏/显示按钮的左侧面板)。 我不需要一个可调整大小的面板,而是一个可以从左向右水平平滑
我有一个 Bootstrap Accordion : 我想禁用非事件面板中的所有输入控件。仅验证事件面板。 我有一个检测事件选项卡的功能: $(".panel").on("show.bs.collap
我正在尝试将组件放入不同尺寸的面板中。但是,我意识到 GridLayout 将大小相同的部分分开。如何实现如下图所示 enter image description here import java.
我是一名优秀的程序员,十分优秀!