Java Canvas错误焦点或动作侦听器在第二个窗口中

时间:2013-04-16 12:13:15

标签: java focus awt java-canvas bufferstrategy

我创建了两个框架:

在1st中 - 我制作了一个按钮,将我移动到由Canvas创建的类创建的第2帧,并且从第1帧移动到第2帧后我失去了动作监听器或焦点

第一个窗口:

public class Credits extends JFrame {

public Credits() {
    super("Credits");
    setSize(300, 200);
    setLayout(new BorderLayout());

    JButton ok = new JButton("GAME");


    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(false);
    this.add(ok);
    setVisible(true);


    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            dispose();
            new WojnaSwiatow().game();

        }
    });



}

public static void main(String []args ) {
    SwingUtilities.invokeLater(new Runnable(){

        @Override
        public void run()
        {
           new Credits();
        }

    });
}
}

第二帧:

public class WojnaSwiatow extends Canvas implements Stage, KeyListener {/**
 * @param args the command line arguments
 */

    createBufferStrategy(2);
    strategia = getBufferStrategy();
    requestFocus();

    requestFocusInWindow(true);
    addKeyListener(this);
    okno.addKeyListener(this);
    okno.pack();
    okno.setVisible(true);



public long usedTime;
public BufferStrategy strategia;
private SpriteCache spriteCache;
private ArrayList actors;
private Player player;


public WojnaSwiatow(){

    spriteCache = new SpriteCache();
    JFrame okno = new JFrame("..:Wojna Swiatow :..");
    JPanel panel = (JPanel)okno.getContentPane();
    setBounds(0,0,Stage.SZEROKOSC,Stage.WYSOKOSC);

    panel.setPreferredSize(new Dimension(Stage.SZEROKOSC,Stage.WYSOKOSC));
    panel.setLayout(null);
    panel.add(this);

    okno.setBounds(0,0,Stage.SZEROKOSC,Stage.WYSOKOSC);
    okno.setVisible(true);
    okno.setResizable(false);
    okno.setLocationRelativeTo(null);


    okno.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    });
可能问题在于缓冲策略?

0 个答案:

没有答案