我一直在构建一个简短的程序,基本上在JPanel上绘制一个太空船,并听取指示程序射击子弹的键.问题是它甚至没有在屏幕上绘制宇宙飞船或子弹.我还怀疑KeyBindings可能无法工作,因为这是以前的问题(我可能已经或可能没有修复),但手头的主要问题仍然是我的屏幕没有被绘制.这是我的代码:
public enum Direction {
LEFT, RIGHT, SPACE
}
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame;
Ship s1;
Shoot shoot;
// Set the frame up
frame = new JFrame();
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
// Get some more necessary objects
s1 = new Ship();
shoot = new Shoot(s1);
frame.getContentPane().add(shoot);
s1.setShoot(shoot);
// Threads
Thread ship = new Thread(s1);
ship.start();
}
}
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import …Run Code Online (Sandbox Code Playgroud)