java - 按下开始按钮Java后程序“冻结”
问题描述
我正在尝试实现一个开始按钮,按下该按钮会将用户带到另一个页面,您可以在其中玩一个简单的游戏。但是,每次我按下开始按钮时,程序都无法正常运行并显示无法玩的游戏。游戏应该有移动平台(在 SimpleWindow 中有效),但在 FrontPage 中按下按钮后就无法工作。如何切换面板以使程序在两者中正确运行?
首页
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
public class FrontPage extends JPanel implements ActionListener {
private JButton start;
private JButton rules;
public FrontPage() {
setLayout(new GridBagLayout());
start = new JButton("Start");
rules = new JButton("Rules");
add(start);
add(rules);
start.addActionListener(this);
rules.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
SimpleWindow window = new SimpleWindow();
add(window);
if (e.getSource() == start){
JFrame w = new JFrame("Simple Window");
w.setBounds(100, 100, 1000, 600);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//SimpleWindow panel = new SimpleWindow();
SimpleWindow panel = window;
w.addKeyListener(panel);
w.add(panel);
w.setResizable(true);
w.setVisible(true);
repaint();
} else if (e.getSource() == start){
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
Font f1 = new Font("TimesRoman", Font.BOLD, (width / 18));
g.setFont(f1);
g.drawString("SQUARED", (int) ((int) width / 2.5), (int) (2.5 * height / 8));
// b.setBounds(50,100,100,100);
}
public static void main(String[] args) {
JFrame a = new JFrame("Squared");
a.setBounds(100, 100, 1000, 600);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FrontPage panel = new FrontPage();
// w.addActionListener(panel);
panel.setBackground(Color.WHITE);
a.add(panel);
a.setResizable(true);
a.setVisible(true);
//panel.run();
}
}
应该运行的程序
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.lang.reflect.Array;
import java.util.Random;
import javax.swing.*;
public class SimpleWindow extends JPanel implements KeyListener {
public static final int DRAWING_WIDTH = 800;
public static final int DRAWING_HEIGHT = 600;
private Runner runner;
private MovingPlatform platform, platform2, platform3, platform4, platform5;
private boolean leftKeyPressed, rightKeyPressed, upKeyPressed;
private int width = getWidth();
private int height = getHeight();
public SimpleWindow() {
super();
runner = new Runner(380, 0);
platform = new MovingPlatform("bricks.png", 200, 500, 200, 50);
platform2 = new MovingPlatform("bricks.png", 600, 400, 200, 50);
platform3 = new MovingPlatform("bricks.png", 200, 300, 200, 50);
platform4 = new MovingPlatform("bricks.png", 400, 200, 200, 50);
platform5 = new MovingPlatform("bricks.png", 100, 100, 200, 50);
setBackground(Color.WHITE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g); // Call JPanel's paintComponent method to paint the background
int width = getWidth();
int height = getHeight();
double ratioX = (double) width / DRAWING_WIDTH;
double ratioY = (double) height / DRAWING_HEIGHT;
Graphics2D g2 = (Graphics2D) g;
AffineTransform at = g2.getTransform();
g2.scale(ratioX, ratioY);
platform.draw(g, this);
platform2.draw(g, this);
platform3.draw(g, this);
platform4.draw(g, this);
platform5.draw(g, this);
runner.draw(g, this);
g2.setTransform(at);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftKeyPressed = true;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightKeyPressed = true;
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
runner.jump();
upKeyPressed = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftKeyPressed = false;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightKeyPressed = false;
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
upKeyPressed = false;
}
}
public void keyTyped(KeyEvent e) {
}
public void run() {
while (true) {
// MAKE A CHANGE
// change runner
if (leftKeyPressed) {
runner.walk(-1);
} else if (rightKeyPressed) {
runner.walk(1);
} else if (upKeyPressed) {
runner.jump();
}
//System.out.println("before fall");
runner.fall(platform, platform2, platform3, platform4, platform5);
// runner.fall(platform2);
checkrunner();
checkplatform();
checkplatform2();
checkplatform3();
checkplatform4();
checkplatform5();
// change platform
platform.moveByAmount(-6, 0);
platform2.moveByAmount(-6, 0);
platform3.moveByAmount(-6, 0);
platform4.moveByAmount(-6, 0);
platform5.moveByAmount(-6, 0);
// SHOW THE CHANGE
repaint();
// WAIT
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void checkrunner() {
int x = runner.getX() + runner.getWidth() / 2;
int y = runner.getY() + runner.getHeight() / 2;
if (x < 0 || x > DRAWING_WIDTH /* || y < 0 */|| y > DRAWING_HEIGHT )
runner = new Runner(380, 0);
}
public void checkplatform() {
int x = platform.getX() + platform.getWidth() / 2;
int y = platform.getY() + platform.getHeight() / 2;
if (x < 0 || x > DRAWING_WIDTH || y < 0 || y > DRAWING_HEIGHT)
platform = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);
}
public int randomNumY() {
int numY = (int) Math.floor((Math.random() * 8)) * 100;
return numY;
}
public void checkplatform2() {
int x2 = platform2.getX() + platform2.getWidth() / 2;
int y2 = platform2.getY() + platform2.getHeight() / 2;
if (x2 < 0 || x2 > DRAWING_WIDTH || y2 < 0 || y2 > DRAWING_HEIGHT) {
platform2 = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);
}
}
public void checkplatform3() {
int x2 = platform3.getX() + platform3.getWidth() / 2;
int y2 = platform3.getY() + platform3.getHeight() / 2;
if (x2 < 0 || x2 > DRAWING_WIDTH || y2 < 0 || y2 > DRAWING_HEIGHT) {
platform3 = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);
}
}
public void checkplatform4() {
int x2 = platform4.getX() + platform4.getWidth() / 2;
int y2 = platform4.getY() + platform4.getHeight() / 2;
if (x2 < 0 || x2 > DRAWING_WIDTH || y2 < 0 || y2 > DRAWING_HEIGHT) {
platform4 = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);
}
}
public void checkplatform5() {
int x2 = platform5.getX() + platform5.getWidth() / 2;
int y2 = platform5.getY() + platform5.getHeight() / 2;
if (x2 < 0 || x2 > DRAWING_WIDTH || y2 < 0 || y2 > DRAWING_HEIGHT) {
platform5 = new MovingPlatform("bricks.png", 600, randomNumY(), 200, 50);
}
}
public void makeChange() {
}
public void showChange() {
repaint();
}
public static void main(String[] args) {
JFrame w = new JFrame("Simple Window");
w.setBounds(100, 100, 1000, 600);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimpleWindow panel = new SimpleWindow();
w.addKeyListener(panel);
w.add(panel);
w.setResizable(true);
w.setVisible(true);
panel.run();
}
}
预期结果:移动平台和跑步者
实际结果:程序“冻结”
解决方案
推荐阅读
- amp-html - 如何将当前时间添加到 amp-video api (1.0v)?
- javascript - Vue JS 根据传递的 props 数据在挂载上获取数据总是返回 false
- tensorflow - 带有 tf.cond 的 TensorFlow 自定义激活函数
- java - JNA 的行为与我的 C# 对应物不同,为什么?
- css - 要并排放置的表单域
- c#-4.0 - 仅更改一个 Console.WriteLine 的颜色
- javascript - 通过单击按钮在 html 页面上显示名称。该按钮是使用数据库中的数据生成的
- shopify - 如何为我的 Shopify 产品动态设置 SEO 标题和描述
- javascript - 在每个间隔后都有一个循环来改变卡片的样式
- unit-testing - 尝试使用 Jest 模拟 fs.readFileSync 时出错