New Posts  All Forums:Forum Nav:

Some more basic java help?

post #1 of 14
Thread Starter 
Okay guys another problem. Was wondering if you guys could help me out again. I've got this code...


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

public class LogicA extends JFrame
implements ActionListener
{

JLabel numOneLabel = new JLabel("Enter your first number: ");
JTextField numOneTxt = new JTextField(3);
JLabel numTwoLabel = new JLabel("Enter your second number: ");
JTextField numTwoTxt = new JTextField(3);
JTextArea commentTxt = new JTextArea (2,20);
JButton sumBtn = new JButton("ADD");

public static void main(String[] args)
{
LogicA jf = new LogicA();
}

public LogicA()
{
setLayout(new FlowLayout());
setSize(600, 200);
setTitle("Adding");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(numOneLabel);
add(numOneTxt);
add(numTwoLabel);
add(numTwoTxt);
add(sumBtn);
add(commentTxt);
sumBtn.addActionListener(this);
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
// put code here
int numberUno = numOneTxt.getValue();
int numberDuo = numTwoTxt.getValue();
int numberFinal = numberUno + numberDuo;
commentTxt.setText(""+ numberFinal);

}
}



And it just isn't compiling. Basically I want to enter in two number and for them to be added together. All the other stuff is fine except for the bit in the action performed at end (the bit where I've commented I need help)... I have a feeling I'm close... Ultimately I'd like it to print to the text area saying "the sum of these two numbers is <insert number>". The errors I get when I compile are...

---------- JavaCompiler ----------
LogicA.java:40: cannot find symbol
symbol : method getValue()
location: class javax.swing.JTextField
int numberUno = numOneTxt.getValue();
^
LogicA.java:41: cannot find symbol
symbol : method getValue()
location: class javax.swing.JTextField
int numberDuo = numTwoTxt.getValue();
^
2 errors

Output completed (0 sec consumed) - Normal Termination

So any ideas what I've left out here?
Edited by kidshenlong - 3/1/11 at 1:04pm
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 14
There is no getValue() method in the the JTextField class.
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #3 of 14
Thread Starter 
Sorry I'm a bit of a noob... Could you a explain what I've done wrong exactly... I didn't know I'd need another getValue () method, so um why do I?...
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #4 of 14
http://download.oracle.com/javase/6/...TextField.html

The java text field does not implement a getValue function, so your trying to use a non existant function.

If you look at the methods contained within the class, you will notice there is no getValue.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #5 of 14
Thread Starter 
Hmmm I sort of get you... So what would I have to do to get this running... I'm pretty confused though as these were the files my lecturer gave me so I didn't think I'd have to change THAT much...
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #6 of 14
Your going to have to wait for Plex on this one, or someone else more handy in Java than i am. It looks like your getting into events here somewhat and its just beyond the scope of my class knowledge for Java.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #7 of 14
Quote:
Originally Posted by kidshenlong View Post
Hmmm I sort of get you... So what would I have to do to get this running... I'm pretty confused though as these were the files my lecturer gave me so I didn't think I'd have to change THAT much...
Well.. by default, the JTextField is bringing in the text as a String. So you would need to parse it to an Integer and assign it to the new variable using the getText() method.

Something like...

Code:
int numberUno = Integer.parseInt(numOneTxt.getText());
int numberDuo = Integer.parseInt(numTwoTxt.getText());
should work.
Edited by Plex - 3/1/11 at 1:58pm
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #8 of 14
Thread Starter 
Thanks! That's done it. But would you mind explaining this parse thing to me a bit more? I've only come across it a bit before and I hate using something that I don't completely understand.

But still thanks for the help everyone. Rep+
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #9 of 14
Ah, ok after reading up a bit more and educating myself.

http://download.oracle.com/javase/1....#getText%28%29

Thats the getText() function that java.swing.Jcomponent exposes, and Jtextfield inherits that function from that java.swing.Jcomponent class. You can see the function returns a string representing the text that is in the field.

As Plex showed, you want to convert the string to an integer and then the addition is self explanatory.


http://download.oracle.com/javase/1....lang.String%29

Shows you the Integer class with its parse method accepting a string.

The parse method is a static method supplied by the Integer class made for converting strings to integer representations.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #10 of 14
Thread Starter 
Thanks guys I'm still not a 100% on this but I get what it basically does. I'm given a String. I convert into something I can apply maths to, an integer. And then the rest is magic. I think my lecturer might've wanted me to use a different method but again the notes aren't too clear. But hey the result in the end is the same. So thanks again guys. Helpful as ever
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming