为什么KeyListener无法正常工作?

时间:2015-04-11 06:30:21

标签: java swing keylistener keyboard-events keyevent

我是java的新手,想知道我的代码有什么问题。

为了测试keylistener,我按了N,但没有发生任何事情。然后我重新安排了一些方法,但这也没有用。

我的代码如下,欢迎任何输入:)

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Calculator extends MyFrame implements KeyListener {

    private static final long serialVersionUID = 1L;

    //declare variable
    protected JPanel northPanel;
    protected JPanel plusPanel, minusPanel, multiplyPanel, dividePanel,modPanel, equalPanel;
    protected JPanel rowPanel1, rowPanel2, rowPanel3, rowPanel4, southPanel;
    protected JPanel[] numPanel = new JPanel[10];
    protected JTextField textField;
    protected JButton plusButton, minusButton, multiplyButton;
    protected JButton divideButton, modButton, equalButton;
    protected JButton[] numButton = new JButton[10];

    public void setTextField() {
        textField = new JTextField("0.0");
        textField.setBackground(Color.GRAY); 
        textField.setHorizontalAlignment(SwingConstants.RIGHT); 
        textField.setEditable(false); 
    }

    public void setButton() {

        for (int i = 0; i <= 9; i++) {
            numButton[i] = new JButton(i + "");
        }

        plusButton = new JButton("+");
        minusButton = new JButton("-");
        multiplyButton = new JButton("*");
        divideButton = new JButton("/");
        modButton = new JButton("%");
        equalButton = new JButton("=");

    }


    public void setSqualJpanel() {

        for (int i = 0; i <= 9; i++) {
            numPanel[i] = createSquareJpanel(Color.pink, 30, numButton[i]);
        }
        plusPanel = createSquareJpanel(Color.green, 30, plusButton);
        minusPanel = createSquareJpanel(Color.green, 30, minusButton);
        multiplyPanel = createSquareJpanel(Color.green, 30, multiplyButton);
        dividePanel = createSquareJpanel(Color.green, 30, divideButton);
        modPanel = createSquareJpanel(Color.green, 30, modButton);
        equalPanel = createSquareJpanel(Color.blue, 30, equalButton);
    }

    public void addComponents() {
        rowPanel1 = new JPanel(); 
        rowPanel2 = new JPanel();
        rowPanel3 = new JPanel();
        rowPanel4 = new JPanel();
        southPanel = new JPanel (new BorderLayout()); 
        northPanel = new JPanel(new BorderLayout());

        northPanel.add(textField); 

        rowPanel1.add(numPanel[0]); 
        rowPanel1.add(numPanel[1]);
        rowPanel1.add(numPanel[2]);
        rowPanel1.add(numPanel[3]);

        rowPanel2.add(numPanel[4]); 
        rowPanel2.add(numPanel[5]);
        rowPanel2.add(numPanel[6]);
        rowPanel2.add(numPanel[7]);

        rowPanel3.add(numPanel[8]); 
        rowPanel3.add(numPanel[9]); 
        rowPanel3.add(plusPanel);
        rowPanel3.add(minusPanel);

        rowPanel4.add(multiplyPanel); 
        rowPanel4.add(dividePanel);
        rowPanel4.add(modPanel);
        rowPanel4.add(equalPanel);

        southPanel.add(rowPanel2, BorderLayout.NORTH); 
        southPanel.add(rowPanel3, BorderLayout.CENTER);
        southPanel.add(rowPanel4, BorderLayout.SOUTH);

        add(northPanel, BorderLayout.NORTH); 
        add(rowPanel1, BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);

    }
    public void setFrameFeatures (String title) {
        setTitle(title);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int w = getSize().width;
        int h = getSize().height;
        int x = (dim.width - w) / 2;
        int y = (dim.height - h) / 2;
        setLocation(x, y);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setVisible(true); 

    }

    public static void createAndShowGUI() {
        Calculator frame = new Calculator();
        frame.setFrameFeatures("Simple Calculator");
        frame.setTextField();
        frame.setButton();
        frame.setSqualJpanel();
        frame.addComponents();
        frame.pack();   
        frame.addListener();
    }

    private void addListener() {
            addKeyListener(this);

    }

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

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_N){
            JOptionPane.showMessageDialog(null,"You Pressed N");
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:0)

我通常将我的关键监听器添加到jpanel而不是帧。