java - 在 CardLayout 中切换后 JPanel 不会聚焦

标签 java swing jpanel cardlayout

我正在创建一个俄罗斯方 block 克隆作为个人项目。为了在菜单之间切换,我使用了 CardLayout,但我遇到了一个问题。当我从一个菜单切换到另一个菜单时,焦点不会转移到另一个 JPanel。我该如何解决这个问题?

这是我的 JFrame:

package com.cgp.tetris;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TetrisFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    TetrisMenu tm = new TetrisMenu();
    static SinglePlayerMenu spm = new SinglePlayerMenu();
    public static CardLayout cl = new CardLayout();
    static JPanel parentComponent = new JPanel(cl);

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

    public TetrisFrame() {
        setTitle("Tetris");
        setSize(new Dimension(646, 604));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((d.width / 2) - 323, (d.height / 2) - 302);
        parentComponent.add(tm, "tm");
        parentComponent.add(spm, "spm");
        cl.show(parentComponent, "tm");
        add(parentComponent);
    }

    public static void replace() {
        cl.show(parentComponent, "spm");
        spm.startClip();
    }
}

这是我的第一个 JPanel/菜单:

package com.cgp.tetris;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class TetrisMenu extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private Thread thread;
    private BufferedImage titletop, titlebottom, selector;
    private boolean left = true, right = false;
    private AudioInputStream themestream;
    private Clip clip;

    public TetrisMenu() {
        super();
    }

    public void run() {
        requestFocus(true);
        loadImages();
        sound();
        bind();
        while (true) {
            repaint();
        }
    }

    private void sound() {
        File theme = new File("res/theme.wav");
        try {
            themestream = AudioSystem.getAudioInputStream(theme);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        DataLine.Info info = new DataLine.Info(Clip.class, themestream.getFormat());
        clip = null;
        try {
            clip = (Clip) AudioSystem.getLine(info);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        try {
            clip.open(themestream);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        clip.start();
    }

    private void bind() {
        InputMap im = getInputMap();
        ActionMap am = getActionMap();

        im.put(KeyStroke.getKeyStroke("LEFT"), "left");
        am.put("left", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                left = true;
                right = false;
            }
        });

        im.put(KeyStroke.getKeyStroke("RIGHT"), "right");
        am.put("right", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                System.out.println("2");
                right = true;
                left = false;
            }
        });

        im.put(KeyStroke.getKeyStroke("SPACE"), "space");
        am.put("space", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (left) {
                    TetrisFrame.replace();
                    clip.stop();
                    thread.stop();a
                }
            }
        });
    }

    private void loadImages() {
        try {
            titletop = ImageIO.read(new File("res/tetrispic.png"));
            titlebottom = ImageIO.read(new File("res/titlebottom.png"));
            selector = ImageIO.read(new File("res/selector.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addNotify() {
        super.addNotify();
        thread = new Thread(this);
        thread.start();
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.drawImage(titletop, 0, 0, 640, 440, null);
        g.drawImage(titlebottom, 0, 440, 640, 136, null);
        if (left) {
            g.drawImage(selector, 36, 452, 16, 24, null);
        } else if (right) {
            g.drawImage(selector, 356, 452, 16, 24, null);
        }


    }
}

这是我的第二个 JPanel/菜单:

package com.cgp.tetris;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class SinglePlayerMenu extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private Thread thread;
    private AudioInputStream themestream;
    private Clip clip;
    private BufferedImage base, bi1, bi2, bi3, bi4, bi1s, bi2s, bi3s, bi4s;
    private boolean typeselected = false, b1 = true, b2 = false, b3 = true, b4 = false, b5 = false, b6 = false;

    public SinglePlayerMenu() {
        super();
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.drawImage(base, 0, 0, 640, 576, null);

        if (b1) {
            g.drawImage(bi1s, 80, 156, 240, 40, null);
        } else if (!b1) {
            g.drawImage(bi1, 80, 156, 240, 40, null);
        }
        if (b2) {
            g.drawImage(bi2s, 324, 156, 236, 40, null);
        } else if (!b2) {
            g.drawImage(bi2, 324, 156, 236, 40, null);
        }
    }

    public void run() {
        sound();
        loadImages();
        bind();
        while (true) {
            repaint();
        }
    }

    private void bind() {
        InputMap im = getInputMap();
        ActionMap am = getActionMap();

        im.put(KeyStroke.getKeyStroke("LEFT"), "left");
        am.put("left", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    b1 = true;
                    b2 = false;
                }
            }
        });

        im.put(KeyStroke.getKeyStroke("RIGHT"), "right");
        am.put("right", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    b2 = true;
                    b1 = false;
                }
            }
        });
    }

    private void loadImages() {
        try {
            base = ImageIO.read(new File("res/base.png"));
            bi1 = ImageIO.read(new File("res/1.png"));
            bi2 = ImageIO.read(new File("res/2.png"));
            bi3 = ImageIO.read(new File("res/3.png"));
            bi4 = ImageIO.read(new File("res/4.png"));
            bi1s = ImageIO.read(new File("res/1s.png"));
            bi2s = ImageIO.read(new File("res/2s.png"));
            bi3s = ImageIO.read(new File("res/3s.png"));
            bi4s = ImageIO.read(new File("res/4s.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sound() {
        File theme = new File("res/playtheme.wav");
        try {
            themestream = AudioSystem.getAudioInputStream(theme);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        DataLine.Info info = new DataLine.Info(Clip.class, themestream.getFormat());
        clip = null;
        try {
            clip = (Clip) AudioSystem.getLine(info);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        try {
            clip.open(themestream);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addNotify() {
        super.addNotify();
        thread = new Thread(this);
        thread.start();
    }

    public void startClip() {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
}

提前致谢!

最佳答案

您是否尝试过给感兴趣的组件一个 ComponentListener,并在其 componentShown(...) 中通过 requestFocusInWindow() 请求焦点?像这样的东西:

  myComponent.addComponentListener(new ComponentAdapter() {

     @Override
     public void componentShown(ComponentEvent cEvt) {
        Component src = (Component) cEvt.getSource();
        src.requestFocusInWindow();
     }

  });

当然,组件(在您的情况下是 JPanel)需要通过

将其可聚焦属性设置为 true
myComponent.setFocusable(true);

例如:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class CardLayoutFocus extends JPanel {
   private static final int PREF_W = 300;
   private static final int PREF_H = 150;
   private static final int COUNT = 4;
   private CardLayout cardlayout = new CardLayout();
   private JPanel cardHolder = new JPanel(cardlayout );

   public CardLayoutFocus() {
      setLayout(new BorderLayout());
      add(cardHolder, BorderLayout.CENTER);
      for (int i = 0; i < COUNT; i++) {
         String labelString = "Card " + i;
         cardHolder.add(createCardLabel(labelString), labelString);
      }

      int timerDelay = 1000;
      new Timer(timerDelay , new ActionListener() {
         public void actionPerformed(ActionEvent arg0) {
            cardlayout.next(cardHolder);
         }
      }).start();
   }

   private JLabel createCardLabel(String labelString) {
      final JLabel label = new JLabel(labelString, SwingConstants.CENTER);
      label.setName(labelString);
      label.setOpaque(true);
      label.setFocusable(true);
      label.addFocusListener(new FocusListener() {

         @Override
         public void focusLost(FocusEvent arg0) {
            label.setBackground(null);
         }

         @Override
         public void focusGained(FocusEvent arg0) {
            label.setBackground(Color.pink);
         }
      });
      label.addComponentListener(new ComponentAdapter() {

         @Override
         public void componentShown(ComponentEvent cEvt) {
            Component src = (Component) cEvt.getSource();
            src.requestFocusInWindow();
         }

      });
      return label;
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      CardLayoutFocus mainPanel = new CardLayoutFocus();

      JFrame frame = new JFrame("Pink if has the focus");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

关于java - 在 CardLayout 中切换后 JPanel 不会聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8283195/

相关文章:

java - AbstractUIPlugin 导致 NoClassDefFoundError

java - 面向对象的方法结构 - 什么是允许的?

java - 为我的所有 Jlabel JTextArea 等设置通用字体大小

java - 在 Android 上,为什么 View.OnClickListener 实例可以像调用它们自己的方法一样调用它们父类的方法?

java - 使用 Nashorn 查找 JMX MBean

java - 如何水平居中JSpinner

java - 如何使用两个Swing Worker实例?

Java 在 Eclipse 中遇到未知错误 - 不会绘制对象

java - 传递绘制的图形

java - Netbeans - 将 JPanel 添加到 JFrame - 问题