标签:temp text 计算器 new 简单 input btn public
简单的计算器
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);
}
/*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) {
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());
}
//JOptionPane.showMessageDialog(this,clickStr);//显示消息对话框
}
}
标签:temp,text,计算器,new,简单,input,btn,public 来源: https://www.cnblogs.com/wyx-5555/p/15969209.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。