0

我创建了 2 个框架:

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

这里的第一个窗口:

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);
        }
    });

也许问题出在缓冲策略上?

4

0 回答 0