简体   繁体   English

除非最小化或调整JFrame的大小,否则我不能将其称为KeyEvent事件

[英]I can't call an event, a KeyEvent, unless I minimize or resize my JFrame

i have 3 classes inside a class. 我在一个班级里有3个班级。 the main class will call one of the 3 classes, depending on the input of the user, if it is B, R or G. B for Blue class, r for red and g for green. 主类将调用3个类之一,具体取决于用户的输入,如果是B,R或G。B代表蓝色类,r代表红色,g代表绿色。

after i call a class, lets say Blue class, the blue will be added to the container and the class that calls this, a panel class will be removed from the container. 在我调用一个类后,假设是蓝色类,蓝色将被添加到容器中,而调用此类的类将被从容器中移除。

when i press the button "exit", the blue class will be removed and the panel class will be added to the container again. 当我按下“退出”按钮时,蓝色类将被删除,面板类将再次添加到容器中。

my problem is, after i've done this scenario above, i can't call another class again, by pressing B, R or G. i must minimize or resize first my jframe to call a class. 我的问题是,在完成上面的这种情况之后,我无法通过按B,R或G再次调用另一个类。我必须首先最小化或调整其jframe的大小才能调用一个类。 again 再次

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

public class practice implements KeyListener{
JFrame frame = new JFrame("practice");
JLabel label = new JLabel("Press B, R, or G");
JPanel panel = new JPanel();

public class BLUE implements ActionListener{
    JPanel bluePanel = new JPanel();
    JButton exit = new JButton("exit");
    public BLUE(){
        bluePanel.setPreferredSize(new Dimension(300, 300));
        bluePanel.setFocusable(true);
        bluePanel.setVisible(true);
        bluePanel.requestFocus();
        bluePanel.setBackground(Color.blue);
        bluePanel.add(exit);
        exit.addActionListener(this);
        frame.remove(panel);
        frame.add(bluePanel);
        frame.revalidate();
        frame.repaint();
    }   

    public void actionPerformed(ActionEvent e){
        if(e.getSource().equals(exit)){
            frame.remove(bluePanel);
            frame.add(panel);
            frame.revalidate();
            frame.repaint();
        }
    }           
}


public class RED extends JPanel implements ActionListener{
    JPanel redPanel = new JPanel();
    JButton exit = new JButton("exit");
    public RED(){
        redPanel.setPreferredSize(new Dimension(300, 300));
        redPanel.setFocusable(true);
        redPanel.setVisible(true);
        redPanel.requestFocus();
        redPanel.setBackground(Color.red);
        redPanel.add(exit);
        exit.addActionListener(this);
        frame.remove(panel);
        frame.add(redPanel);
        frame.revalidate();
        frame.repaint();
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource().equals(exit)){
            frame.remove(redPanel);
            frame.add(panel);
            frame.revalidate();
            frame.repaint();
        }
    }           
}

public class GREEN extends JPanel implements ActionListener{
    JPanel greenPanel = new JPanel();
    JButton exit = new JButton("exit");
    public GREEN(){
        greenPanel.setPreferredSize(new Dimension(300, 300));
        greenPanel.setFocusable(true);
        greenPanel.setVisible(true);
        greenPanel.requestFocus();
        greenPanel.setBackground(Color.green);
        greenPanel.add(exit);
        exit.addActionListener(this);
        frame.remove(panel);
        frame.add(greenPanel);
        frame.revalidate();
        frame.repaint();
    }   

    public void actionPerformed(ActionEvent e){
        if(e.getSource().equals(exit)){
            frame.remove(greenPanel);
            frame.add(panel);
            frame.revalidate();
            frame.repaint();
        }
    }           

}

public practice(){  
    frame.setVisible(true);
    frame.setPreferredSize(new Dimension(300, 300));
    frame.setDefaultCloseOperation(3);
    frame.setResizable(false);
    frame.pack();
    frame.setLocationRelativeTo(null);

    initUI();
}

public void initUI(){
    frame.addKeyListener(this);
    panel.add(label);
    panel.setBackground(Color.black);
    frame.add(panel);
}

public void keyReleased(KeyEvent e){
}

public void keyTyped(KeyEvent e){   
}

public void keyPressed(KeyEvent e){
    if(e.getKeyCode() == KeyEvent.VK_B){
        BLUE blue = new BLUE();
    }
    if(e.getKeyCode() == KeyEvent.VK_R){
        RED red = new RED();
    }
    if(e.getKeyCode() == KeyEvent.VK_G){
        GREEN green = new GREEN();
    }
}

public static void main(String[] args){
    practice p = new practice();
}
}

I think the problem is that the focus is given to the panel after the call, and the JFrame then can't listen for input. 我认为问题在于调用后将焦点放在面板上,然后JFrame无法监听输入。 You want to requestFocus() from the panel after the call is made. 调用后,您想从面板上请求requestFocus()

Your jframe (where you added the keyListener) lost focus as you request focus on the jpanels which do not have a keyListener. 当您请求将焦点集中在没有keyListener的jpanel上时,您的jframe(添加了keyListener的地方)失去了焦点。 Use requestFocus() on the jframe after every change of jpanels, i cant test it myself atm though so not completely sure . 每次更改jpanels后,请在jframe上使用requestFocus(),但我不能完全确定自己无法在atm上对其进行测试。

Not 100% sure but I think your problem lies with what component is in focus. 不确定100%,但是我认为您的问题在于重点关注哪些组件。 Once you click the exit button the focus shifts to the exit button and even though it's removed later focus is never given back to the original frame. 单击退出按钮后,焦点将移至退出按钮,即使以后将其删除,也永远不会将焦点返回给原始帧。 At the end of your exit button code add: 在退出按钮代码的末尾添加:

frame.requstFocus();

Yup, checked and this works. 是的,已检查,这可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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