我的代码中有一个cardlayout错误,指出我有点击鼠标的问题

时间:2018-06-06 02:30:52

标签: java swing cardlayout

我的卡布局有问题。它认为(在GameWindow $ 2.mouseClicked(GameWindow.java:80))有一个错误。我的卡片布局看起来像这样

                ((CardLayout) getContentPane().getLayout()).show(getContentPane(), "game");

有人可以帮我解决问题。我有两个包含游戏的帧,另一个包含用于获得游戏阶段的按钮。有人可以帮我做一件事。 卡片布局的布局为空

// Import necessary GUI classes
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JLabel;
import javax.swing.Timer;

// "Extend" the JFrame class so we can customize it but still keep all its features
public class GameWindow extends JFrame implements ActionListener, KeyListener {

    // Serial version UIDs are used to differentiate between different object
    // versions--don't worry about it!
    private static final long serialVersionUID = 1L;

    JLabel Player = new JLabel();
    int playerSpeed = 1;
    int FPS = 30;
    // The keys set holds the keys being pressed
    private final Set<Integer> keys = new HashSet<>();

    // This main method runs when the program is executed
    public static void main(String[] args) {

        // This method runs a new "runnable" program after a brief pause that allows the
        // main program to exit
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                // The try/catch block prevents errors from crashing the program
                try {
                    GameWindow window = new GameWindow(); // Create and setup the main game window
                    window.setVisible(true); // show the new window
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Constructor: runs when a GameWindow object is created (instantiated)
     */
    public GameWindow() {

        // Run the parent class constructor
        super();
        // Allow the panel to get focus
        setFocusable(true);
        // Don't let keys change the focus
        setFocusTraversalKeysEnabled(false);

        setBounds(100, 100, 250, 360); // the window will appear at (100, 100) and 250w by 260h
        setTitle("Banana Jumper 1.0"); // the window will have this title
        setResizable(false); // the window can't be resized
        setLocationRelativeTo(null); // the window will move to the centre of the screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when the window is closed, the program will exit
        getContentPane().setLayout(new CardLayout()); // the window can swap between "stages" added as JPanels

        // Make a new screen with a button and put it in the window
        JPanel startStage = new JPanel(); // Create a new JPanel and add it to the card layout
        startStage.setSize(getWidth(), getHeight()); // make the new JPanel fit the window
        startStage.setBackground(Color.BLUE); // set the JPanel background to blue
        JButton playButton = new JButton("Play"); // Add a button to the panel
        playButton.addMouseListener(new MouseAdapter() { // Set the button to switch to the game stage
            @Override
            public void mouseClicked(MouseEvent arg0) {
                ((CardLayout) getContentPane().getLayout()).show(getContentPane(), "game");
                setLayout(null);
            }
        });
        startStage.add(playButton); // add the button to the stage
        add(startStage, "start"); // add the stage to the window


        // Create a second JPanel with band add it to the card layout
        JPanel gameStage = new JPanel();
        gameStage.setSize(getWidth(), getHeight());
        gameStage.setVisible(true);
        gameStage.setBackground(Color.RED);
        // Add a button to the panel
        JButton mainButton = new JButton("Back to Main Menu");
        // Set the button to switch to the start stage
        mainButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                ((CardLayout) getContentPane().getLayout()).show(getContentPane(), "start");
            }
        });
        gameStage.add(mainButton);
        // Put the stages in the window
        add(startStage, "start");
        add(gameStage, "game"); // put the stage in the window


        // Setup the movable box
         Player.setBounds(10, 10, 10, 10);
         Player.setVisible(true);
         Player.setBackground(Color.BLUE);
         // Opaque makes the background visible
         Player.setOpaque(true);

         // Setup the key listener
         addKeyListener(this);
         // Null layout allows moving objects!!!
         setLayout(null);
         add(Player);

         // Set the timer
         Timer tm = new Timer(1000 / FPS, this);
         tm.start();

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
     // Move up if W is pressed
     if (keys.contains(KeyEvent.VK_W)) {
      Player.setLocation(Player.getX(), Player.getY() - playerSpeed);
     }
     // Move right if D is pressed
     if (keys.contains(KeyEvent.VK_D)) {
      Player.setLocation(Player.getX() + playerSpeed, Player.getY());
     }
     // Move down if S is pressed
     if (keys.contains(KeyEvent.VK_S)) {
      Player.setLocation(Player.getX(), Player.getY() + playerSpeed);
     }
     // Move left if A is pressed
     if (keys.contains(KeyEvent.VK_A)) {
      Player.setLocation(Player.getX() - playerSpeed, Player.getY());
     }
    }

    @Override
    public void keyPressed(KeyEvent e) {
     // Add the key to the list
     // of pressed keys
     if (!keys.contains(e.getKeyCode())) {
      keys.add(e.getKeyCode());
     }
    }

    @Override
    public void keyReleased(KeyEvent e) {
     // Remove the key from the
     // list of pressed keys
     keys.remove((Integer) e.getKeyCode());
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }


}

1 个答案:

答案 0 :(得分:0)

您已将布局设置为null - setLayout(null);,这相当于说getContentPane().setLayout(null)

观测...

  • 避免像startStage.setSize(getWidth(), getHeight()); // make the new JPanel fit the window这样的事情 - 让布局管理员做它的工作
  • startStage.setVisible(true); // show the JPanel毫无意义,因为默认情况下可以看到Swing组件
  • 避免从JFrame延伸。有很多原因(你的问题是其中的一个亮点),但你实际上并没有在课堂上添加新的/可重复使用的功能,而是将你的自我锁定在一个用例中。最好从JPanel开始,然后将其添加到您想要的容器中。
  • 请勿使用KeyListener,而是使用Key Bindings API。它将解决与KeyListener,可靠
  • 相关的所有问题
  • 我也会避免使用&#34;组件&#34; (如JLabel)作为游戏实体。根据自定义绘画路线,您将获得更好的运气和更大的灵活性