Reputation: 33
I'm fairly new to Java, and I have totally self taught my self to this point.
Right now I am trying to add a KeyListener to my JFrame class, I have no idea what I'm doing wrong and need some help. Any other tips are welcome.
My JFrame class:
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class TestJavaFrame implements ActionListener, KeyListener {
private static JFrame frame = new JFrame();
// Componenets
JLabel timeinmslabel = new JLabel("Enter the time in miliseconds:");
JTextField timeinms = new JTextField();
JRadioButton checkBox = new JRadioButton();
JRadioButton checkBox2 = new JRadioButton();
private boolean amountoftimes = false;
public TestJavaFrame(String windowname) {
frame.setName(windowname);
frame.setResizable(true);
frame.setSize(900, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(150, 50);
// JPanel
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(frame.getBounds());
// Bounds for components
timeinmslabel.setBounds((int) (frame.getBounds().getX() / 2), 125, 200, 25);
timeinms.setBounds((int) (frame.getBounds().getX() / 2 + 185), 125, 200, 25);
checkBox.setBounds((int) (frame.getBounds().getX() / 2 + 185), 40, 200, 25);
checkBox2.setBounds((int) (frame.getBounds().getX() / 2 + 185), 70, 200, 25);
// Action Listeners
checkBox.addActionListener(this);
frame.addKeyListener(this);
// edit components
checkBox.setText("Use clicked amount of times.");
// add components
panel.add(timeinmslabel);
panel.add(timeinms);
panel.add(checkBox);
panel.add(checkBox2);
frame.add(panel);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyChar());
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println(e.getKeyChar());
}
@Override
public void keyTyped(KeyEvent e) {
System.out.println(e.getKeyChar());
}
}
If you need my main, I'm perfectly fine with posting it, all it does right now is create this gui though.
Upvotes: 0
Views: 264
Reputation: 324118
So when the user hits the specific key it stops the auto clicker
Yes you can add the Key Binding to the panel.
A better approach is to create a menu bar for the various Actions support by your application. Then you can have menu items to start/stop the clicker. When you create the menu items you can then assign an accelerator to the menu item and the menu item will create the key bindings for you automatically.
This is a better solution because the "key binding" is self documenting since it is part of the menu item.
Read the section from the Swing tutorial on How to Use Menus for more information and working examples to get you started.
as I said I'm trying to learn here.
Keep a link to the tutorial handy for all Swing basics. There are also sections on "Key Bindings" and "How to Use Actions".
Upvotes: 1