程序无法在答案文本字段中显示该值

时间:2015-07-29 14:13:47

标签: java swing math awt

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

public class Math5 extends JFrame implements ActionListener {
  JFrame frame;
  Container myPane;
  JLabel aDisplay, bDisplay, cDisplay, ansDisplay; // label
  JTextField aText, bText, cText, ansText; // input
  JButton calculate; // calculate button
  JPanel panel0, panel1, panel2, panel3;
  public double aValue, bValue, cValue, negativeB, bSquared, fourAC, twoA, result1, result2;

  public Math5() {
    frame = new JFrame("My Quadratic Equation");
    myPane = frame.getContentPane();

    calculate = new JButton("Calculate"); // display calculate button
    calculate.addActionListener(this);

    panel0 = new JPanel();
    panel1 = new JPanel();
    panel2 = new JPanel();
  }

  // calculate.addActionListener(new buttonListener());

  public void launchFrame() {

    GridLayout ly0 = new GridLayout(1, 3);
    panel0.setLayout(ly0);

    GridLayout ly1 = new GridLayout(1, 3);
    panel1.setLayout(ly1);

    GridLayout ly2 = new GridLayout(1, 3);
    panel2.setLayout(ly2);

    panel0.add(new JLabel("A :"));
    panel0.add(new JTextField(3));
    panel0.add(new JLabel("B :"));
    panel0.add(new JTextField(3));
    panel0.add(new JLabel("C :"));
    panel0.add(new JTextField(3));

    panel1.add(new JLabel("Answer "));
    panel1.add(new JTextField(3));
    panel2.add(calculate);

    GridLayout mylayout = new GridLayout(3, 1);
    myPane.setLayout(mylayout);

    myPane.add(panel0);
    myPane.add(panel1);
    myPane.add(panel2);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }

  public void actionPerformed(ActionEvent calc) {
    if (calc.equals(calculate)) {

      aValue = Integer.parseInt(aText.getText());
      bValue = Integer.parseInt(bText.getText());
      cValue = Integer.parseInt(cText.getText());

      negativeB = bValue - (2 * bValue); // -b
      bSquared = bValue * bValue; // b^2
      fourAC = 4 * aValue * cValue; // 4ac
      twoA = 2 * aValue; // 2a

      result1 = negativeB + (Math.sqrt(bSquared - fourAC)) / twoA;
      result2 = negativeB - (Math.sqrt(bSquared - fourAC)) / twoA;

      ansText.setText(Double.toString(result1));
      ansText.setText(Double.toString(result2));
      /*
       * add (resultDisplay1); add (resultDisplay2); resultDisplay1 = new JLabel("Result 1 : ",); //display result1 resultDisplay2 = new JLabel("Result 2 : ", ); //display result2
       */
    }
  }

  public static void main(String[] args) {
    Math5 Welcome = new Math5();
    Welcome.launchFrame();
    // frame.getContentPane().add(new QuadraticFormula());
  }
}

代码本身正常运行但由于Actionlistener按钮无法在文本字段中显示答案。

为什么calculate.addActionListener(this);不起作用?你能简单解释一下吗?

1 个答案:

答案 0 :(得分:1)

if (calc.equals(calculate)){中,calc的类型为ActionEventcalculate的类型为JButton。他们不平等。

编辑使用@FastSnail建议的解决方案:

calc.getSource().equals(calculate)calc.getSource()==calculate

编辑完全修复

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

public class Math5 extends JFrame implements ActionListener {
    JFrame frame;
    Container myPane;
    JLabel aDisplay, bDisplay, cDisplay, ansDisplay; // label
    JTextField aText, bText, cText, ansText; // input
    JButton calculate; // calculate button
    JPanel panel0, panel1, panel2, panel3;
    public double aValue, bValue, cValue, negativeB, bSquared, fourAC, twoA, result1, result2;

    public Math5() {
        frame = new JFrame("My Quadratic Equation");
        myPane = frame.getContentPane();

        calculate = new JButton("Calculate"); // display calculate button
        calculate.addActionListener(this);

        panel0 = new JPanel();
        panel1 = new JPanel();
        panel2 = new JPanel();
    }

    // calculate.addActionListener(new buttonListener());

    public void launchFrame() {

        GridLayout ly0 = new GridLayout(1, 3);
        panel0.setLayout(ly0);

        GridLayout ly1 = new GridLayout(1, 3);
        panel1.setLayout(ly1);

        GridLayout ly2 = new GridLayout(1, 3);
        panel2.setLayout(ly2);

        panel0.add(new JLabel("A :"));
        aText = new JTextField(3);
        panel0.add(aText);
        panel0.add(new JLabel("B :"));
        bText = new JTextField(3);
        panel0.add(bText);
        panel0.add(new JLabel("C :"));
        cText = new JTextField(3);
        panel0.add(cText);

        panel1.add(new JLabel("Answer "));
        ansText = new JTextField(3);
        panel2.add(ansText);
        panel2.add(calculate);

        GridLayout mylayout = new GridLayout(3, 1);
        myPane.setLayout(mylayout);

        myPane.add(panel0);
        myPane.add(panel1);
        myPane.add(panel2);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent calc) {
        if (calc.getSource().equals(calculate)) {

            aValue = Integer.parseInt(aText.getText());
            bValue = Integer.parseInt(bText.getText());
            cValue = Integer.parseInt(cText.getText());

            negativeB = bValue - (2 * bValue); // -b
            bSquared = bValue * bValue; // b^2
            fourAC = 4 * aValue * cValue; // 4ac
            twoA = 2 * aValue; // 2a

            result1 = negativeB + (Math.sqrt(bSquared - fourAC)) / twoA;
            result2 = negativeB - (Math.sqrt(bSquared - fourAC)) / twoA;

            ansText.setText(Double.toString(result1));
            ansText.setText(Double.toString(result2));
      /*
       * add (resultDisplay1); add (resultDisplay2); resultDisplay1 = new JLabel("Result 1 : ",); //display result1 resultDisplay2 = new JLabel("Result 2 : ", ); //display result2
       */
        }
    }

    public static void main(String[] args) {
        Math5 Welcome = new Math5();
        Welcome.launchFrame();
        // frame.getContentPane().add(new QuadraticFormula());
    }
}