简单的计算器
package start;
import util.Const;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Caculator 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 Caculator() throws HeadlessException{
this.init();
this.addNorthComponent();
this.addCentorButton();
}
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.FRAME_X,Const.FRAME_Y);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addNorthComponent(){
this.input_text.setPreferredSize(new Dimension(230,30));
jp_north.add(input_text);
jp_north.add(c_Btn);
this.c_Btn.setForeground(Color.RED);
c_Btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input_text.setText("");
}
});
this.add(jp_north,BorderLayout.NORTH);
}
public void addCentorButton(){
String btn_text="123+456-789*0.=/";
String regex="[\\+\\-*/.=]";
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.matches(regex)){
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) {
Caculator caculator = new Caculator();
caculator.setVisible(true);
}
private String firstInput=null;
private String operator=null;
@Override
public void actionPerformed(ActionEvent e) {
String clickStr=e.getActionCommand();
if(".0123456789".indexOf(clickStr)!=-1){
this.input_text.setText(input_text.getText()+clickStr);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
}else if(clickStr.matches("[\\+\\-*/]{1}")){
operator=clickStr;
firstInput=this.input_text.getText();
this.input_text.setText("");
}else if(clickStr.equals("=")){
Double a=Double.valueOf(firstInput);
Double b=Double.valueOf(this.input_text.getText());
Double result=null;
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());
}
}
}