简体   繁体   English

KeyEvent不会更改JPanels的背景颜色

[英]KeyEvent won't change background color of JPanels

I want the KeyEvent to change the background color of my JPanels. 我希望KeyEvent更改我的JPanels的背景颜色。 Nothing happens when I press anything on the keyboard. 当我按键盘上的任何键时,什么都没有发生。 One of my applications specifications is that I need a 'Customized component extended from JPanel.' 我的应用程序规范之一是我需要一个“从JPanel扩展的自定义组件”。 which is why I have another class for my graphics panel. 这就是为什么我的图形面板还有另一个类的原因。

My problem is when G is pressed nothing happens but my center panel should turn green... 我的问题是当按下G键时什么也没发生,但是我的中央面板应该变成绿色。

Here is code for part of my application. 这是我的应用程序一部分的代码。

public class Maths extends JFrame implements KeyListener
{    
private JPanel pNorth = new JPanel();
private JPanel pSouth = new JPanel();
private JPanel pCenter = new JPanel();
private JPanel pEast = new JPanel();
private JPanel pWest = new JPanel();
private File file;
private JPanel pDraw = new GraphicsPanel();

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

}

public Maths()
{
    mainFrame = new JFrame();

    mainFrame.setTitle("Maths Test Game");
    mainFrame.setLayout(new BorderLayout());
    mainFrame.setSize(1200, 800);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    mainFrame.add(pNorth, BorderLayout.NORTH);
    mainFrame.add(pSouth, BorderLayout.SOUTH);
    mainFrame.add(pCenter, BorderLayout.CENTER);
    mainFrame.add(pEast, BorderLayout.EAST);
    mainFrame.add(pWest, BorderLayout.WEST);

    pNorth.setLayout(new FlowLayout());
    pSouth.setLayout(new FlowLayout());
    pCenter.setLayout(new FlowLayout());
    pEast.setLayout(new FlowLayout());
    pWest.setLayout(new FlowLayout());

    addKeyListener(this);
    setFocusable(true);

    mainFrame.setVisible(true);
}

class GraphicsPanel extends JPanel 
{
    GraphicsPanel() 
    {
        // set a preferred size for the custom panel.
        setPreferredSize(new Dimension(250, 300));
    }
    @Override
    public void paint(Graphics g) 
    {
        super.paint(g);
        // set blue color for drawing
        g.setColor(Color.blue);
        // face
        g.drawOval(90, 70, 80, 80);
        // eyes
        g.drawOval(110, 95, 5, 5);
        g.drawOval(145, 95, 5, 5);
        // nose
        g.drawLine(130, 95, 130, 115);
        // mouth
        g.drawArc(113, 115, 35, 20, 0, -180);

    }
}

@Override
public void keyPressed(KeyEvent e) 
{
    if(e.getKeyCode() == KeyEvent.VK_G)
    {
        pCenter.setBackground(Color.green);
    }
    repaint();
}

@Override
public void keyReleased(KeyEvent e) 
{

}

@Override
public void keyTyped(KeyEvent e) 
{

}

}

There are a multitude of reasons this is probably not working, generally, you don't want to attach KeyListener s to top level containers like JFrame , as they is simply to many things that can get in the way and prevent the frame from raising key events. 有很多原因可能导致此方法不起作用,通常,您不想将KeyListener附加到JFrame类的顶级容器上,因为它们只是阻碍了很多事情的发生,并阻止了框架提升键事件。

Instead, use the key bindings API. 而是使用键绑定API。 See How to Use Key Bindings for more details 有关更多详细信息,请参见如何使用键绑定

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

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