Java新手

我已经尝试了很多次,但似乎无法正常工作。.我尝试阅读Javadoc,但我并不完全理解。

eqn.setABC();接受三个整数,但是CoeffA,CoeffB和CoeffC是Jtextfields。我只想从Jtextfields中获取输入,将它们转换为ints,并将其输入eqn.setABC()。

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

public class FactorQuadraticUI extends JFrame implements ActionListener {

public JTextField coeffA;
public JTextField coeffB;
public JTextField coeffC;
public JTextField factors; //contains answer in form (px+q)(rx+s)
public JButton findFactors;
public QuadraticEqn eqn;
static final long serialVersionUID = 12345L;

public FactorQuadraticUI(QuadraticEqn e) {
    super("Quadratic Equation Factor Finder");

    eqn = e;

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JPanel eqnArea = new JPanel(new FlowLayout());
    coeffA = new JTextField(2);
    eqnArea.add(coeffA);
    eqnArea.add(new JLabel("x^2 +"));
    coeffB = new JTextField(2);
    eqnArea.add(coeffB);
    eqnArea.add(new JLabel("x +"));
    coeffC = new JTextField(2);
    eqnArea.add(coeffC);


    //////////JTextField f1 = new JTextField("-5");

    //control button:  find factors
    findFactors = new JButton("factor!");
    findFactors.addActionListener(this);
    eqnArea.add(findFactors);

    c.add(eqnArea);

    //output area
    factors = new JTextField(27);
    factors.setEditable(false);
    c.add(factors);

    this.setBounds(100, 100, 350, 100);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
 }

public void actionPerformed(ActionEvent e) {
    //"factor" button pressed


    //how to get the values out
    // and make them ints

    eqn.setABC(coeffA, coeffB, coeffC);

    factors.setText(eqn.toString()  + " = " + eqn.getQuadraticFactors()     );


    factors.setText("testing...");

}

}

最佳答案

您可以使用以下方法从JTextField中提取整数:

Integer.parse.Int(jtextField.getText());


此命令分为两部分:

第一部分:

JTextField.getText() // This gets text from text field. Ofcourse replace "JTextField" with your textfield's name.


第二部分:

Integer.parseInt(..) // This gets/parses the integer values in a string. We are inputting the string here from above step.

关于java - 从JTextField中获取值并转换为int,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33966644/

10-12 03:41