实验十一

Posted hhyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验十一相关的知识,希望对你有一定的参考价值。

package 实验十;
 
import java.awt.*;
import java.awt.event.*;
public class Jisuanqi 
    public static void main(String[] args)
        new MyFrame("计算器");
    

class MyFrame extends Frame
    private static final long serialVersionUID = 1L;
    private TextArea ta;
    public MyFrame(String title)
        super(title);
        SetTextAreas();
        SetButtonArea();
        SetMainFram();
    
    private void SetButtonArea() 
        addButton("7",33,28,20,128);
        addButton("8",33,28,58,128);
        addButton("9",33,28,96,128);
        addButton("/",33,28,134,128);
        addButton("%",33,28,172,128);
        addButton("4",33,28,20,160);
        addButton("5",33,28,58,160);
        addButton("6",33,28,96,160);
        addButton("*",33,28,134,160);
        addButton("1/x",33,28,172,160);
        addButton("1",33,28,20,192);
        addButton("2",33,28,58,192);
        addButton("3",33,28,96,192);
        addButton("-",33,28,134,192);
        addButton("0",71,28,20,224);
        addButton(".",33,28,96,224);
        addButton("+",33,28,134,224);
        addButton("=",33,60,172,192);
    
    double m,n;
    String k;
    boolean flag =true;
    boolean flag2 =false;
    private void addButton(String string, int i, int j,int x,int y) 
        final Button b = new Button(string);
        b.setLocation(x, y);
        b.setSize(i, j);
        b.setFont(new Font("标楷体", Font.BOLD, 15));
        b.setBackground(Color.pink);
        b.setForeground(Color.darkGray);
        b.addMouseListener(new MouseAdapter() 
            @Override
            public void mousePressed(MouseEvent e) 
                counts();
            
            private void counts() 
                if(ta.getText().equals("")&&(b.getActionCommand().equals("+")||
                        b.getActionCommand().equals("-")||
                        b.getActionCommand().equals("*")||
                        b.getActionCommand().equals("/")||
                        b.getActionCommand().equals("%")||
                        b.getActionCommand().equals("1/x")||
                        b.getActionCommand().equals("="))) 
                else if(ta.getText().equals(".")&&(b.getActionCommand().equals("+")||
                        b.getActionCommand().equals("-")||
                        b.getActionCommand().equals("*")||
                        b.getActionCommand().equals("/")||
                        b.getActionCommand().equals("%")||
                        b.getActionCommand().equals("1/x")||
                        b.getActionCommand().equals("=")))
                else 
                    if( b.getActionCommand().equals("+")||
                            b.getActionCommand().equals("-")||
                            b.getActionCommand().equals("*")||
                            b.getActionCommand().equals("%")||
                            b.getActionCommand().equals("1/x")||
                            b.getActionCommand().equals("/"))
                        if(flag2 = true) 
                            flag2 = false;
                        
                        if(flag) 
                            n = new Double(ta.getText()).doubleValue();
                            flag = false;
                        
                        else 
                            if(k=="=")
                            else 
                                m = new Double(ta.getText()).doubleValue();
                                if(k == "-") 
                                    if(n==0)
                                        n = m;
                                    else
                                        n=n-m;
                                
                                else if(k == "+") 
                                    if(n==0)
                                        n = m;
                                    else
                                        n=n+m;
                                    else if(k == "*") 
                                        if(n==0)
                                            n = m;
                                        else
                                            n=n*m;
                                    
                                    else if(k=="%")
                                        if(n==0)
                                            n=m;
                                        else
                                            m=m*100;
                                    
                                    else if(k=="1/x")
                                        if(n==0)
                                            n=m;
                                        else
                                            n=1/m;
                                    
                                    else if(k == "/") 
                                        if(n==0)
                                            n = m;
                                        else
                                            n=n/m;
                                    
                            
                        
                        k = b.getActionCommand();
                        ta.setText("");
                    
                    else if(b.getActionCommand().equals("=")) 
                        m = new Double(ta.getText()).doubleValue();
                        if(k == "+") 
                            ta.setText("");
                            ta.append(n+"+"+m);
                            ta.append(System.getProperty("line.separator"));
                            n = n+m;
                            ta.append("="+n);
                        
                        else if(k == "-") 
                            ta.setText("");
                            ta.append(n+"-"+m);
                            ta.append(System.getProperty("line.separator"));
                            n = n-m;
                            ta.append("="+n);
                        
                        else if(k == "*") 
                            ta.setText("");
                            ta.append(n+"*"+m);
                            ta.append(System.getProperty("line.separator"));
                            n = n*m;
                            ta.append("="+n);
                        
                        else if(k == "%") 
                            ta.setText("");
                            ta.append("%"+m);
                            ta.append(System.getProperty("line.separator"));
                            m = m*100;
                            ta.append("="+m+"%");
                        
                        else if(k == "1/x") 
                            ta.setText("");
                            ta.append(1+"/"+m);
                            ta.append(System.getProperty("line.separator"));
                            n = 1/m;
                            ta.append("="+n);
                        
                        else if(k == "/") 
                            ta.setText("");
                            ta.append(n+"/"+m);
                            ta.append(System.getProperty("line.separator"));
                            n= n/m;
                            ta.append("="+n);
                        
                        k="=";
                        flag2 = true;
                    
                    else 
                        if(flag2) 
                            flag = true;
                            flag2 = false;
                            ta.setText("");
                            m = n =0;
                        
                        ta.append(b.getActionCommand());
                    
                
            
        );
        this.add(b);
    
    private void SetTextAreas() 
        ta = new TextArea("0",8,52,3);
        ta.setBackground(Color.lightGray);
        ta.setSize(190, 50);
        ta.setFont(new Font("标楷体", Font.BOLD, 15));
        ta.setLocation(20,60);
        this.add(ta);
        this.add(ta);
    
    @SuppressWarnings("deprecation")
    private void SetMainFram() 
        this.setLayout(null);
        this.setSize(220,270);
        this.setVisible(true);
        this.setLocation(310, 340);
        this.setResizable(false);
        ta.setEditable(false);
        this.setCursor(Cursor.HAND_CURSOR);
        this.addWindowListener(new WindowAdapter() 
            public void windowClosing(WindowEvent e) 
                System.exit(0);
            
        );
    

技术图片技术图片

以上是关于实验十一的主要内容,如果未能解决你的问题,请参考以下文章

JSP 设计教师与学生不同登陆界面(带验证码)

汇编实验五

开发工具 | 即将jupyter的新一代notebook

汇编实验9

iPhone一代十年后再开箱:比X爽多了

第十一代雅阁的雏形?本田发布可实现自动驾驶的下一代雅阁原型车!