下面我在我的 Java 程序中包含了两个类(Launcher 和 Controls)。在这两个类中,他们创建了一个 JFrame,绘制了一个背景图像,并添加了文本行。我一遍又一遍地查看代码,但由于某种原因,背景图像和文本行没有出现在第二类(控件)中。谁能向我解释为什么会这样?
发射器类:
import hungerGames.Display;
import hungerGames.RunGame;
import hungerGames.input.InputHandler;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Launcher extends JFrame implements Runnable {
public static final long serialVersionUID = 1L;
protected JPanel window = new JPanel();
private int width = 800;
private int height = 450;
boolean running = false;
Thread thread;
public Launcher(int id) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
setUndecorated(true);
setSize(new Dimension(width, height));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
window.setLayout(null);
InputHandler input = new InputHandler();
addKeyListener(input);
addFocusListener(input);
addMouseListener(input);
addMouseMotionListener(input);
startMenu();
}
public void updateFrame() {
if (InputHandler.dragged) {
Point p = getLocation();
if (InputHandler.MouseDX != InputHandler.MousePX || InputHandler.MouseDX != InputHandler.MousePX) {
setLocation(p.x + InputHandler.MouseDX - InputHandler.MousePX, p.y + InputHandler.MouseDY - InputHandler.MousePY);
}
}
}
public void startMenu() {
running = true;
thread = new Thread(this, "menu");
thread.start();
}
public void stopMenu() {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (running) {
try {
renderMenu();
} catch (IllegalStateException e) {
System.out.println("Handled");
}
updateFrame();
}
}
private void renderMenu() throws IllegalStateException {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 450);
try {
g.drawImage(ImageIO.read(Display.class.getResource("/main_menu.jpg")), 0, 0, 800, 450, null);
if (InputHandler.mouseX >= 50 && InputHandler.mouseX <= 100 && InputHandler.mouseY >= 290 && InputHandler.mouseY <= 325) {
g.drawImage(ImageIO.read(Launcher.class.getResource("/pin.png")), 10, 295, 30, 33, null);
if (InputHandler.MouseButton == 1) {
dispose();
new RunGame();
}
}
if (InputHandler.mouseX >= 50 && InputHandler.mouseX <= 320 && InputHandler.mouseY >= 390 && InputHandler.mouseY <= 425) {
g.drawImage(ImageIO.read(Launcher.class.getResource("/pin.png")), 10, 395, 30, 33, null);
if (InputHandler.MouseButton == 1) {
dispose();
new Controls();
}
}
if (InputHandler.mouseX >= 400 && InputHandler.mouseX <= 490 && InputHandler.mouseY >= 290 && InputHandler.mouseY <= 325) {
g.drawImage(ImageIO.read(Launcher.class.getResource("/pin.png")), 360, 295, 30, 33, null);
if (InputHandler.MouseButton == 1) {
dispose();
new Credits();
}
}
if (InputHandler.mouseX >= 400 && InputHandler.mouseX <= 440 && InputHandler.mouseY >= 390 && InputHandler.mouseY <= 425) {
g.drawImage(ImageIO.read(Launcher.class.getResource("/pin.png")), 360, 395, 30, 33, null);
if (InputHandler.MouseButton == 1) {
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
g.setColor(Color.WHITE);
g.setFont(new Font("Agency FB", 0, 30));
g.drawString("By Lawrence Zhao", 210, 275);
g.setFont(new Font("Agency FB", 0, 40));
g.drawString("Play", 50, 325);
g.drawString("Controls and Options", 50, 425);
g.drawString("Credits", 400, 325);
g.drawString("Exit", 400, 425);
g.dispose();
bs.show();
}
}
控制类:
import hungerGames.Display;
import hungerGames.input.InputHandler;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Controls extends JFrame{
public static final long serialVersionUID = 1L;
protected JPanel window = new JPanel();
private int width = 720;
private int height = 450;
private Rectangle rResolution;
private Choice resolution = new Choice();
public Controls() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
setUndecorated(true);
setSize(new Dimension(width, height));
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
window.setLayout(null);
InputHandler input = new InputHandler();
addKeyListener(input);
addFocusListener(input);
addMouseListener(input);
addMouseMotionListener(input);
renderControls();
drawButtons();
stopMenuThread();
}
private void stopMenuThread() {
Display.getLauncherInstance().stopMenu();
}
private void drawButtons() {
rResolution = new Rectangle(50,100, 100, 25);
resolution.setBounds(rResolution);
resolution.add("640, 400");
resolution.add("800, 600");
resolution.add("1024, 768");
resolution.select(1);
add(resolution);
}
private void renderControls() throws IllegalStateException {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, 720, 450);
try {
g.drawImage(ImageIO.read(Display.class.getResource("/controls.jpg")),0, 0, 720, 450, null);
if (InputHandler.mouseX >= 360 && InputHandler.mouseX <= 400 && InputHandler.mouseY >=270 && InputHandler.mouseY <=305) {
g.drawImage(ImageIO.read(Controls.class.getResource("/pin.png")),360,270, 30, 33, null);
if (InputHandler.MouseButton == 1) {
Display.selection = resolution.getSelectedIndex();
dispose();
new Launcher(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
g.setColor(Color.WHITE);
g.setFont(new Font("Agency FB", 0, 40));
g.drawString("Exit", 400, 300);
g.dispose();
bs.show();
}
}