简体   繁体   中英

CardLayout Input Issue Java

For the game I'm making I'm trying to make a simple title screen with a play button that begins the game once it's pressed. I've heard that a card layout is best suited for this, but now that I implemented it for some reason my input into the game is being ignored. Here's a simplified version of the problem, and thanks for the help.

MainFrame.java

//package panels.examples;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

   public class MainFrame extends JFrame implements ActionListener
      {
         JPanel headerPanel;
         JPanel bodyPanel;
         JPanel panel1,panel2;
         JButton button1,button2;
         Container con;
         CardLayout clayout;
      public MainFrame() 
   {
    //con=getContentPane();
    clayout=new CardLayout();
    headerPanel=new JPanel();
    bodyPanel=new JPanel(clayout);

    button1=new JButton("button1");
    button2=new JButton("button2");


    //add three buttons to headerPanel
    headerPanel.add(button1);
    headerPanel.add(button2);


    button1.addActionListener(this);
    button2.addActionListener(this);


    panel1=new JPanel();
    panel1.add(new JLabel("Panel1"));
    panel1.setBackground(Color.pink);
    panel2=new PrizePanel();
    panel2.add(new JLabel("Panel2"));
    panel2.setBackground(Color.gray);

    //add above three panels to bodyPanel
    bodyPanel.add(panel1,"one");    
    bodyPanel.add(panel2,"two");    


    setLayout(new BorderLayout());
    setSize(800,400);
    add(headerPanel,BorderLayout.NORTH);
    add(bodyPanel,BorderLayout.CENTER);
//  headerPanel.setBounds(0,0,600,100);
    bodyPanel.setBounds(0,100, 600, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);



}

      public static void main(String args[])
      {
          new MainFrame();
      }

      public void actionPerformed(ActionEvent e) {

          if(e.getSource()==button1)
          {
           clayout.show(bodyPanel, "one");
           }

          else if(e.getSource()==button2)
          {
              clayout.show(bodyPanel, "two");
          }

      }

}

PrizePanel.java

//INCLUDE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.IOException;

//CLASS
public class PrizePanel extends JPanel {

   //DECLARATION   
   private static final int FRAMEX = 800;
   private static final int FRAMEY = 400;
   private static final Color BACKGROUND = new Color(204, 204, 204);
   private BufferedImage myImage;
   private Graphics myBuffer;

   private Player pd;


   private Timer t; 
   int points = 0;





    //CONSTRUCTOR  
   public PrizePanel()
   {
      myImage =  new BufferedImage(FRAMEX, FRAMEY, BufferedImage.TYPE_INT_RGB);
      myBuffer = myImage.getGraphics();
      myBuffer.setColor(BACKGROUND);
      myBuffer.fillRect(0, 0, FRAMEX,FRAMEY);

      int xPos = (int)(Math.random()*(FRAMEX-100) + 50);
      int yPos = (int)(Math.random()*(FRAMEY-100)+ 50);
      pd = new Player(150,150,50);


      t = new Timer((17), new Listener());
      t.start();

      addKeyListener(new KeyHandler());
      setFocusable(true);
   }



   //PAINT
   public void paintComponent(Graphics g)
   {
      g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);

   }

   //LISTENER
   private class Listener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         myBuffer.setColor(BACKGROUND);     
         myBuffer.fillRect(0,0,FRAMEX,FRAMEY);   

         pd.move();
          pd.draw(myBuffer);

         myBuffer.setColor(Color.BLACK);
         repaint();
      }
   }   



  //MOVE
   private class KeyHandler extends KeyAdapter
   {
   //Ground
      public void keyPressed(KeyEvent e){
         if(e.getKeyCode() == KeyEvent.VK_LEFT){

            pd.setdx(-2);
         }
         if(e.getKeyCode() == KeyEvent.VK_RIGHT){

            pd.setdx(2);
         }
         if(e.getKeyCode() == KeyEvent.VK_UP){
            pd.setY(pd.getY()-1);
            pd.setdy(-5.5);
         }



   }
}
}

The problems not the CardLayout , the problem is the use of KeyListener

Basically what's happening is when you the layout is switched, you component is not being given focus, meaning your KeyListener can't respond to key input.

The simplest solution is to use Key Bindings instead, as they allow you to define the focus level that key events will be raised.

Add one line - and panel2 keys will work:

   //LISTENER
   private class Listener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         myBuffer.setColor(BACKGROUND);     
         myBuffer.fillRect(0,0,FRAMEX,FRAMEY);   

         pd.move();
         pd.draw(myBuffer);

         myBuffer.setColor(Color.BLACK);
         repaint();
         requestFocusInWindow();//---->> New line
      }
   } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM