yzh14249040 2015-08-19 02:56 采纳率: 0%
浏览 4215
已结题

java简单计算器,清除,回退和等号功能

图片说明界面已经有了。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.jar.JarFile;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalculatorJFrame implements ActionListener
{

private final String[] str = 
{    "7", "8", "9", "/",
     "4", "5", "6", "*",
     "1", "2", "3", "-", 
     ".", "0", "=", "+" 
};

private JFrame frame;
private JButton[ ] buttons;
private JButton reset, back;
private JTextField display;
private JPanel panel1, panel2,panel3,panel4,panel5;
private JLabel label;

/**
 * @param args
 */
public static void main(String[] args) 
{
    // TODO Auto-generated method stub
    CalculatorJFrame that = new CalculatorJFrame();
    that.Layout();

}

public void Layout() 
{
    frame = new JFrame("Calculator");
    Container contentpane = frame.getContentPane();
    frame.setLayout(new BorderLayout());

    label=new JLabel("計算器");
    label.setForeground(Color.BLACK);
    label.setHorizontalAlignment(label.CENTER);

    display = new JTextField("0");
    display.setHorizontalAlignment(JTextField.RIGHT);

    reset=new JButton("C");
    back=new JButton("back");

    panel3 = new JPanel(new GridLayout(4, 4));
    buttons = new JButton[str.length];
    for (int i = 0; i < str.length; i++) 
    {
        buttons[i] = new JButton(str[i]);
        panel3.add(buttons[i]);
        buttons[i].addActionListener(this);
    }

    panel1=new JPanel(new BorderLayout());
    panel1.add("North",label);
    panel1.add("South",display);

    panel2=new JPanel(new GridLayout(1,2));
    panel2.add(reset);
    panel2.add(back);

    panel4=new JPanel(new BorderLayout());
    panel4.add("North",panel2);
    panel4.add("Center",panel3);

    contentpane.add("North",panel1);
    contentpane.add("Center",panel4);

    frame.setSize(200, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

public void actionPerformed(ActionEvent e) 
{
    Object target = e.getSource();
    String label = e.getActionCommand();
    check(label);

}



public void check(String label)
{
    if ("0123456789".indexOf(label) >= 0)
    {
        if(display.getText().equals("0"))
        {
            display.setText(" ");
            show(label);
        }
        else
        {
            show(label);
        }

    }
    else if(".+-*/".indexOf(label) >= 0)
    {
        if(display.getText().indexOf(".") < 0)
        {
            show(label);
        }
    }
}
public void show(String label) 
{
    String s=display.getText();
    if(s.equals("0"))
    {

    }
    display.setText(s+label);

}

}

  • 写回答

1条回答 默认 最新

  • 纵横车 2015-08-19 04:40
    关注
     public class CalculatorJFrame implements ActionListener {
        private final String[] str = 
        {    "7", "8", "9", "/",
             "4", "5", "6", "*",
             "1", "2", "3", "-", 
             ".", "0", "=", "+" 
        };
        private String optition;
        private JFrame frame;
        private JButton[ ] buttons;
        private JButton reset, back;
        private JTextField display;
        private JPanel panel1, panel2,panel3,panel4,panel5;
        private JLabel label;
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            CalculatorJFrame that = new CalculatorJFrame();
            that.Layout();
    
        }
    
        public void Layout() 
        {
            frame = new JFrame("Calculator");
            Container contentpane = frame.getContentPane();
            frame.setLayout(new BorderLayout());
    
            label=new JLabel("計算器");
            label.setForeground(Color.BLACK);
            label.setHorizontalAlignment(label.CENTER);
    
            display = new JTextField("0");
            display.setHorizontalAlignment(JTextField.RIGHT);
    
            reset=new JButton("C");
            back=new JButton("back");
    
            reset.addActionListener(this);
            back.addActionListener(this);
    
            panel3 = new JPanel(new GridLayout(4, 4));
            buttons = new JButton[str.length];
            for (int i = 0; i < str.length; i++) 
            {
                buttons[i] = new JButton(str[i]);
                panel3.add(buttons[i]);
                buttons[i].addActionListener(this);
            }
    
            panel1=new JPanel(new BorderLayout());
            panel1.add("North",label);
            panel1.add("South",display);
    
            panel2=new JPanel(new GridLayout(1,2));
            panel2.add(reset);
            panel2.add(back);
    
            panel4=new JPanel(new BorderLayout());
            panel4.add("North",panel2);
            panel4.add("Center",panel3);
    
            contentpane.add("North",panel1);
            contentpane.add("Center",panel4);
    
            frame.setSize(200, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
    
        }
    
        public void actionPerformed(ActionEvent e) 
        {
            Object target = e.getSource();
            String label = e.getActionCommand();
            check(label);
    
        }
    
    
    
        public void check(String label)
        {
            if ("0123456789".indexOf(label) >= 0)
            {
                if(display.getText().equals("0"))
                {
                    display.setText(" ");
                    show(label);
                }
                else
                {
                    show(label);
                }
    
            }
            else if(".+-*/".indexOf(label) >= 0){
                show(label);
                if(!label.equals("."))
                {
                    optition = label;
                }
    
            }
            else if(label.equals("C")){
                display.setText("0");
            }
            else if(label.equals("back")){
                display.setText(display.getText().substring(0, display.getText().length()-1));
            }
            else if(label.equals("=")){
                int a,b;
                display.setText(display.getText().substring(1, display.getText().length())); 
                String[] str = display.getText().split("\\"+optition);
                a = Integer.parseInt(str[0]); 
                b = Integer.parseInt(str[1]);
                if(optition.equals("+"))
                    display.setText(a+b+"");
                else if(optition.equals("-"))
                    display.setText(a-b+"");
                else if(optition.equals("*"))
                    display.setText(a*b+"");
                else if(optition.equals("/"))
                    display.setText(a/b+"");
    
            }
        }
        public void show(String label) 
        {
            String s=display.getText();
            display.setText(s+label);
    
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并