多个Jtextfields到一个Jbutton

时间:2012-10-09 09:03:09

标签: jbutton jtextfield

我想知道如何将4个单独的Jtextfield放入一个Jbutton中,如果在文本字段中输入3个值并按下按钮,它们将在第4个文本字段中显示新值。

btnDoIt.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        double a;
        a = Double.parseDouble(txtA.getText());
        txtOutput.setText("The root(s) of the equation is(are): " + a);
        double b;

            }

1 个答案:

答案 0 :(得分:0)

添加条件检查,并记得清楚问题。以下代码可以帮助您

btnDoIt.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        //get text from first 3 textfields
        String s1 = txtA.getText();
        String s2 = txtB.getText();
        String s3 = txtC.getText();
        //make sure strings aren't empty
        if(s1.isEmpty() || s2.isEmpty() || s3.isEmpty()){
            return;
        }
        //set text of final textfield
        txtOutput.setText(/*Your operation here*/);
    }
});