So I am making a typing tutor program as an assignment
The goal is to build a GUI with the shape of a keyboard and whenever a key is pressed on the keyboard, its corresponding JButton should be highlighted
I have written this code >> The action keys and the modifiers are working correctly but the letters and numbers are not working
Can you help me ?
Is there something wrong with my key listener?
What I have tried:
<pre>import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Keyboard extends JFrame {
private JTextField t,pangram;
private JLabel j1,j2;
private keys key;
private Color color;
private String label1,label2,label3;
private String keyNames[]={"Back Quote","1","2","3","4","5","6","7","8","9","0","Minus","Equals","Backspace","","Q","W","E","R","T","Y","U","I","O","P","Open Bracket","Close Bracket","Back Slash","Caps Lock","A","S","D","F","G","H","J","K","L","Semicolon","Quote","Enter","Shift","Z","X","C","V","B","N","M","Comma","Period","Slash","Up","Ctrl","","Windows","Alt","Space","Alt","Ctrl","Left","Down","Right"};
public Keyboard(){
setLayout(null);
label1=" Type some text using your keyboard.The Keys you press will be highlighted and the text will be displayed";
label2=" Note: CLicking the button with your mouse will not perform any action";
j1=new JLabel(label1);
j2=new JLabel(label2);
j1.setFont(new Font("Serif",Font.BOLD,20));
j2.setFont(new Font("Serif",Font.BOLD,20));
j1.setSize(945,23);
j2.setSize(945,30);
j1.setLocation(0,0);
j2.setLocation(0,23);
add(j1);
add(j2);
t=new JTextField();
t.setFont(new Font("",Font.PLAIN,30));
t.setSize(945,200);
t.setLocation(0,85);
t.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e){
System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
for (int i=0;i<keyNames.length;i++){
if (keyNames[i]==KeyEvent.getKeyText(e.getKeyCode())){
color=key.getButtons()[i].getBackground();
key.getButtons()[i].setBackground(Color.GREEN);
}
}
}
@Override
public void keyReleased(KeyEvent e){
for (int i=0;i<keyNames.length;i++){
if (keyNames[i]==KeyEvent.getKeyText(e.getKeyCode())){
key.getButtons()[i].setBackground(color);
}
}
}
});
add(t);
key=new keys();
key.setLocation(0,350);
add(key);
}
public static void main(String[] args) {
Keyboard k=new Keyboard();
k.setVisible(true);
k.setResizable(false);
k.setDefaultCloseOperation(EXIT_ON_CLOSE);
k.setSize(950,655);
}
}
Here's the Keys class code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
class keys extends JLabel{
private String firstRow[]={"~","1","2","3","4","5","6","7","8","9","0","-","+","BackSpace"};
private String secondRow[]={"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\\"};
private String thirdRow[]={"Caps","A","S","D","F","G","H","J","K","L",":","'","Enter"};
private String fourthRow[]={"Shift","Z","X","C","V","B","N","M",",",".","/","^"};
private String fifthRow[]={"Ctrl","Fn","Win","Alt","","Alt","Ctrl","<","*",">"};
private int count;
private JButton buttons[];
private int m;
private Color color;
public keys(){
setLayout(null);
setSize(970,263);
count=firstRow.length+secondRow.length+thirdRow.length+fourthRow.length+fifthRow.length;
buttons=new JButton[count];
for(int i=0;i<firstRow.length-1;i++){
buttons[i]=new JButton(firstRow[i]);
buttons[i].setFont(new Font("",Font.PLAIN,14));
buttons[i].setSize(60,50);
buttons[i].setLocation(i*60+i*2+5,5);
add(buttons[i]);
}
buttons[13]=new JButton(firstRow[13]);
buttons[13].setFont(new Font("",Font.PLAIN,14));
buttons[13].setSize(130,50);
buttons[13].setLocation(811,5);
add(buttons[13]);
buttons[14]=new JButton(secondRow[0]);
buttons[14].setFont(new Font("",Font.PLAIN,14));
buttons[14].setSize(100,50);
buttons[14].setLocation(5,57);
buttons[14].setFocusable(false);
add(buttons[14]);
m=15;
for(int i=0;i<secondRow.length-2;i++){
buttons[m]=new JButton(secondRow[i+1]);
buttons[m].setFont(new Font("",Font.PLAIN,14));
buttons[m].setSize(60,50);
buttons[m].setLocation(i*60+i*2+107,57);
add(buttons[m]);
m++;
}
buttons[27]=new JButton(secondRow[13]);
buttons[27].setFont(new Font("",Font.PLAIN,14));
buttons[27].setSize(90,50);
buttons[27].setLocation(851,57);
add(buttons[27]);
buttons[28]=new JButton(thirdRow[0]);
buttons[28].setFont(new Font("",Font.PLAIN,14));
buttons[28].setSize(110,50);
buttons[28].setLocation(5,109);
add(buttons[28]);
m=29;
for(int i=0;i<thirdRow.length-2;i++){
buttons[m]=new JButton(thirdRow[i+1]);
buttons[m].setFont(new Font("",Font.PLAIN,14));
buttons[m].setSize(60,50);
buttons[m].setLocation(i*60+i*2+117,109);
add(buttons[m]);
m++;
}
buttons[40]=new JButton(thirdRow[12]);
buttons[40].setFont(new Font("",Font.PLAIN,14));
buttons[40].setSize(142,50);
buttons[40].setLocation(799,109);
add(buttons[40]);
buttons[41]=new JButton(fourthRow[0]);
buttons[41].setFont(new Font("",Font.PLAIN,14));
buttons[41].setSize(122,50);
buttons[41].setLocation(5,161);
add(buttons[41]);
m=42;
for(int i=0;i<fourthRow.length-2;i++){
buttons[m]=new JButton(fourthRow[i+1]);
buttons[m].setFont(new Font("",Font.PLAIN,14));
buttons[m].setSize(60,50);
buttons[m].setLocation(i*60+i*2+129,161);
add(buttons[m]);
m++;
}
buttons[52]=new JButton(fourthRow[11]);
buttons[52].setFont(new Font("",Font.PLAIN,14));
buttons[52].setSize(60,50);
buttons[52].setLocation(813,161);
add(buttons[52]);
buttons[53]=new JButton(fifthRow[0]);
buttons[53].setFont(new Font("",Font.PLAIN,14));
buttons[53].setSize(100,50);
buttons[53].setLocation(5,213);
add(buttons[53]);
m=54;
for (int i=0;i<3;i++){
buttons[m]=new JButton(fifthRow[i+1]);
buttons[m].setFont(new Font("",Font.PLAIN,14));
buttons[m].setSize(65,50);
buttons[m].setLocation(i*65+i*2+107,213);
add(buttons[m]);
m++;
}
buttons[57]=new JButton(fifthRow[4]);
buttons[57].setFont(new Font("",Font.PLAIN,14));
buttons[57].setSize(280,50);
buttons[57].setLocation(308,213);
add(buttons[57]);
buttons[58]=new JButton(fifthRow[5]);
buttons[58].setFont(new Font("",Font.PLAIN,14));
buttons[58].setSize(60,50);
buttons[58].setLocation(590,213);
add(buttons[58]);
buttons[59]=new JButton(fifthRow[6]);
buttons[59].setFont(new Font("",Font.PLAIN,14));
buttons[59].setSize(95,50);
buttons[59].setLocation(652,213);
add(buttons[59]);
m=60;
for (int i=0;i<3;i++){
buttons[m]=new JButton(fifthRow[i+7]);
buttons[m].setFont(new Font("",Font.PLAIN,14));
buttons[m].setSize(60,50);
buttons[m].setLocation(i*60+i*2+751,213);
add(buttons[m]);
m++;
}
setFocusable(true);
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
for (JButton b: buttons){
if (b.getText().equals(KeyEvent.getKeyText(e.getKeyCode())))
{color=b.getBackground();
b.setBackground(Color.RED);}
}
}
public void keyReleased(KeyEvent e){
for (JButton b: buttons){
if (b.getText()==KeyEvent.getKeyText(e.getKeyCode()))
b.setBackground(color);
}
}
});
}
public JButton[] getButtons(){
return buttons;
}
}