Note that there are some explanatory texts on larger screens.

plurals
  1. POJava keyListener
    primarykey
    data
    text
    <p>I need some help on my java code I'm a newbie in java, What I want to do is once I press a letter I want to display some message just to verify that the code works. Using javax swing library specifically the keylistener and key event and I don't know if I'm doing it right. here are my code:</p> <pre><code>import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; public class KeyStrokeButton{ public JFrame frame; public JPanel panel; public JButton btnfocus, btnrelease, btnwindow, btnancestor; public JTextField text; public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { KeyStrokeButton mydesign = new KeyStrokeButton(); mydesign.design(); } }); } public void design() { panel = new JPanel(); panel.setLayout(null); frame = new JFrame("Keystroke Tutorials"); frame.getContentPane().add(panel); frame.setSize(250, 250); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btnfocus = new JButton("&lt;html&gt;&lt;center&gt;Focus"); btnfocus.setBounds(10, 10, 90, 80); panel.add(btnfocus); btnfocus.addKeyListener(new ClickPressed()); btnrelease = new JButton("Release"); btnrelease.setBounds(110, 10, 90, 80); panel.add(btnrelease); btnancestor = new JButton("Ancestor"); btnancestor.setBounds(10, 100, 90, 80); panel.add(btnancestor); btnwindow = new JButton("Window"); btnwindow.setBounds(110, 100, 90, 80); panel.add(btnwindow); } class ClickPressed implements KeyListener { public void KeyPressed(KeyEvent e) { //any code. } } } </code></pre> <p>as much as possible I want to separate my code for the event, so I create a class to make it readable.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload