我正在尝试编写这个屏幕键盘,当用户按下键盘时会做出反应。 我的问题是使用keyListener,我想在JPanel类中编写它,但我一直得到这个编译错误,一些关于缺失的东西" {"在"实现"之后在线: 公共类MyKeyListener实现了KeyListener()
有人可以帮助我理解我做错了什么吗? 这是代码: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
}
}
}
答案 0 :(得分:1)
问题在于:
public class MyKeyListener implements KeyListener(){
应该是
public class MyKeyListener implements KeyListener {
没有括号。