2014-01-31 75 views
1

對於我正在製作的遊戲,我試圖用按鈕開始遊戲的簡單標題屏幕。我聽說卡片佈局最適合這種情況,但現在我因爲某種原因實施了它,因此我的輸入被忽略了。以下是該問題的簡化版本,並感謝您的幫助。CardLayout輸入問題Java

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



    } 
} 
} 

回答

1

的問題不是CardLayout,問題是採用KeyListener

基本上發生的事情是當你的佈局切換後,您的組件未被賦予焦點,這意味着您的KeyListener無法響應按鍵輸入。

最簡單的解決方案是使用Key Bindings代替,因爲它們允許你定義的焦點一級的關鍵事件將得到提升。

+0

嘿感謝,沒想到得到的迴應如此之快,這是現在所有工作。 – user3050810

+0

希望它適合您;) – MadProgrammer

1

添加一條線 - 和panel2鍵將工作:

//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 
     } 
    } 
+0

感謝您的回覆。你也是對的,但MadProgrammer是第一個,所以我選擇了他的答案。 – user3050810

+0

完全沒有問題:)現在你有2種方法如何做到這一點。 –