gpt4 book ai didi

java - 扩展 JPanel 类中的 KeyListener

转载 作者:行者123 更新时间:2023-12-01 13:00:31 25 4
gpt4 key购买 nike

我正在尝试编写这个屏幕键盘,当用户按下键盘时它将使用react。我的问题是 keyListener,我想把它写在 JPanel 类中,但我不断收到这个编译错误,关于行中“implements”一词后面缺少“{”的问题:公共(public)类 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

}
}
}

最佳答案

问题出在这里:

public class MyKeyListener implements KeyListener(){

应该是

public class MyKeyListener implements KeyListener {

不带括号。

关于java - 扩展 JPanel 类中的 KeyListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23548647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com