JavaSwing图形界面编程之简易计算器(三)


package three.day.frame;



import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class Calc3  extends JFrame
{

private JLabel lbNum1;
private JLabel lbNum2;
private JLabel lbEquals;
private JTextField tfNum1;
private JTextField tfNum2;
private JTextField tfResult;
private JComboBox cbCalc;
private JButton btCalc;
private JButton btClear;
private JButton btExit;
private double result=0;
private int index = 0;
private double num1;
private double num2;

public Calc3(String title) {
super(title);
lbNum1 = new JLabel("第一个数");
lbNum2 = new JLabel("第二个数");
lbEquals = new JLabel("=");

tfNum1 = new JTextField(10);
tfNum2 = new JTextField(10);
tfResult = new JTextField(10);
tfResult.setBackground(Color.cyan);

String[] str = {"+","-","x","/"};
cbCalc = new JComboBox(str);
cbCalc.addItemListener(new ItemListener(){


@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
index = cbCalc.getSelectedIndex();
}

});

btCalc = new JButton("计算");
btCalc.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

String str1 = tfNum1.getText();
String str2 = tfNum2.getText();
if(str1.equals("")||str2.equals("")){
if(str1.equals("")){
JOptionPane.showMessageDialog(Calc3.this, "第一个数为空,请重新输入", "警告", JOptionPane.WARNING_MESSAGE);
return;
}
if(str2.equals("")){
JOptionPane.showMessageDialog(Calc3.this, "第二个数为空,请重新输入", "警告", JOptionPane.WARNING_MESSAGE);
return;
}
}

try {
num1 = Double.parseDouble(str1);
num2 = Double.parseDouble(str2);
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
tfNum1.setText("");
tfNum2.setText("");
tfResult.setText("");
JOptionPane.showMessageDialog(Calc3.this, "输入不正确,请核对后重新输入", "警告", JOptionPane.WARNING_MESSAGE);
e1.printStackTrace();
}

if(0==index){
result = num1 + num2;
}
else if(1==index){
result = num1 - num2;
}
else if(2==index){
result = num1 * num2;
}
else if(3==index){
result = num1 / num2;
}
tfResult.setText(String.valueOf(result));
}

});
btClear = new JButton("清除");
btClear.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tfNum1.setText("");
tfNum2.setText("");
tfResult.setText("");
}

});
btExit = new JButton("退出");
btExit.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}

});

Container cp = this.getContentPane();
cp.setLayout(new FlowLayout());

cp.add(lbNum1);
cp.add(tfNum1);
cp.add(cbCalc);
cp.add(lbNum2);
cp.add(tfNum2);
cp.add(lbEquals);
cp.add(tfResult);
cp.add(btCalc);
cp.add(btClear);
cp.add(btExit);

setSize(550,100);
setVisible(true);
}
public static void main(String[] args){
new Calc3("我的计算机器");
}


}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值