按钮新游戏不工作Java

时间:2013-05-31 18:07:31

标签: java swing button frame actionlistener

嗨我正在为我的学习做一个项目,并且我是新的java。我需要做一个游戏 - 打砖块。我想做的是一个带有2个按钮“New game”和“Quit”的游戏手册。有人从stackoverflow.com重建我的菜单代码,但它仍然无法工作:(点击“新游戏”后我得到空白帧:(。可能是因为Gra构造函数。Gra的构造函数需要如何看起来像是使用我的菜单代码。 以下是代码(我将粘贴所有Arkanoid类代码和Gra类的最重要部分):

Arkanoid类:

package arkanoid;

import javax.swing.*;
import java.awt.event.*;

public class Arkanoid extends JFrame 
{

private static final long serialVersionUID = 5647459635833443178L;

public Arkanoid() {
    super("Arkanoid");
    setSize(500,400);
    setTitle("Arkanoid BETA");
    setLocationRelativeTo(null);
    setResizable(false);

    final JPanel panel = new JPanel();
    setContentPane(panel);

    panel.add(new JButton(new AbstractAction("New game") {
        public void actionPerformed (ActionEvent e) {     
            panel.removeAll();
            panel.add(new Gra()); //constructor of Gra need to return some Panel i think but i dont know how to do that
            panel.revalidate();
            panel.repaint();
        }
    }));

   panel.add(new JButton(new AbstractAction("Quit") {
        public void actionPerformed (ActionEvent e) {     
            Arkanoid.this.setVisible(false);
        }
    }));
}
public static void main(String[] args) 
{
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          Arkanoid frame = new Arkanoid();
          frame.setVisible(true);
        }
    });
}
}

Gra class:

package arkanoid;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Gra extends JPanel
{
/*some not important variables*/

Pilka pilka;
Paletka paletka;
Cegla cegla_tab[];
Serce serce_tab[];

Timer timer;

public Gra() 
{
    addKeyListener(new TAdapter());
    setFocusable(true);

    cegla_tab = new Cegla[liczba_cegiel];
    serce_tab = new Serce[max_zycia];
    //setDoubleBuffered(true);
    timer = new Timer();
    timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
}

public void addNotify() 
{
    super.addNotify();
    start_gry();
}
public void start_gry() 
{
    pilka = new Pilka();
    paletka = new Paletka();
    /* painting a bricks and lifes for game start */

}
public void koniec_gry()  //end of a game
{
    czas_konca = System.currentTimeMillis();

    gra = 3;
    timer.cancel(); 

}
public void paint(Graphics g) 
{
    super.paint(g);
    //repaiting ball positions, bricks and paddle

    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}
private class TAdapter extends KeyAdapter 
{
    public void keyReleased(KeyEvent e) 
    {
        paletka.klawisz_puszczony(e);
    }
    public void keyPressed(KeyEvent e) 
    {
        paletka.klawisz_wcisniety(e);
    }
}
class ScheduleTask extends TimerTask 
{
    public void run() 
    {
        if(paletka.czy_pausa()==false)
        {
            pilka.przesun();
            paletka.przesun();
            czy_zderzenie();
            repaint();
        }
    }
}

public void czy_zderzenie() 
{
  //checking a collisions with bricks
}
}

1 个答案:

答案 0 :(得分:2)

您需要覆盖paintComponent,而不是绘制。

另外,Gra的构造函数确实返回了JPanel。构造函数将返回您正在构造的对象,在本例中为Gra,它也是一个JPanel。这是工作中的多态性。

哦,根据上面的评论,你真的不应该处理图形对象。