![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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............. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
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 ) |
|
|
|
|
|
#4 |
|
Programmer
|
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++; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|