我的JButton工作不正常

时间:2013-03-24 19:45:37

标签: java swing jframe jbutton actionlistener

好的,所以我在这里得到了这个问题。我正在为我的游戏制作一个启动器,我得到了一切正常工作,除非我试图让Play按钮工作它工作正常并执行我为它设置的命令,除了它没有打开文件我设置它打开按钮点击。 (JAVA代码) 下面是启动器的代码:

 private static final long serialVersionUID = 1L;

 private JPanel window = new JPanel();
 private JButton Play, Options, Update, Multiplayer, Quit;
 private Rectangle rPlay, rOptions, rUpdate, rMultiplayer, rQuit;

 private int width = 240;
 private int height = 320;

public Launcher(){
    try{
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e){
        e.printStackTrace();
    }
    setTitle("Questor Launcher");
    setSize(new Dimension(800, 600));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().add(window);
    setLocationRelativeTo(null);
    setResizable(true);
    setVisible(true);
    window.setLayout(null);

    drawButtons();
}
private void drawButtons(){
    Play = new JButton ("Play");
    rPlay = new Rectangle(650, 500, 80, 40);
    Play.setBounds(rPlay);
    window.add(Play);

    Options = new JButton ("Options");
    rOptions = new Rectangle(550, 500, 80, 40);
    Options.setBounds(rOptions);
    window.add(Options);

    Update = new JButton ("Update");
    rUpdate = new Rectangle(450, 500, 80, 40);
    Update.setBounds(rUpdate);
    window.add(Update);

    Multiplayer = new JButton ("Multiplayer");
    rMultiplayer = new Rectangle(300, 500, 130, 40);
    Multiplayer.setBounds(rMultiplayer);
    window.add(Multiplayer);

    Quit = new JButton ("Quit");
    rQuit = new Rectangle(200, 500, 80, 40);
    Quit.setBounds(rQuit);
    window.add(Quit);

    Play.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent a) {
            System.out.println("Entering Game...");
            dispose();
            new RunGame();
        }
        });
    Options.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent a) {
            System.out.println("Entering Options Menu...");
        }
        });
    Update.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent a) {
            System.out.println("Opening browser to Questor.weebly.com/questor...");
        }
        });
    Multiplayer.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent a) {

        }
        });
    Quit.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent a) {
            System.out.println("Quiting Game...");
            System.exit(0);
        }
        });

}}

继承RunGame文件的代码:

private static Game game = new Game();
public static final boolean DEBUG = false;

@Override
public void init() {
    setLayout(new BorderLayout());
    add(game, BorderLayout.CENTER);
    setMaximumSize(Game.DIMENSIONS);
    setMinimumSize(Game.DIMENSIONS);
    setPreferredSize(Game.DIMENSIONS);
    game.debug = DEBUG;
    game.isApplet = true;
}

@Override
public void start() {
    game.start();
}

@Override
public void stop() {
    game.stop();
}

public static void main(String[] args) {
    game.setMinimumSize(Game.DIMENSIONS);
    game.setMaximumSize(Game.DIMENSIONS);
    game.setPreferredSize(Game.DIMENSIONS);

    game.frame = new JFrame(Game.NAME);

    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLayout(new BorderLayout());

    game.frame.add(game, BorderLayout.CENTER);
    game.frame.pack();

    game.frame.setResizable(false);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);

    game.windowHandler = new WindowHandler(game);
    game.debug = DEBUG;

    game.start();
}

}

最后但并非最不重要的游戏文件代码:

private static final long serialVersionUID = 1L;

public static final int WIDTH = 240;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 3;
public static final String NAME = "Questor";
public static final Dimension DIMENSIONS = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
public static Game game;

public JFrame frame;

private Thread thread;

public boolean running = false;
public int tickCount = 0;

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
private int[] colours = new int[6 * 6 * 6];

private Screen screen;
public InputHandler input;
public WindowHandler windowHandler;
public Level level;
public Player player;

public GameClient socketClient;
public GameServer socketServer;

public boolean debug = true;
public boolean isApplet = false;

public void init() {
     requestFocus();
    game = this;
    int index = 0;
    for (int r = 0; r < 6; r++) {
        for (int g = 0; g < 6; g++) {
            for (int b = 0; b < 6; b++) {
                int rr = (r * 255 / 5);
                int gg = (g * 255 / 5);
                int bb = (b * 255 / 5);

                colours[index++] = rr << 16 | gg << 8 | bb;
            }
        }   
    }

    screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
    input = new InputHandler(this);
    level = new Level("/levels/water_test_level.png");
    player = new PlayerMP(level, 100, 100, input, JOptionPane.showInputDialog(this, "Please enter your Username"),
            null, -1);
    level.addEntity(player);
    if (!isApplet) {
        Packet00Login loginPacket = new Packet00Login(player.getUsername(), player.x, player.y);
        if (socketServer != null) {
            socketServer.addConnection((PlayerMP) player, loginPacket);
        }
        loginPacket.writeData(socketClient);
    }
}

public synchronized void start() {
    running = true;

    thread = new Thread(this, NAME + "_main");
    thread.start();
    if (!isApplet) {
        if (JOptionPane.showConfirmDialog(this, "Do you want to run the server") == 0) {
            socketServer = new GameServer(this);
            socketServer.start();
        }

        socketClient = new GameClient(this, "localhost");
        socketClient.start();
    }
}

public synchronized void stop() {
    running = false;

    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void run() {
    long lastTime = System.nanoTime();
    double nsPerTick = 1000000000D / 60D;

    int ticks = 0;
    int frames = 0;

    long lastTimer = System.currentTimeMillis();
    double delta = 0;

    init();

    while (running) {
        long now = System.nanoTime();
        delta += (now - lastTime) / nsPerTick;
        lastTime = now;
        boolean shouldRender = true;

        while (delta >= 1) {
            ticks++;
            tick();
            delta -= 1;
            shouldRender = true;
        }

        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (shouldRender) {
            frames++;
            render();
        }

        if (System.currentTimeMillis() - lastTimer >= 1000) {
            lastTimer += 1000;
            debug(DebugLevel.INFO, ticks + " ticks, " + frames + " frames");
            frames = 0;
            ticks = 0;
        }
    }
}

public void tick() {
    tickCount++;
    level.tick();
}

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }

    int xOffset = player.x - (screen.width / 2);
    int yOffset = player.y - (screen.height / 2);

    level.renderTiles(screen, xOffset, yOffset);
    level.renderEntities(screen);

    for (int y = 0; y < screen.height; y++) {
        for (int x = 0; x < screen.width; x++) {
            int colourCode = screen.pixels[x + y * screen.width];
            if (colourCode < 255)
                pixels[x + y * WIDTH] = colours[colourCode];
        }
    }


    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    g.dispose();
    bs.show();
}

public static long fact(int n) {
    if (n <= 1) {
        return 1;
    } else {
        return n * fact(n - 1);
    }
}

public void debug(DebugLevel level, String msg) {
    switch (level) {
    default:
    case INFO:
        if (debug) {
            System.out.println("[" + NAME + "] " + msg);
        }
        break;
    case WARNING:
        System.out.println("[" + NAME + "] [WARNING] " + msg);
        break;
    case SEVERE:
        System.out.println("[" + NAME + "] [SEVERE]" + msg);
        this.stop();
        break;
    }
}

public static enum DebugLevel {
    INFO, WARNING, SEVERE;
}
public static void main(String[]argo){
    new Launcher();
}

}

0 个答案:

没有答案