• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help!!!!!How to add hotkeys into JFrame

 
kim jungil
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help!!
Now, i want to make an application with several hotKeys in JFrame.My source like this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
public class Demo extends JFrame {
public Demo () {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.enableEvents(AWTEvent.KEY_EVENT_MASK);
getContentPane().setLayout(null);
closeBtt.setBounds(new Rectangle(0, 13, 19, 40));
closeBtt.setBorder(new TitledBorder(""));
closeBtt.setHorizontalAlignment(SwingConstants.LEFT);
closeBtt.setHorizontalTextPosition(SwingConstants.LEFT);
closeBtt.setMargin(new Insets(0, 0, 0, 0));
closeBtt.setMnemonic('0');
closeBtt.setText("X");
closeBtt.setVerticalAlignment(SwingConstants.TOP);
closeBtt.setVerticalTextPosition(SwingConstants.TOP);
this.setEnabled(true);
templateLab.setText("template");
templateLab.setBounds(new Rectangle(23, 25, 80, 17));
templateCB.setBounds(new Rectangle(103, 23, 109, 21));
codeLab.setText("name");
codeLab.setBounds(new Rectangle(225, 26, 33, 17));
codeTxt.setBounds(new Rectangle(266, 23, 63, 21));
setBtt.setBounds(new Rectangle(340, 20, 79, 27));
setBtt.setBorder(new TitledBorder(""));
setBtt.setText("set");
getContentPane().add(templateCB, null);
getContentPane().add(codeTxt, null);
getContentPane().add(setBtt, null);
getContentPane().add(closeBtt, null);
getContentPane().add(templateLab, null);
this.getContentPane().add(codeLab, null);
}
protected void processKeyEvent(KeyEvent evt) {
super.processKeyEvent(evt);
if(evt.getKeyCode() == KeyEvent.VK_F4){
System.out.println("AAAA");
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
;
}
Demo demo = new Demo();
demo.setBounds(50,50,479, 80);
demo.show();
}
private JButton closeBtt = new JButton();
private JLabel templateLab = new JLabel();
private JComboBox templateCB = new JComboBox();
private JLabel codeLab = new JLabel();
private JTextField codeTxt = new JTextField();
private JButton setBtt = new JButton();
}
when i clicked the F4 button, i can not catch the KeyEvent. "AAAA" is not printed.
What problem with my program?
help me, plz
thank u

BTW, if i remove all the components from JFrame except label, "AAAA" will be printed.
if possible, give me a demo.
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you shouldn't open several sessions for one and the same topic with the same code inside.
Anyway I compiled your demo and I made a run, at least for me it worked fine. Maybe it is a problem of JDK version? I use java version "1.4.0".
BTW if you register a KeyListener at your JFrame, then you will also catch all key events. It doesn't matter which component has the focus.
 
kim jungil
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i use "1.4.0" too.
but i can not print out "AAAA" anyway
why?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!