I am making a typing app for a school project, I am trying to implement the KeyListener class however, when I look at any of the methods like “keyTyped”, there is a message saying throws UnsupportedOperationException.If I code anything underneath it it highlights that code as an unreachable statement. I’ve tried deleting the message and coding as normal, however, this only leads to the method never working when you type a key on the frame.
I originally ignored the exception and coded above it but it just produces nothing in the return box.
I also am not sure how to add the key listener, this could be part of the problem.
here is my code:
the main issue is at the bottom.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package it.pat;
import BackEnd.User;
import BackEndTyping.TypingDataBase;
import static it.pat.Account.Usermain;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
*
* @author danie
*/
public class MainTypingScreen extends javax.swing.JFrame implements KeyListener{
static public User main;
public String Text=" ";
public TypingDataBase db=new TypingDataBase();
public int check;
//TypingDataBase db=new TypingDataBase();
/**
* Creates new form MainTypingScreen
*/
public MainTypingScreen(User u) {
initComponents();
main=u;
TypingDataBase db=new TypingDataBase();
for (int i = 1; i <51; i++) {
Text=Text+db.generateWord()+" ";
}
TypeArea.setText(Text);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel4 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel6 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
Reset = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
TypeArea = new javax.swing.JTextPane();
jLabel4.setFont(new java.awt.Font("Yu Gothic UI", 0, 36)); // NOI18N
jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
jLabel4.setText("This is where the text will go where the user must type what is on screen if the correct letter is typed letter will turn green if it is the wrong vlaue letter turns red,if they backspace amistake it will go yellow");
jLabel4.setVerticalAlignment(javax.swing.SwingConstants.TOP);
jLabel4.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setPreferredSize(new java.awt.Dimension(1800, 1000));
jPanel4.setBackground(new java.awt.Color(51, 153, 255));
jPanel4.setBorder(new javax.swing.border.MatteBorder(null));
jPanel4.setPreferredSize(new java.awt.Dimension(1800, 72));
jPanel4.setLayout(null);
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/pat/Untitled design (1).png"))); // NOI18N
jPanel4.add(jLabel3);
jLabel3.setBounds(50, 0, 40, 64);
jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/pat/icons8-help-40.png"))); // NOI18N
jPanel4.add(jLabel10);
jLabel10.setBounds(1740, 10, 50, 40);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/pat/icons8-right-arrow-50.png"))); // NOI18N
jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jLabel1MousePressed(evt);
}
});
jPanel4.add(jLabel1);
jLabel1.setBounds(10, 0, 70, 60);
jPanel2.setBackground(new java.awt.Color(255, 255, 255));
jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel2.setLayout(null);
jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/pat/Right hand.png"))); // NOI18N
jPanel2.add(jLabel6);
jLabel6.setBounds(1340, 490, 460, 430);
jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/pat/Left hand.png"))); // NOI18N
jPanel2.add(jLabel5);
jLabel5.setBounds(20, 480, 460, 430);
jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/pat/KeyBoard2.png"))); // NOI18N
jPanel2.add(jLabel7);
jLabel7.setBounds(460, 540, 900, 390);
Reset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/pat/RESET.png"))); // NOI18N
Reset.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
Reset.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
ResetMousePressed(evt);
}
});
jPanel2.add(Reset);
Reset.setBounds(1630, 100, 126, 70);
TypeArea.setEditable(false);
TypeArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(51, 153, 255)));
TypeArea.setFont(new java.awt.Font("Yu Gothic UI Semibold", 0, 36)); // NOI18N
TypeArea.setForeground(new java.awt.Color(204, 204, 204));
jScrollPane3.setViewportView(TypeArea);
jPanel2.add(jScrollPane3);
jScrollPane3.setBounds(290, 50, 1270, 260);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 934, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
private void jLabel1MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
this.dispose();
MainMenu mg=new MainMenu(Usermain);
mg.setVisible(true);
}
private void ResetMousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
Text=" ";
for (int i = 1; i <51; i++) {
Text=Text+db.generateWord()+" ";
}
TypeArea.setText(Text);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainTypingScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainTypingScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainTypingScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainTypingScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainTypingScreen(main).setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Reset;
private javax.swing.JTextPane TypeArea;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane3;
// End of variables declaration
@Override
public synchronized void addKeyListener(KeyListener kl) {
super.addKeyListener(kl); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
}
@Override
public void keyTyped(KeyEvent ke) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
System.out.println("You pressed key "+ke.getKeyChar());
//this line produces an error("unreachable statment")
}
@Override
public void keyPressed(KeyEvent ke) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void keyReleased(KeyEvent ke) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
}
Dan.codeman12 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.