计算器制作JAVA版(第三步,表达式求值(+
2017-12-31
1.首先思考一下制作计算器需要哪些Swing组件,下面列出一些制作计算器的一些常用组件:
JFrame Jpanel JButton JTextField
2.选用布局管理器:
这里采用的是GridBagLayout,即网格包布局管理器。
3.如何处理按钮事件:
这里可以分两种情况来考虑,一:处理0~9的数字按钮和"."按钮,这种按钮的单击事件很简单,只需要获取监听的对象,并在文本框中显示对象的数据即可。
二:处理操作按钮即文本为+,-,*,/,C(清除),D(删除)和=的按钮,这些按钮的事件处理稍微复杂。
4.具体实现代码如下:
1.创建Calculator类,该类继承自JFrame类
[java] view plain copy
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField jtf=new JTextField();
private JPanel panel=new JPanel();
private GridBagLayout gridbaglayout=new GridBagLayout();
private GridBagConstraints constraints=new GridBagConstraints();
private int select=0;
// private String command="";
private double temp=0.0;
private double number;
public Calculator(){ //创建Calculator类的构造方法
this.setTitle("计算器");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(jtf,BorderLayout.NORTH);
this.add(panel, BorderLayout.CENTER);
panel.setLayout(gridbaglayout); //将panel面板设置成网格包布局管理器
this.add(panel);
jtf.setHorizontalAlignment(JTextField.RIGHT); //文本靠右
ActionListener nums=new CalculatorAction1();
ActionListener opin=new CalculatorAction2();
constraints.fill=GridBagConstraints.BOTH;
// constraints.weightx=0.1;
// constraints.weighty=0.1;
constraints.gridheight=GridBagConstraints.REMAINDER;
addButton("",nums);
constraints.gridheight=1;
addButton("C",opin);
addButton("7",nums);
addButton("4",nums);
addButton("1",nums);
constraints.gridheight=GridBagConstraints.REMAINDER;
addButton("%",nums);
constraints.gridheight=1;
addButton("D",opin);
addButton("8",nums);
addButton("5",nums);
addButton("2",nums);
constraints.gridheight=GridBagConstraints.REMAINDER;
addButton("0",nums);
constraints.gridheight=1;
addButton("/",opin);
addButton("9",nums);
addButton("6",nums);
addButton("3",nums);
constraints.gridheight=GridBagConstraints.REMAINDER;
addButton(".",nums);
constraints.gridheight=1;
addButton("*",opin);
addButton("-",opin);
addButton("+",opin);
constraints.gridheight=GridBagConstraints.REMAINDER;
addButton("=",opin);
}
private void addButton(String str,ActionListener list){ //添加按钮方法
JButton button=new JButton(str);
if(str.equals("")||str.equals("%")){
button.setEnabled(false);
}
button.addActionListener(list); //向按钮添加事件
gridbaglayout.setConstraints(button, constraints);
panel.add(button); //将按钮添加到JPanel容器中
}
class CalculatorAction1 implements ActionListener{ //创建内部类并实现ActionListener接口,实现数字按钮的监听事件
public void actionPerformed(ActionEvent e){
String input = e.getActionCommand();
// System.out.println(input);
jtf.setText(jtf.getText()+input); //在文本中显示用户单击的按钮文本内容
}
}
class CalculatorAction2 implements ActionListener{ //创建内部类并实现ActionListener接口,实现操作按钮的监听事件
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
if(command.equals("+")){
temp=Double.parseDouble(jtf.getText());
jtf.setText("");
select=1;
}
if(command.equals("-")){
temp=Double.parseDouble(jtf.getText());
jtf.setText("");
select=2;
}
if(command.equals("*")){
temp=Double.parseDouble(jtf.getText());
jtf.setText("");
select=3;
}
if(command.equals("/")){
temp=Double.parseDouble(jtf.getText());
jtf.setText("");
select=4;
}
if(command.equals("C")){
jtf.setText("");
}
if(command.equals("D")){
jtf.setText(jtf.getText().substring(0, jtf.getText().length()-1));
}
if(command.equals("=")){
if(select==1){
number=Double.parseDouble(jtf.getText());
jtf.setText((temp+number)+"");
}
else if(select==2){
number=Double.parseDouble(jtf.getText());
jtf.setText((temp-number)+"");
}
else if(select==3){
number=Double.parseDouble(jtf.getText());
jtf.setText((temp*number)+"");
}
else if(select==4){
number=Double.parseDouble(jtf.getText());
if(number==0){
JOptionPane.showMessageDialog(panel, "除数不能为0");
}
else
jtf.setText((temp/number)+"");
}
}
}
}
}
public class CalculatorTest {
public static void main(String[] args) {
Calculator mainFrame=new Calculator();
mainFrame.setBounds(300, 200, 200, 200);
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
}
2.创建CalculatorTest类
[java] view plain copy
阅读全文