java简单计算器

来源:互联网 发布:网上真实赌博软件 编辑:程序博客网 时间:2024/06/18 07:04

java简单计算器

package dianzijisuanqi;import java.awt.BorderLayout;import java.awt.Color;import java.awt.EventQueue;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.UIManager;import javax.swing.border.EmptyBorder;import javax.swing.plaf.nimbus.NimbusLookAndFeel;public class MainFrame extends JFrame implements ActionListener{private JPanel contentPane;private static final String[] KEYS={"CE","C","BACK","%","7","8","9","/","4","5","6","*","1","2","3","+","0",".","=","-"};private JButton[] buttons=new JButton [KEYS.length];private boolean firstNum=true;private double result;private String operator="=";private boolean operatorFlag=false;private JTextField resultText;/** * Launch the application. */public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {UIManager.setLookAndFeel(new NimbusLookAndFeel());MainFrame frame = new MainFrame();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the frame. */public MainFrame() {super();setResizable(false);setTitle("简易计算器hwn");getContentPane().setLayout(new BorderLayout());setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(500, 50, 280, 230);resultText=new JTextField();resultText.setHorizontalAlignment(SwingConstants.RIGHT);resultText.setText("0");resultText.setEditable(false);getContentPane().add(resultText,BorderLayout.NORTH);JPanel calcPanel=new JPanel();getContentPane().add(calcPanel,BorderLayout.CENTER);final GridLayout gridLayout=new GridLayout(5,4,3,3);calcPanel.setLayout(gridLayout);for(int i = 0; i <KEYS.length; i++){buttons[i]=new JButton(KEYS[i]);buttons[i].addActionListener(this);calcPanel.add(buttons[i]);}buttons[3].setForeground(Color.RED);buttons[7].setForeground(Color.RED);buttons[11].setForeground(Color.RED);buttons[15].setForeground(Color.RED);buttons[19].setForeground(Color.RED);}@Overridepublic void actionPerformed(ActionEvent e) {String command = e.getActionCommand();if("0123456789.".indexOf(command)!=-1){numberKey(command);}else if("C".equals(command)){resultText.setText("0");firstNum=true;result=0d;operatorFlag=false;operator="=";}else if("BACK".equals(command)){backPress(command);}else{operatorPress(command);}}private void operatorPress(String command) {// TODO Auto-generated method stubif("+".equals(operator)){result+=getNumber();}else if("-".equals(operator)){result-=getNumber();}else if("*".equals(operator)){result*=getNumber();}else if("/".equals(operator)){result/=getNumber();}else if("%".equals(operator)){result/=100;}else if("=".equals(operator)){result=getNumber();}if (operatorFlag){long l=(long)result;double temp=result-l;if(temp==0){resultText.setText(String.valueOf(l)); }else{resultText.setText(String.valueOf(result));}}operatorFlag=true;operator =command;firstNum=true;}private double getNumber() {// TODO Auto-generated method stubdouble result=0;try{result=Double.valueOf(resultText.getText()).doubleValue();}catch(NumberFormatException e){e.printStackTrace();}return result;}private void backPress(String command) {// TODO Auto-generated method stubString temp=resultText.getText();if(temp.length()>0){temp=temp.substring(0, temp.length()-1);if(temp.length()==0){temp="0";firstNum=true;operator="=";}}resultText.setText(temp);}private void numberKey(String command) {// TODO Auto-generated method stubif(firstNum){resultText.setText(command);firstNum=false;}else if(".".equals(command)&&resultText.getText().indexOf(".")==-1){String temp=resultText.getText();resultText.setText(temp+".");}else if(!".".equals(command)){String temp=resultText.getText();resultText.setText(temp+command);}}}


0 0