启动器不会打开我的JFrame

时间:2014-04-01 18:47:09

标签: java class methods window jframe

我正在尝试使用简单的启动器打开一个涉及JFrame的类:

public class Launcher {

public static void main(String[] args) {

    new StartScreen();

   }

}

这个启动器用于启动课程:

import javax.swing.JFrame;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.image.BufferedImage;

public class StartScreen extends JFrame {
private static JFrame frame;
GameKeyboard GK;

boolean gamePlay = false;
boolean gameQuit = false;
boolean gameTwoPlayer = false;
String option;

//set dimension of window and buttons
public final int screenWidth = 800; // Width of window
public final int screenHeight = screenWidth / 12 * 9; // Height of window

private static Graphics gr;

//store images
private static Image background;
private static Image play;
private static Image twoPlayer;
private static Image quit;
private static Image playSelected;
private static Image twoPlayerSelected;
private static Image quitSelected;

public void StartScreen() {
    frame = new JFrame();
    setSize(screenWidth, screenHeight);
    frame.add(this, BorderLayout.CENTER);
    //         frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setTitle("Space Wars Menu");
    frame.setLocationRelativeTo(null);
    getContentPane().setLayout(null);

    BufferedImage canvas=new BufferedImage(920,720,BufferedImage.TYPE_INT_ARGB);

    gr=canvas.getGraphics();

    JLabel label=new JLabel(new ImageIcon(canvas));
    frame.add(label);

    MenuKeyboard.initialise();

    //load images
    background = GameImage.loadImage("Images//background.jpg");
    play = GameImage.loadImage("Images//play.png");
    playSelected = GameImage.loadImage("Images//playSelected.png");
    twoPlayer = GameImage.loadImage("Images//twoPlayer.png");
    twoPlayerSelected = GameImage.loadImage("Images//twoPlayerSelected.png");
    quit = GameImage.loadImage("Images//quit.png");
    quitSelected = GameImage.loadImage("Images//quit.png");


    //draw images
    gr.drawImage(background, 0, 0, null);
    gr.drawImage(playSelected, 160, -50, null);
    gr.drawImage(twoPlayer, 160, 150, null);
    gr.drawImage(quit, 160, 250, null);

    int specialKey = MenuKeyboard.getSpecialKey();
    while(gamePlay == false)
    {

        if (MenuKeyboard.getSpecialKey() == 40) //if down pressed
        {
            gr.drawImage(twoPlayerSelected, 160, 150, null);
            gr.drawImage(play, 160, -50, null);
          }

      }

   }
}

该类尚未完成,但我只是测试它是否会打开一个窗口并加载所有图像,但是没有窗口甚至被打开..尽管这些类实际上是链接的。有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

你的while循环连续运行,它正在踩踏Swing事件线程,有效地冻结它并防止Swing绘制任何东西。解决方案:摆脱循环。

而是使用Key Bindings来捕获特定的按键。如果您需要游戏循环,请使用Swing Timer。

教程:


修改

您的StartScreen类没有构造函数。摆脱伪构造函数中的 void 返回类型。

其他问题:

  • 您不应使用null布局。
  • 您的大多数变量不应该是静态的。

答案 1 :(得分:1)

您的构造函数实际上是一个带有构造函数名称的方法。

您应该从此处切换:

public void StartScreen() {
    // Code
}

到此:

public StartScreen() {
    // Code
}