java 中第三个文本框要实现前两个文本框的的计算结果(+-*/)应该怎样编程?
1个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestCount {
private JFrame f;
private JPanel p;
private JTextField textOne;
private JTextField textTwo;
private JTextField textResult;
private JComboBox comboBox;
private JButton button;
public TestCount() {
f = new JFrame();
p = new JPanel();
textOne = new JTextField(12);
textTwo = new JTextField(12);
textResult = new JTextField(12);
button = new JButton("=");
String sign[] = { "+", "-", "*", "/" };
comboBox = new JComboBox(sign);
f.setTitle("简单的数学计算");
f.setSize(600, 200);
p.add(textOne);
p.add(comboBox);
p.add(textTwo);
p.add(button);
p.add(textResult);
button.addActionListener(new ButtonListener());
f.getContentPane().add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int i = comboBox.getSelectedIndex();// 得到当前下拉列表框的值
String str1 = textOne.getText();// 得到第一个文本框的值
String str2 = textTwo.getText();// 得到第二个文本框的值
if (str1 != null && str2 != null) {
double numOne = Double.parseDouble(str1);
double numTwo = Double.parseDouble(str2);
double result = 0;
if (i == 0)
result = numOne + numTwo;
if (i == 1)
result = numOne - numTwo;
if (i == 2)
result = numOne * numTwo;
if (i == 3)
result = numOne / numTwo;
textResult.setText("" + result);// 将结果显示在第三个文本框中
}
}
}
public static void main(String[] args) {
new TestCount();
}
}
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestCount {
private JFrame f;
private JPanel p;
private JTextField textOne;
private JTextField textTwo;
private JTextField textResult;
private JComboBox comboBox;
private JButton button;
public TestCount() {
f = new JFrame();
p = new JPanel();
textOne = new JTextField(12);
textTwo = new JTextField(12);
textResult = new JTextField(12);
button = new JButton("=");
String sign[] = { "+", "-", "*", "/" };
comboBox = new JComboBox(sign);
f.setTitle("简单的数学计算");
f.setSize(600, 200);
p.add(textOne);
p.add(comboBox);
p.add(textTwo);
p.add(button);
p.add(textResult);
button.addActionListener(new ButtonListener());
f.getContentPane().add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int i = comboBox.getSelectedIndex();// 得到当前下拉列表框的值
String str1 = textOne.getText();// 得到第一个文本框的值
String str2 = textTwo.getText();// 得到第二个文本框的值
if (str1 != null && str2 != null) {
double numOne = Double.parseDouble(str1);
double numTwo = Double.parseDouble(str2);
double result = 0;
if (i == 0)
result = numOne + numTwo;
if (i == 1)
result = numOne - numTwo;
if (i == 2)
result = numOne * numTwo;
if (i == 3)
result = numOne / numTwo;
textResult.setText("" + result);// 将结果显示在第三个文本框中
}
}
}
public static void main(String[] args) {
new TestCount();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询