Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 27th, 2005, 12:42 AM   #1
Nano
Newbie
 
Nano's Avatar
 
Join Date: Aug 2004
Location: Hacienda Heights, CA
Posts: 8
Rep Power: 0 Nano is on a distinguished road
Send a message via AIM to Nano Send a message via MSN to Nano Send a message via Yahoo to Nano
Display result to GUI window

Hi eveyone,
I am working on a program that will calculate the power generated by increasing the velocity. I made it to work with pressing up and down arrow keys to increase and decrease the velocity, then it output velocity and power by System.out.println. I am trying to get it to work so it output the values on the GUI window.
This is my program so far, I tried to use JLabel, but no luck
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class KeyListenerTest extends JFrame implements KeyListener
{

   private final double Weigh=600;
   private final double Crr=0.007;
   private final double Cd=0.15;
   private final double A=0.0417;
   private final double p=0.00238;
   private double velocity=0;
   private String power="";
   
   JLabel label;


   public KeyListenerTest(String s)
   {
      super(s);

      setLayout(null);
      addKeyListener(this);
      
      label = new JLabel();
   }

   public void calcPow(double velocity)
   {
 	  power+=((Weigh*Crr)+((0.5*p)*(velocity*velocity)*Cd*A))*velocity;
   }

   public void keyTyped(KeyEvent e)
   {
   }

   public void keyPressed(KeyEvent e)
   {
	  if(e.getKeyCode()==KeyEvent.VK_UP)
      {
      	velocity++;
      }

      if(e.getKeyCode()==KeyEvent.VK_DOWN)
      {
      	velocity--;
      }

      calcPow(velocity);
	  //System.out.println("Velocity is: "+velocity);
	  //System.out.println("Power is: "+power);
	  label.setText(power);
   }

   public void keyReleased(KeyEvent e)
   {
   }
}
public class KeyListenerPower
{
   public static void main(String[] args)
   {
      KeyListenerTest keyListenerTester = new KeyListenerTest("Key Listener Tester");

      keyListenerTester.setSize(100,100);
      keyListenerTester.setVisible(true);
   }
}

Any help will be appreciated.
__________________
why....why u.s. citizen.............
Nano is offline   Reply With Quote
Old May 27th, 2005, 1:43 AM   #2
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
it doesn't look like you put your label on the panel. and then also, it doesn't look like you made a panel to put on your frame either.
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old Jun 1st, 2005, 12:06 PM   #3
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
Yeah, you want to finish making your JLabel. Then, try:

label = new JLabel(velocity);

Then add the JLabel and other parts, then validate your GUI and it should work for the velocity (I'm a little rusty on Java, so if there is a better way, please enlighten me )
Knight is offline   Reply With Quote
Old Jun 1st, 2005, 12:18 PM   #4
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
This goes kinda this:
Container c = getContentPane(); //the container that holds the panel
JPanel panel = new JPanel();
panel.add(label); //puts the label into the panel
c.add(panel); //puts the panel into the container
__________________
countdown++;
HeX is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC