The Artima Developer Community
Sponsored Link

Java Answers Forum
Help With Text Field Labels

1 reply on 1 page. Most recent reply: Jul 18, 2011 4:09 AM by Venkata Kishore Podili

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Owen Kelly

Posts: 1
Nickname: okelly4408
Registered: Jul, 2011

Help With Text Field Labels Posted: Jul 17, 2011 11:41 PM
Reply to this message Reply
Advertisement
Hey I am pretty new to Java but I was wondering if anyone could help me with a few things. I wrote this applet that solves quadratic equations (you enter the coefficient of A in the first field B in the Second and value of C in the Last) Now this should return two solutions but it is only returning one of those in the program. Also the labels for each text fields are now apparent so they do not appear. Thanks for your time. Program:

import java.awt.Frame;

import javax.swing.JFrame;
public class Quadratic {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame ("Quadratic Solver");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

QuadraticPanel panel = new QuadraticPanel();

frame.getContentPane().add(panel);
frame.pack();
frame.setVi sible(true);


}

}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QuadraticPanel extends JPanel {
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField a1, b1, c1;

public QuadraticPanel()
{
inputLabel = new JLabel ("Enter the Coeffecient of A Squared: ");
inputLabel = new JLabel ("Enter the Coeffecient of B: ");
inputLabel = new JLabel ("Enter the Value of C: ");
outputLabel = new JLabel ("The solutions are: ");
resultLabel = new JLabel ();

a1 = new JTextField (5);
a1.addActionListener(new TempListener());

b1 = new JTextField (5);
b1.addActionListener(new TempListener());

c1 = new JTextField (5);
c1.addActionListener(new TempListener());

add (inputLabel);
add (a1, BorderLayout.WEST);
add (b1, BorderLayout.CENTER);
add (c1, BorderLayout.EAST);
add (outputLabel);
add (resultLabel);


setPreferredSize (new Dimension(300, 150));
setBackground (Color.lightGray);

}
private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
double aTemp, bTemp, cTemp, discriminant, solution1, solution2;


String text = a1.getText();
String text1 = b1.getText();
String text2 = c1.getText();

aTemp = Double.parseDouble(text);
bTemp = Double.parseDouble(text1);
cTemp = Double.parseDouble(text2);

discriminant = Math.pow(bTemp, 2) - (4 * aTemp * cTemp);
solution1 = ((-1 * bTemp) + Math.sqrt(discriminant)) / (2 * aTemp);
solution2 = ((-1 * bTemp ) - Math.sqrt(discriminant)) / (2 * aTemp);

resultLabel.setText (Double.toString (solution1));
resultLabel.setText (Double.toString (solution2));

}


}
}


Venkata Kishore Podili

Posts: 1
Nickname: kishorep
Registered: Jul, 2011

Re: Help With Text Field Labels Posted: Jul 18, 2011 4:09 AM
Reply to this message Reply
Dear Owen Kelly,

These are the small small mistakes every one will do at the starting time of learning.

The Mistake the you have done was, i will explain with the example.
eg: int i = 20;
--------
--------
i = 30;
--------
--------
i = 40;
--------
--------

System.out.print("i-->"+i );

Will it print like this i= 20, i=30, i=40? it will print the last value(i.e i = 40) because you are continuosly re-assigning(keeps on chagning the value of i).

In the same manner, as like in the above example reassinging the "i" value, In your application you have reassinged the "inputLabel" and "resultLabel", so you are not getting the out put as you have expected.

// Here is the changed code snippet of "TempListener.java"
// I have used the out put statements to console just for debugging purpose (as i know, using out put statements to console is good practise at the time of learning, not in Real Time Bussiness Applications)

private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
double aTemp, bTemp, cTemp, discriminant, solution1, solution2;


String text = a1.getText();
String text1 = b1.getText();
String text2 = c1.getText();

aTemp = Double.parseDouble(text);
bTemp = Double.parseDouble(text1);
cTemp = Double.parseDouble(text2);

System.out.println("-A--->"+text+"- "+aTemp+"-------");
System.out.println("-B--->"+text1+"- "+bTemp+"-------");
System.out.println("-C--->"+text2+"- "+cTemp+"-------");

discriminant = Math.pow(bTemp, 2) - (4 * aTemp * cTemp);
System.out.println("discriminant---->"+discriminant);
solution1 = Math.sqrt(discriminant); //((-1 * bTemp) + Math.sqrt(discriminant)) / (2 * aTemp);
System.out.println("solution1---->"+solution1);
solution2 = ((-1 * bTemp ) - Math.sqrt(discriminant)) / (2 * aTemp);
System.out.println("solution2---->"+solution2);

//resultLabel.setTe xt (Double.toString (solution1));
resultLabel.setText ("Result 1= "+Double.toString (solution1)+" Result 2 = "+Double.toString (solution2));

}


}


Kelly.. i think you have cleared your problem. if you have any doughts in java/j2EE get me on "kishore.podili@gmail.com"

Flat View: This topic has 1 reply on 1 page
Topic: multiple versions of same jar Previous Topic   Next Topic Topic: interface and inheritance fill in

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use