I have this code. I can get a Keypress to Output text on a label. But I want to launch another class when I press "w" . I thought it was just a matter of creating an object and clicking the key but it still opens the program in a console.
CODE
import java.awt.*;
import java.awt.event.*;
public class KeyPress extends Frame{
static String str ="";
Label label;
TextField txtField;
public static void main(String[] args,) {
KeyPress k = new KeyPress();
if(str.equals("w")){
Calcnew cn = new Calcnew();
}
}
public KeyPress(){
super("Key Press Event Frame");
Panel panel = new Panel();
label = new Label();
txtField = new TextField(20);
txtField.addKeyListener(new MyKeyListener());
add(label, BorderLayout.NORTH);
panel.add(txtField, BorderLayout.CENTER);
add(panel, BorderLayout.CENTER);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
setSize(400,400);
setVisible(true);
}
public class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent ke){
char i = ke.getKeyChar();
str = Character.toString(i);
String yo = "yo";
label.setText(str);
if(str.equals("k")){
label.setText(yo);
}
}
}
}
IDEAS???
C.Ninja