前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java高扩展计算器

java高扩展计算器

作者头像
张泽旭
发布2018-12-10 16:47:35
2K0
发布2018-12-10 16:47:35
举报
文章被收录于专栏:张泽旭的专栏

次计算器可以随意增加按钮,且不影响其它的按钮,如需增加更多的计算按钮只需要修改Answer2.java中的部分代码即可:

代码语言:javascript
复制
switch(ch1)
        {
        case "=":
            
             num =  Double.parseDouble(ch[i-1]);
             zongnum=fflag(num,zongnum,flag);
             flag = 0;
            
             break;
        case "+":
            
             num =  Double.parseDouble(ch[i-1]);
            
             
             zongnum=fflag(num,zongnum,flag);
             flag = 1;
             
             break;
        case "-":
            num =  Double.parseDouble(ch[i-1]);
            
             
             zongnum=fflag(num,zongnum,flag);
            
             flag = 2;
            
             break;
        case "*":
                num =  Double.parseDouble(ch[i-1]);
            
             
             zongnum=fflag(num,zongnum,flag);
            
             flag = 3;
             
             break;
        case "/":
             num =  Double.parseDouble(ch[i-1]);
        
             zongnum=fflag(num,zongnum,flag);
             flag = 4;
            
             break;
        case "sqrt":
             num =  Double.parseDouble(ch[i-1]);
        
             zongnum=fflag(num,zongnum,flag);
             flag = 5;
            
             break;
        case"del":
            flag = 6;
            break;
//后面追加case 键值,并赋值标志变量,flag++;
            
        }
        
        
        
        
        }
        
    
        return zongnum;
    
        
    
    }
    private static double fflag(double num,double zongnum,int flag){
        switch(flag){
        
        case 0:
            zongnum=num;
            break;
        case 1:
            zongnum=zongnum+num;
            break;
        case 2:
            zongnum=zongnum-num;
            break;
        case 3:
            zongnum=zongnum*num;
            break;
        case 4:
            zongnum=zongnum/num;
            break;
        case 5:
            zongnum=Math.cbrt(zongnum);
            break;
        case 6:
            zongnum = 0;
//此处填写相应的计算代码,判断flag;
            
        break;
        }
        return zongnum;
        
        
    }

以下是源代码,有startFrame.java 和 Answer2.java 分别给出:

