Results 1 to 20 of 20
- 11-05-2009, 01:25 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
GUI's and inputting doubles or ints
hey, im working on a basic gui program and i need to input some numbers from the interface so i can calculate their average.
So far my program is working, except for that part
Java Code:import javax.swing.*; // Required library import java.awt.*; // Required library import java.awt.event.*; // Required library public class MyGUI extends JFrame { private JTextField myExam = new JTextField(""); // Create a text field private JLabel labelWithExam = new JLabel("Exam avg"); private JTextField myHW = new JTextField(""); // Create a text field private JLabel labelWithHW = new JLabel("HW avg"); private JTextField myLab = new JTextField(""); // Create a text field private JLabel labelWithLab = new JLabel("Lab Avg"); private JTextField myQuiz = new JTextField(""); // Create a text field private JLabel labelWithQuiz = new JLabel("Quiz Avg"); private JButton myButton = new JButton("Calculate"); // Create a new button // Constructor public MyGUI() { setLayout(new GridLayout(5, 3, 5, 5)); // Define layout as a grid add(labelWithExam); add(myExam); add(labelWithHW); add(myHW); add(labelWithLab); add(myLab); add(labelWithQuiz); add(myQuiz); add(myButton); // Make button appear in window myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String ex = myExam.getText(); // Get the text in the TextField String h = myHW.getText(); // Get the text in the TextField String l = myLab.getText(); // Get the text in the TextField String q = myQuiz.getText(); // Get the text in the TextField
- 11-05-2009, 01:26 AM #2
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
im entering numbers for exam, hw, lab and quiz in the interface, but I cant use them because their strings...and im not sure how to input them rather as double types
Thanks
- 11-05-2009, 02:03 AM #3
- 11-05-2009, 02:30 AM #4
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
heyy, thanks
but its still not working. im getting incompatible types as an error.Java Code:String ex = myExam.getText(); // Get the text in the TextField Double.parseDouble(ex); String h = myHW.getText(); // Get the text in the TextField Double.parseDouble(h); String l = myLab.getText(); // Get the text in the TextField Double.parseDouble(l); String q = myQuiz.getText(); // Get the text in the TextField Double.parseDouble(q); double avg = 0; avg = ex + h + l + q; System.out.println(avg);
Anything else that could fix it?
-
You need to read the Double API as that's not how you use this method. It cannot change the String that is given into it magically into a double (nothing can). Rather it returns a double value that you must place into a double variable. In other words ex, h, l, and q are still Strings. So instead do this:
or perhaps even better:Java Code:String ex = myExam.getText(); double examGrade = Double.parseDouble(ex);
Java Code:double examGrade = Double.parseDouble(myExam.getText());
- 11-05-2009, 02:59 AM #6
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
thanks so much for your help...i have one more question though.
after i input my numbers and calculate the average, i want to press the calculate button and have the average appear on the screen as text next to the button. I'm trying:
Java Code:private JLabel labelWithButton = new JLabel(avg); ... ... add(labelWithButton);
-
- 11-05-2009, 03:09 AM #8
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
well its not working...got any tips?
-
How exactly is it not working? Sorry, but we really can't do anything to help you without more information.
One suggestion is to have the JLabel present in your app from the start, but with no text. Then when you get a solution, don't add a new JLabel, just set the text of the already existing label. Note that it must have class scope -- it must be visible throughout the class.
- 11-05-2009, 03:20 AM #10
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
well, the program is designed to calculate avg. when the gui pops up, i enter the four grades for the four components. then after i press the calculate button, the avg should appear next to it. however thats not working.
i also have the main method but its not important. everything is working, just not the part where i click the button and the average prints out next to it on the guiJava Code:import javax.swing.*; // Required library import java.awt.*; // Required library import java.awt.event.*; // Required library public class MyGUI extends JFrame { private JTextField myExam = new JTextField(""); // Create a text field private JLabel labelWithExam = new JLabel("Exam avg"); private JTextField myHW = new JTextField(""); // Create a text field private JLabel labelWithHW = new JLabel("HW avg"); private JTextField myLab = new JTextField(""); // Create a text field private JLabel labelWithLab = new JLabel("Lab Avg"); private JTextField myQuiz = new JTextField(""); // Create a text field private JLabel labelWithQuiz = new JLabel("Quiz Avg"); private JButton myButton = new JButton("Calculate"); // Create a new button private JLabel labelWithButton = new JLabel(avg); // Constructor public MyGUI() { setLayout(new GridLayout(5, 3, 5, 5)); // Define layout as a grid add(labelWithExam); add(myExam); add(labelWithHW); add(myHW); add(labelWithLab); add(myLab); add(labelWithQuiz); add(myQuiz); add(myButton); // Make button appear in window add(labelWithButton); myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String ex = myExam.getText(); // Get the text in the TextField double examGrade = Double.parseDouble(ex); String h = myHW.getText(); // Get the text in the TextField double hwGrade = Double.parseDouble(h); String l = myLab.getText(); // Get the text in the TextField double labGrade = Double.parseDouble(l); String q = myQuiz.getText(); // Get the text in the TextField double quizGrade = Double.parseDouble(q); double avg = 0; avg = examGrade*.56 + hwGrade*.22 + labGrade*.17 + quizGrade*.05; } }); }
- 11-05-2009, 03:21 AM #11
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
theres a problem with this line:
private JLabel labelWithButton = new JLabel(avg);
-
Sorry to sound like a broken record, but what problem? Are you getting an error at compile time or an exception at run time? If so, please post the error. Have you tried my suggestion: creating the JLabel at start up but not giving it text until the button has been pressed?
-
Oh I see what you are doing. You're trying to put a non-existent value into the JLabel. don't do that but instead just leave it blank:
Java Code:// private JLabel labelWithButton = new JLabel(avg); private JLabel labelWithButton = new JLabel();
- 11-05-2009, 03:33 AM #14
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
but if i do that then the average will not be printed after i press the calculate button
-
You have to tell it to do that in your actionPerformed method by setting the label's text as I mentioned in post 9: you must add the text when you get a solution. You only get the solution within the actionPerformed method.
- 11-05-2009, 03:44 AM #16
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
so basically move:
private JLabel labelWithButton = new JLabel(avg);
to the actionPerformed method
-
No. Again, declare the jlabel on the class level as you've done. It needs to be visible from the whole class. Place it in your app where it sits empty. Again, in your actionperformed set the text of the label only once you've calculated your average. Note that the label will require a String parameter not double when setting the text.
Something like
myLabel.setText("average: " + avg);
will work.
- 11-05-2009, 04:02 AM #18
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
ok so now i created this:
which i think you were saying...but now there is a "setText(java.lang.String) in javax.swing.AbstractButton cannot be applied to (double)" errorJava Code:private JTextField myExam = new JTextField(""); // Create a text field private JLabel labelWithExam = new JLabel("Exam avg"); private JTextField myHW = new JTextField(""); // Create a text field private JLabel labelWithHW = new JLabel("HW avg"); private JTextField myLab = new JTextField(""); // Create a text field private JLabel labelWithLab = new JLabel("Lab Avg"); private JTextField myQuiz = new JTextField(""); // Create a text field private JLabel labelWithQuiz = new JLabel("Quiz Avg"); private JButton myButton = new JButton("Calculate"); // Create a new button private JLabel labelWithButton = new JLabel(); // Constructor public MyGUI() { setLayout(new GridLayout(5, 3, 5, 5)); // Define layout as a grid add(labelWithExam); add(myExam); add(labelWithHW); add(myHW); add(labelWithLab); add(myLab); add(labelWithQuiz); add(myQuiz); add(myButton); // Make button appear in window add(labelWithButton); myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String ex = myExam.getText(); // Get the text in the TextField double examGrade = Double.parseDouble(ex); String h = myHW.getText(); // Get the text in the TextField double hwGrade = Double.parseDouble(h); String l = myLab.getText(); // Get the text in the TextField double labGrade = Double.parseDouble(l); String q = myQuiz.getText(); // Get the text in the TextField double quizGrade = Double.parseDouble(q); double avg = 0; avg = examGrade*.56 + hwGrade*.22 + labGrade*.17 + quizGrade*.05; myButton.setText(avg); } });
i tried doing String avgGrade = String.parseString(avg);
but it didnt work
- 11-05-2009, 04:34 AM #19
Member
- Join Date
- Nov 2009
- Posts
- 15
- Rep Power
- 0
I got it, the average is being printed on the button itself so if you know how to fix it can you tell me? but other than that, thanks a ton, youve been great
-
Similar Threads
-
Problem with division using doubles
By chrismanahan in forum New To JavaReplies: 3Last Post: 10-10-2009, 09:26 PM -
GUI's
By ngc0202 in forum New To JavaReplies: 7Last Post: 07-26-2009, 03:38 AM -
reading in unsigned ints into a 2D array
By newToIt in forum New To JavaReplies: 9Last Post: 03-06-2009, 12:36 PM -
GUI's
By diggitydoggz in forum New To JavaReplies: 2Last Post: 12-22-2008, 09:19 PM -
arrays strings and doubles
By rgvbabe in forum New To JavaReplies: 1Last Post: 01-13-2008, 11:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks