Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,310 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 878 people online right now. Registration is fast and FREE... Join Now!




Launch App with Key Press

 
Reply to this topicStart new topic

Launch App with Key Press, Launch App with Key Press

codeninja
23 Oct, 2007 - 08:25 AM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 44


My Contributions
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



User is offlineProfile CardPM
+Quote Post


PennyBoki
RE: Launch App With Key Press
23 Oct, 2007 - 11:12 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,075



Thanked: 12 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Well I think if you post the code of the Calcnew class, at least it's constructor, some of us could be able to tell you what's wrong, or where might the problem be.
User is offlineProfile CardPM
+Quote Post

codeninja
RE: Launch App With Key Press
23 Oct, 2007 - 08:37 PM
Post #3

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 44


My Contributions

Sorry. Here it is:

CODE

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;


public class Calcnew extends JFrame implements FocusListener {
    
    JTextField value1 = new JTextField("0", 5);
    JLabel plus = new JLabel("+");
    JTextField value2 = new JTextField("0", 5);
    JLabel equals = new JLabel("=");
    JTextField sum = new JTextField("0", 5);
    
    public Calcnew(){
        super("Add Two Numbers");
        setSize(350, 90);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
        setLayout(flow);
        value1.addFocusListener(this);
        value2.addFocusListener(this);
        
        sum.setEditable(false);
        add(value1);
        add(plus);
        add(value2);
        add(equals);
        add(sum);
        
        setVisible(true);
        
    }
    
    public void focusGained(FocusEvent event){
        
        
        try{
            float total = Float.parseFloat(value1.getText())+Float.parseFloat(value2.getText());
            
            sum.setText("" + total);
            
            
            
        
            
            
        }catch(NumberFormatException nfe){
            
            value1.setText("0");
            value2.setText("0");
            sum.setText("0");
            
        }
    }
    
    
    public void focusLost(FocusEvent event){
        focusGained(event);
        
    }
    
    
    //public static void main (String[] args){
    //    Calcnew frame = new Calcnew();
        
        
    //}
    

}









User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 07:31PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month