gpt4 book ai didi

java - java中从哈希表中获取方法

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

在下面的代码中,我创建了一个存储 int 数字的哈希表,以及带有“put”方法的 JButton。

但是当我尝试使用 int 数字从哈希表中“获取”时,我收到编译器错误 - “类型不匹配:无法从 Integer 转换为 JButton”

这是为什么呢?我应该找回 JButton 否?

这是代码:

package Q2;
import javax.swing.*;
import java.util.Hashtable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.*;
import java.util.EventListener;
import java.util.Hashtable;

public class MainPanel extends JPanel{

**private Hashtable<JButton, Integer> keyboardHash;**
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",">"}};

private static final int[][] keyNumber = {
{192,49,50,51,52,53,54,55,56,57,48,45,61,8},
{9,81,87,69,82,84,89,85,73,79,80,91,93},
{20,65,83,68,70,71,72,74,75,76,59,192,222,10},
{16,90,88,67,86,66,66,78,77,44,46,47},
{32,37,40,39,86}};

//Constructor for main Panel
public MainPanel(){
super();
setLayout(new BorderLayout());

**keyboardHash = new Hashtable<JButton,Integer>();**



TextField textField = new TextField(20);
MyKeyListener myKeyListener = new MyKeyListener();
textField.addKeyListener(myKeyListener);
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));
**keyboardHash.put(button[row][column], keyNumber[row][column]);**
/*
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) {
int numKey = e.getKeyCode();
**JButton btnToColor = keyboardHash.get(numKey);**

System.out.println(numKey);
button[2][2].setBackground(Color.yellow);
}

@Override
public void keyReleased(KeyEvent e) {
button[2][2].setBackground(null);
}

@Override
public void keyTyped(KeyEvent e) {
}
}
}

最佳答案

你的泛型类在哈希表声明上的方式是错误的。交换 JButton 和 Integer。

private Hashtable<Integer, JButton> keyboardHash;

关于java - java中从哈希表中获取方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23550439/

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