【问题标题】:My JButton only does set.Text on my textfield if all of my 3 textfields have input如果我的所有 3 个文本字段都有输入,我的 JButton 只会在我的文本字段上设置.Text
【发布时间】:2013-03-13 15:56:23
【问题描述】:

每当我在计算器上单击 1 时,它都应该执行 t1.setText("1");但只有在我的 textfield1、textfield2、textfield3 上已经有文本并且如果没有它会产生错误时,它才会这样做,这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
GridLayout layout = new GridLayout(4, 2);
JLabel l1 = new JLabel("Number 1:");
JLabel l2 = new JLabel("Number 2:");
JLabel l3 = new JLabel("Answer:"); 
JTextField t1 = new JTextField(30);
JTextField t2 = new JTextField(30);
JTextField t3 = new JTextField(30);
JPanel botleft = new JPanel();
JPanel botright = new JPanel();
JButton clear = new JButton("Clear");
JButton n0 = new JButton("0");
JButton n1b = new JButton("1");
JButton n2 = new JButton("2");
JButton n3 = new JButton("3");
JButton n4 = new JButton("4");
JButton n5 = new JButton("5");
JButton n6 = new JButton("6");
JButton n7 = new JButton("7");
JButton n8 = new JButton("8");
JButton n9 = new JButton("9");
JButton add = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("*");
JButton div = new JButton("/");
JButton equals = new JButton("="); 
Float ans;

{
botleft.setLayout(new GridLayout(4,2));
botright.setLayout(new GridLayout(4,2));
botleft.add(n1b);
botleft.add(n2);
botright.add(n3);
botright.add(add);
botleft.add(n4);
botleft.add(n5);
botright.add(n6);
botright.add(sub);
botleft.add(n7);
botleft.add(n8);
botright.add(n9);
botright.add(mul);
botleft.add(n0);
botleft.add(clear);
botright.add(equals);
botright.add(div);
}

public Calculator()
{
super("Calculator");
setSize(300, 300);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(botleft);
add(botright);
setLayout(layout);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
n1b.addActionListener(this);
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
String n1 = t1.getText();
String n2 = t2.getText();
Float num1 = Float.parseFloat(n1);
Float num2 = Float.parseFloat(n2); 
Object clicked = e.getSource();

if (n1b == clicked)
{
t1.setText("1");
}

else if(add == clicked)
{
t3.setText(String.valueOf(num1+num2));
}
else if(sub == clicked)
{
t3.setText(String.valueOf(num1-num2));
}
else if(mul == clicked)
{
t3.setText(String.valueOf(num1*num2));
}
else
{
if(num2 == 0)
t3.setText("Can't Divide By Zero");
else
t3.setText(String.valueOf(num1/num2));
}
}
}

这是我的另一堂课:

public class CalcReader
{
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.setVisible(true);
}
}

【问题讨论】:

    标签: java swing user-interface jbutton textfield


    【解决方案1】:

    您正在解析JTextField t1 中的值获取ActionListener 中的源对象之前。如果此字段为空(或包含无效号码),您将获得NumberFormatException,然后才能拨打t1.setText("1")。尝试这样做首先

    Object clicked = e.getSource();
    if (n1b == clicked) {
       t1.setText("1");
    } else if // handle all other conditions...
    

    除此之外: 操作操作的性质(+- 等)对于每个JButton 都存在很大差异,因此在此处拥有自己的ActionListener,而不是试图破译点击了什么按钮。

    【讨论】:

    • 非常感谢,我已经放弃了我的解析,并分别在我的 add、sub、mul 和 div 中声明了它们。谢谢它现在可以工作了
    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2020-05-24
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多