简体   繁体   English

扩展JPanel类中的KeyListener

[英]KeyListener in extends JPanel class

I am trying to write this onscreen keyboard that will react when the user press on the keyboard. 我正在尝试编写此屏幕键盘,当用户在键盘上按该键盘时会做出反应。 my problem is with the keyListener, I want to write it inside the JPanel class but i keep getting this compile error, somethig about missing "{" after the word "implements" in the line: public class MyKeyListener implements KeyListener() 我的问题是keyListener,我想在JPanel类中编写它,但我不断收到此编译错误,这是有关该行中的“ implements”后缺少“ {”的问题:public class MyKeyListener实现了KeyListener()

can someone help me understand what i am doing wrong? 有人可以帮助我了解我在做什么错吗? this is the code: 这是代码:

package Q2;
import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.util.EventListener;

public class MainPanel extends JPanel{

    private JButton[][] button;
    private JPanel[] panel;                                                     //Array of panels for each buttons line
    private JPanel parent;
    private static final String[][] key = {
        {"`","1","2","3","4","5","6","7","8","9","0","-","+","Backspace"},
        {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]"},
        {"Caps","A","S","D","F","G","H","J","K","L",";","'","\\","Enter"},
        {"Shif","Z","X","C","V","B","N","M",",",".","?","/"},
        {"                                                          ",",","<","v",">"}};

    //Constructor for main Panel
    public MainPanel(){
        super();
        setLayout(new BorderLayout());
        TextField textField = new TextField(20);
        Font font1 = new Font("david", Font.BOLD, 22);

        textField.setFont(font1);
        add(textField,BorderLayout.CENTER);

        //initialize the parent panel and array of 5 panels and the buttons array
        parent = new JPanel();
        parent.setLayout(new GridLayout(0,1));
        panel = new JPanel[5];
        button = new JButton[20][20];

        for (int row = 0; row<key.length; row++){
            panel[row] = new JPanel();
            for (int column = 0; column<key[row].length; column++){
                button[row][column] = new JButton(key[row][column]);
                button[row][column].setFont(new Font("Ariel",Font.PLAIN, 22));
                button[row][column].setMargin(new Insets(10, 20, 10, 20));
                button[row][column].putClientProperty("row", row);
                button[row][column].putClientProperty("column", column);
                button[row][column].putClientProperty("key", key[row][column]);
                panel[row].add(button[row][column]);
            }
            parent.add(panel[row]);
        }
        add(parent,BorderLayout.SOUTH);
    }

    public class MyKeyListener implements KeyListener(){


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

    }

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

    }

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

    }
    }
}

The problem lies here: 问题出在这里:

public class MyKeyListener implements KeyListener(){

should be 应该

public class MyKeyListener implements KeyListener {

without the parentheses. 没有括号。

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

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