Results 1 to 2 of 2
  1. #1
    starexecutive is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Getting unrelated input from several text fields

    I am trying to write a program in which there are a series of questions on the left, and a text field for user input on the right. For each of the questions, the person can give one of two answers. If the person answers A, for instance, then a set of integers get one value, and if the person enters B, the same set of integers gets assigned anther value. At the end, the program is supposed to generate a response based on mathematical computations from the A and B values and provide the user with feedback. So I created a class/panel for each question, with if-else statements assigning the values. What would be the best way to write this program? Also, is it necessary to implement
    public void actionPerformed(ActionEvent event)
    for each of the text fields?

    I originally created a separate class for each question:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Container;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class Test7{
      public static void main (String [] Args) {
        JFrame frame = new JFrame ("Lets do this");
        frame.show();
        Container content = frame.getContentPane();
        content.setLayout(new GridLayout(5, 2, 20, 20));
        frame.setSize(900, 900);
        frame.setLocation(400, 50);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new AgePanel());
        frame.getContentPane().add(new EISPanel());
        frame.getContentPane().add(new CBEPanel());
        
        class AgePanel extends JPanel implements ActionListener {
      JLabel aLabel, sLabel;
      JTextField aField;
     
      AgePanel() {
        aLabel = new JLabel ("Are you younger than thirty five years of age?       ");
        aField = new JTextField (3);
        aField.addActionListener(this);
        aLabel.setFont(new Font(null, 0, 12));
        add(aLabel);
        add(aField);
      }
     
      public void actionPerformed(ActionEvent event) {
        String InputAge = aField.getText();
        double YoungerYes = 0.48;
        double YoungerNo = 0.41;
        double OlderYes = 0.52;
        double OlderNo = 0.59;
        double AgeY = 0;
        double AgeN = 0;
      
       if ("YES".equals(InputAge.toUpperCase()))
        {
          AgeY = YoungerYes;
          AgeN = YoungerNo;
          aField.setText("");
        }
        else if ("NO".equals(InputAge.toUpperCase()))
          {
          AgeY = OlderYes;
          AgeN = OlderNo;
          aField.setText("");
          }
        else {
          return;
        }
      }
    }
    
    class EISPanel extends JPanel implements ActionListener {
      JLabel EISLabel;
      JTextField EISField;
     
      EISPanel() {
        EISLabel = new JLabel ("Indicate whether your EIS results are positive or negative:       ");
        EISField = new JTextField (3);
        EISField.addActionListener(this);
        EISLabel.setFont(new Font(null, 0, 12));
        add(EISLabel);
        add(EISField);
      }
     
      public void actionPerformed(ActionEvent event) {
        String InputResult = EISField.getText();
        double NegativeYes = 0.57;
        double NegativeNo = 0.96;
        double PositiveYes = 0.43;
        double PositiveNo = 0.04;
        double EISY = 0;
        double EISN = 0;
       if ("POSITIVE".equals(InputResult.toUpperCase()) || "POS".equals (InputResult.toUpperCase()))
        {
          EISY = PositiveYes;
          EISN = PositiveNo;
          EISField.setText ("");
        }
        else if ("NEGATIVE".equals(InputResult.toUpperCase()) || "NEG".equals (InputResult.toUpperCase ()))
          {
          EISY = NegativeYes;
          EISN = NegativeNo;
          EISField.setText ("");
        } else {
          return;
        }
      }
    }
    
    class CBEPanel extends JPanel implements ActionListener {
      JLabel CBELabel;
      JTextField CBEField;
     
      CBEPanel() {
        CBELabel = new JLabel ("Are your clinical exams not suspicious or suspicious?     ");
        CBEField = new JTextField (3);
        CBEField.addActionListener(this);
        CBELabel.setFont(new Font(null, 0, 12));
        add(CBELabel);
        add(CBEField);
      }
     
      public void actionPerformed(ActionEvent event) {
        String InputCBE = CBEField.getText();
        double NotSusYes = 0.62;
        double NotSusNo = 0.96;
        double SusYes = 0.38;
        double SusNo = 0.04;
        double CBEY = 0;
        double CBEN = 0;
       
        if ("SUSPICIOUS".equals(InputCBE.toUpperCase()))
        {
          CBEY = SusYes;
          CBEN = SusNo;
          CBEField.setText ("");
        }
        else if ("NOT".equals(InputCBE.toUpperCase()) || "NOT SUSPICIOUS".equals (InputCBE.toUpperCase ()))
          {
          CBEY = NotSusYes;
          CBEN = NotSusNo;
          CBEField.setText ("");
          }
        else {
          return;
        }
      }
    }


    At the end, I need to add up all the -Y and -N values and determine which one is larger and tell the user. How would I do this? I do not know how to

    Thank you in advance!

    Moderator Edit: Code tags added
    Last edited by Fubarable; 01-16-2011 at 06:56 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I wouldn't give any of the JTextFields an ActionListener and in fact wouldn't use JTextFields here at all. My guess is that you want to process all the input at the end, so perhaps you want to have a submit JButton or something similar at the bottom of the GUI. If you want to limit the input to one of two choices, then perhaps you want to use a JCheckBox or two JRadioButtons, thus limiting the user to only making choices that make sense. One way to display the questions and the JRadioButtons is via a JTable or another way is with a grid of JPanels.

    Finally, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

    Best of luck and welcome to the forum!

Similar Threads

  1. Activating Text Fields
    By BennyJass in forum AWT / Swing
    Replies: 1
    Last Post: 01-15-2011, 11:13 PM
  2. disapling text fields
    By javanew in forum AWT / Swing
    Replies: 2
    Last Post: 05-06-2010, 03:39 PM
  3. Problem With Text Fields!
    By freshoreo in forum AWT / Swing
    Replies: 3
    Last Post: 08-04-2008, 09:52 PM
  4. Demonstration of text fields in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:20 PM
  5. Help with text fields in Java
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •