1,项目概述
1.1项目目标和主要内容
1) 能通过设计的按钮控件输入并 实现算术 表达式 ,表达式 在文本框中 显示
2) 能够检验算术表达式的合法性;
3) 能够 实现混合运算 的求解 ,算术表达式中包括加、减、乘、除等运算符 ;
4) 要求交互界面友好,程序健壮;
5)使用Java编程语言,在IntelliJ IDEA Community Edition上进行开发。
6)学习并实践JavaGUI编程和frame框架
1.2项目的主要功能
基本的加,减,乘,除,四则运算
package start;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Carculator extends JFrame implements ActionListener {
/* 上方控件 */
private JPanel jp_north=new JPanel();
private JTextField input_text=new JTextField();
private JButton c_Btn=new JButton("C");
/* 中间控件 */
private JPanel jp_center=new JPanel();
public Carculator() throws HeadlessException {
this.init();
this.addNorthComponent();
this.addCenterComponent();
}
//初始化方法
public void init(){
this.setTitle(Const.Title);
this.setSize(Const.FRAME_W,Const.FRAME_H);
this.setLayout(new BorderLayout());
this.setResizable(false);
this.setLocation(Const.Fame_x,Const.Fame_y);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//上方控件方法
public void addNorthComponent(){
this.input_text.setPreferredSize(new Dimension(230,30));
jp_north.add(input_text);
this.c_Btn.setForeground(Color.RED);
c_Btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input_text.setText("");
}
});
jp_north.add(c_Btn);
this.add(jp_north,BorderLayout.NORTH);
}
//中间控件方法
public void addCenterComponent(){
String btn_Text="123+456-789*0.=/";
this.jp_center.setLayout(new GridLayout(4,4));
for (int i=0;i<16;i++){
String temp=btn_Text.substring(i,i+1);
JButton btn=new JButton();
btn.setText(temp);
if (temp.equals("+")||
temp.equals("-")||
temp.equals("*")||
temp.equals("/")||
temp.equals(".")||
temp.equals("=")){
btn.setFont(new Font("粗体",Font.BOLD,16));
btn.setForeground(Color.RED);
}
btn.addActionListener(this);
jp_center.add(btn);
}
this.add(jp_center,BorderLayout.CENTER);
}
public static void main(String[] args){
Carculator carlacutor=new Carculator();
carlacutor.setVisible(true);
}
private String firstInput=null;
private String operator=null;
@Override
public void actionPerformed(ActionEvent e) {
String clicker=e.getActionCommand();
if ("-.1234567890".indexOf
)!=-1) {
this.input_text.setText(input_text.getText()+clicker);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
//JOptionPane.showMessageDialog(this, clicker);
}else if (clicker.matches("[\\+\\-*/]{1}")){
firstInput=this.input_text.getText();
operator=clicker;
this.input_text.setText("");
}else if (clicker.equals("=")){
Double a=Double.valueOf(firstInput);
Double b=Double.valueOf(this.input_text.getText());
Double result=0.0;
switch (operator){
case "+":
result=a+b;
break;
case "-":
result=a-b;
break;
case "*":
result=a*b;
break;
case "/":
if (b!=0){
result=a/b;
break;
}
}this.input_text.setText(result.toString());
}
}
}
package start;
import java.awt.*;
public class Const {
public static final int FRAME_W=300;
public static final int FRAME_H=300;
public static final int SCREEN_W= Toolkit.getDefaultToolkit().getScreenSize().height;
public static final int SCREEN_H=Toolkit.getDefaultToolkit().getScreenSize().width;
public static final int Fame_x=(SCREEN_H-FRAME_H)/2;
public static final int Fame_y=(SCREEN_W-FRAME_W)/2;
public static final String Title="计算器";
}