gpt4 book ai didi

java - Try and Catch 用于简单的计算器输入

转载 作者:行者123 更新时间:2023-12-02 05:42:11 25 4
gpt4 key购买 nike

这个用java编写的程序用作计算器。如果用户尝试从结果文本中输入,谁能告诉我如何实现 try/catch block ,以便我可以处理不正确的工作流程?

这是代码:

    package simplecal;

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

public class SimpleCal extends JFrame {

JTextField jftinput1, jftinput2, jftresult;
JButton jbtminus, jbtadd,jbtdivid, jbttimes;
final int JTEXT_SIZE = 5;
SimpleCal(){
setLayout(new FlowLayout());
jftinput1 = new JTextField(JTEXT_SIZE);
jftinput2 = new JTextField(JTEXT_SIZE);
jftresult = new JTextField(JTEXT_SIZE);
add(new JLabel("Input 1: "));
add(jftinput1);
add(new JLabel("Input 2: "));
add(jftinput2);
add(new JLabel("Result "));
add(jftresult);
JPanel p1 = new JPanel();

jbtminus = new JButton("Subtract");
jbtadd = new JButton("Add");
jbttimes = new JButton("Multiple");
jbtdivid = new JButton("Divided");

p1.add(jbtminus);
jbtminus.addActionListener(new ButtonListener1());
p1.add(jbtadd);
jbtadd.addActionListener(new ButtonListener2());
p1.add(jbttimes);
jbttimes.addActionListener(new ButtonListener3());
p1.add(jbtdivid);
jbtdivid.addActionListener(new ButtonListener4());
add(p1, BorderLayout.SOUTH);

}

class ButtonListener1 implements ActionListener{
public void actionPerformed(ActionEvent e){
double input1 = Double.parseDouble(jftinput1.getText());
double input2 = Double.parseDouble(jftinput2.getText());

double answer = input1 - input2;

jftresult.setText(Double.toString(answer));
}
}

class ButtonListener2 implements ActionListener{
public void actionPerformed(ActionEvent e){
double input1 = Double.parseDouble(jftinput1.getText());
double input2 = Double.parseDouble(jftinput2.getText());

double answer = input1 + input2;

jftresult.setText(Double.toString(answer));
}
}

class ButtonListener3 implements ActionListener{
public void actionPerformed(ActionEvent e){
double input1 = Double.parseDouble(jftinput1.getText());
double input2 = Double.parseDouble(jftinput2.getText());

double answer = input1 * input2;

jftresult.setText(Double.toString(answer));
}
}

class ButtonListener4 implements ActionListener{
public void actionPerformed(ActionEvent e){
double input1 = Double.parseDouble(jftinput1.getText());
double input2 = Double.parseDouble(jftinput2.getText());

double answer = input1 / input2;

jftresult.setText(Double.toString(answer));
}
}

public static void main(String[] args) {
SimpleCal frame = new SimpleCal();
frame.setTitle("Simple Cal");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(450,100);
}

}

最佳答案

您不需要为此使用异常处理程序。只需将该字段设置为不可编辑即可:

jftresult.setEditable(false);

参见http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setEditable(boolean) .

关于java - Try and Catch 用于简单的计算器输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24420789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com