Launch App with Key Press

Launch App with Key Press

Page 1 of 1

2 Replies - 3125 Views - Last Post: 23 October 2007 - 09:37 PM Rate Topic: -----

#1 codeninja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

Launch App with Key Press

Post icon  Posted 23 October 2007 - 09:25 AM

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.

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

Is This A Good Question/Topic? 0
  • +

Replies To: Launch App with Key Press

#2 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: Launch App with Key Press

Posted 23 October 2007 - 12:12 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 codeninja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-February 07

Re: Launch App with Key Press

Posted 23 October 2007 - 09:37 PM

Sorry. Here it is:

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(); 
		
		
	//}
	

}









Was This Post Helpful? 0
  • +
  • -

Page 1 of 1