سؤال

So I have been working on this program and I cannot get the input from the 3 fields (a,b,c) to store as variables. Any help will be appreciated. Everything seems to work and if the button is pressed it closes the window but it will not proceed because the input is not stored.

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;

public class Herons extends JFrame implements ActionListener {
 public static JTextField a;
 public static JTextField b;
 public static JTextField c;
 public static String aa1;
 public static String bb1;
 public static String cc1;
 public static JFrame main = new JFrame("Herons Formula");
 public static JPanel myPanel = new JPanel(new GridLayout (0,1));
public static void main(String args[]){
     Herons object = new Herons();
    }
Herons(){
    //JFrame main = new JFrame("Herons Formula");
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //JPanel myPanel = new JPanel(new GridLayout (0,1));
    //JPanel pane = new JPanel(new GridLayout(0,1));

    myPanel.setPreferredSize(new Dimension(250,300));

    JTextField a = new JTextField(3);
    JTextField b = new JTextField(3);
    JTextField c = new JTextField(3);
    JButton find = new JButton("Calculate!");

    main.add(myPanel);
    myPanel.add(new JLabel ("Input the lengh of each side:"));
    main.add(myPanel);
    myPanel.add(new JLabel ("A:"));
    myPanel.add(a);

    myPanel.add(new JLabel ("B:"));
    myPanel.add(b);

    myPanel.add(new JLabel ("C:"));
    myPanel.add(c);

    myPanel.add(find);
    //find.setActionCommand("Calculate!");
    find.addActionListener(this);
    main.pack();
    main.setVisible(true);

    String aa = a.getText();
    String bb = b.getText();
    String cc = c.getText();
    aa1 = aa;
    bb1 = bb;
    cc1 = cc;

    //JOptionPane.showMessageDialog(null, myPanel);

}
 public void actionPerformed(ActionEvent e) {
    String actionCommand = ((JButton) e.getSource()).getActionCommand();
    //System.out.println("Action command for pressed button: " + actionCommand);
    if (actionCommand == "Calculate!") {



     main.setVisible(false);
     myPanel.setVisible(false);
     main.dispose();
   //String aa = a.getText();
   //String bb = b.getText();
   //String cc = c.getText();

    double aaa = Double.parseDouble(aa1);
    double bbb = Double.parseDouble(bb1);
    double ccc = Double.parseDouble(cc1);
    double s = 0.5 * (aaa + bbb + ccc);
    double area = Math.sqrt(s*(s-aaa)*(s-bbb)*(s-ccc));
    area = (int)(area*10000+.5)/10000.0;
    if (area == 0){
        area = 0;
    }
    JOptionPane.showMessageDialog(null, "The area of the triangle is: " + area);
    }
}
}
هل كانت مفيدة؟

المحلول

The problem is basically this:

class Herons {
    static JTextField a;

    Herons() {
        JTextField a = new JTextField(); // 'a' is shadowed
    }
}

When you say JTextField a = ... in the constructor it declares a different local variable called a and shadows the field.

It should be like this:

Herons() {
    a = new JTextField(); // field 'a' is assigned
}

Otherwise you may have noticed that when you tried to call getText on your fields they were null in actionPerformed.

I found this page about hiding/shadowing if you want to read more.

As a side note, your variables also do not need to be static (and maybe shouldn't be since you're creating an instance of the class to refer to them).

نصائح أخرى

You must get the value of the TextFields in the actionPerformed method, not in your constructor. When you get the value of these text fields in the constructor, they're empty, because they've just been added to the interface. So add this at the beginning of the method:

public void actionPerformed(ActionEvent e) {

String aa = a.getText();

String bb = b.getText();

String cc = c.getText();

and remove those lines from the constructor:

String aa = a.getText();

String bb = b.getText();

String cc = c.getText();

aa1 = aa;

bb1 = bb;

cc1 = cc;

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top