代码语言:javascript
复制
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.Vector;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class statrFrame extends JFrame implements ActionListener{
    private int x,y;
    private int width, height; 
    private JFrame jf;
    private JPanel contentPane;
    private JPanel panel,panel_1;
    private JTextField jt;
    private String str="",str1="",str2="";
    private String[] strindex;
    
    private Vector<JButton> bu = new Vector<>();
    public statrFrame(int width,int height,int x,int y,String str){
        
        this.strindex =str.split(",");
        this.str = str;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height=height;
        jf = new JFrame("计算机");
        panel = new JPanel();
        panel_1 = new JPanel();
        jt = new JTextField();
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jf.setContentPane(contentPane);
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(42)
                    .addComponent(panel, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                    .addGap(38))
                .addComponent(panel_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(panel, GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))
        );
        
        
        
        
        
        GridLayout gd = new GridLayout(x,y,0,0);
        panel_1.setLayout(gd);
        jt = new JTextField();
        jt.setColumns(10);
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addComponent(jt, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
        );
        gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addComponent(jt, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
        );
        panel.setLayout(gl_panel);
        contentPane.setLayout(gl_contentPane);
        if(y!=0){
            
            gd.setHgap(y);
            panel_1.setLayout(gd);
        }
            else
                panel_1.setLayout(new GridLayout());
        for(int i =0;i<strindex.length;i++){
            bu.add(new JButton(strindex[i]));
        }
        
    }
    public statrFrame(int width,int height,int x,String str){
        
        this.strindex =str.split(",");
        this.str = str;
        this.x = x;
        this.width = width;
        this.height=height;
        
        jf = new JFrame("计算机");
        panel = new JPanel();
        panel_1 = new JPanel();
        jt = new JTextField();
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jf.setContentPane(contentPane);
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(42)
                    .addComponent(panel, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                    .addGap(38))
                .addComponent(panel_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(panel, GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))
        );
        
        
        
        
        
        GridLayout gd = new GridLayout(x,y,0,0);
        panel_1.setLayout(gd);
        jt = new JTextField();
        jt.setColumns(10);
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addComponent(jt, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
        );
        gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addComponent(jt, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
        );
        panel.setLayout(gl_panel);
        contentPane.setLayout(gl_contentPane);
        if(x!=0){
            
            gd.setRows(x);
            panel_1.setLayout(gd);
        }
            else
                panel_1.setLayout(new GridLayout());
        for(int i =0;i<strindex.length;i++){
            bu.add(new JButton(strindex[i]));
            
        }
        
    }
    
    public void start(){
        for(JButton button:bu){
            JPanel panel_2= new JPanel();
            panel_2.setLayout(new FlowLayout());
            panel_1.add(panel_2);
//            button.setPreferredSize(new Dimension(130, 50));
            panel_2.add(button);
            button.setFont(new Font("微软雅黑", Font.PLAIN, 40));
            button.addActionListener(this);
            jt.setFont(new Font("宋体", Font.PLAIN, 30));
        }
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jf.setSize(width, height);
        jf.setVisible(true);
        new Adjust().start();
    }
    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                
                @Override
                public void run() {
                    statrFrame sf=new statrFrame(500,600,5,"7,8,9,/,4,5,6,*,1,2,3,-,0,.,+,=,sqrt,->,del,abs");
                    sf.start();
                    
                }
            });
        } catch (InvocationTargetException | InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jb =(JButton) e.getSource();
        
        
        
        if(jb.getText().equals("+")||jb.getText().equals("-")||jb.getText().equals("*")||jb.getText().equals("/")||jb.getText().equals("sqrt"))
        {
            str2=str2+",";
            str2=str2+jb.getText();
            str2=str2+",";
        
        str1=str1+jb.getText();
        
        
        }
        if (jb.getText().equals("="))
        {
            
            if(Answer2.suanzhi(str2) % 1.0 == 0){
                 str1 =Long.toString((long)Answer2.suanzhi(str2));
                }
            else{
                 str1 =Double.toString(Answer2.suanzhi(str2));
            }
            
            
        }
        
        if(jb.getText().equals("1")||jb.getText().equals("2")||jb.getText().equals("3")||jb.getText().equals("4")||jb.getText().equals("5")
                ||jb.getText().equals("6")||jb.getText().equals("7")||jb.getText().equals("8")||jb.getText().equals("9")
                ||jb.getText().equals("0")||jb.getText().equals("."))
        {
        
        str1=str1+jb.getText();
        
        str2=str2+jb.getText();
        }
        if (jb.getText().equals("del"))
        {
            str1 = "";
        }
        if(jb.getText().equals("->"))
        {
            str1 = str1.substring(0, str1.length()-1);
        }
        
        jt.setText(str1);
        
    }
    public  class Adjust extends Thread{
        @Override
        public void run() {
            while(true){
                jt.setPreferredSize(new Dimension(jf.getWidth()/6, jf.getHeight()/15));
            
                for(JButton button:bu){
                    
                    button.setPreferredSize(new Dimension(jf.getWidth()/7, jf.getHeight()/10));
                
            
                    
                }
            
                
            }
        }
    }
    

}
代码语言:javascript
复制
public class Answer2 {
    
    public static double  suanzhi(String str){
     double zongnum=0;
         double num=0;
         int index=0;
         int flag=0;
          String s1="";
          str=str+",=";
        
          String[] ch = str.split(",");
        
        for(int i=0;i<ch.length;i++){
            String ch1 = ch[i];
            
        switch(ch1)
        {
        case "=":
            
             num =  Double.parseDouble(ch[i-1]);
             zongnum=fflag(num,zongnum,flag);
             flag = 0;
            
             break;
        case "+":
            
             num =  Double.parseDouble(ch[i-1]);
            
             
             zongnum=fflag(num,zongnum,flag);
             flag = 1;
             
             break;
        case "-":
            num =  Double.parseDouble(ch[i-1]);
            
             
             zongnum=fflag(num,zongnum,flag);
            
             flag = 2;
            
             break;
        case "*":
                num =  Double.parseDouble(ch[i-1]);
            
             
             zongnum=fflag(num,zongnum,flag);
            
             flag = 3;
             
             break;
        case "/":
             num =  Double.parseDouble(ch[i-1]);
        
             zongnum=fflag(num,zongnum,flag);
             flag = 4;
            
             break;
        case "sqrt":
             num =  Double.parseDouble(ch[i-1]);
        
             zongnum=fflag(num,zongnum,flag);
             flag = 5;
            
             break;
        case"del":
            flag = 6;
            break;
            
        }
        
        
        
        
        }
        
    
        return zongnum;
    
        
    
    }
    private static double fflag(double num,double zongnum,int flag){
        switch(flag){
        
        case 0:
            zongnum=num;
            break;
        case 1:
            zongnum=zongnum+num;
            break;
        case 2:
            zongnum=zongnum-num;
            break;
        case 3:
            zongnum=zongnum*num;
            break;
        case 4:
            zongnum=zongnum/num;
            break;
        case 5:
            zongnum=Math.cbrt(zongnum);
            break;
        case 6:
            zongnum = 0;
            
        break;
        }
        return zongnum;
        
        
    }
    
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年10月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档