package com.hellojava.practice.test; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Numerator<textFiled> extends JFrame implements ActionListener{ JFrame frame=new JFrame(); TextField textField; private String op1="0"; private boolean append=false; private String operator="+"; public Numerator(){ frame.setTitle("简单计算器"); frame.setLayout(new BorderLayout()); frame.setLocationRelativeTo(null); textField=new TextField(); Panel panel1=new Panel(); panel1.setLayout(new BorderLayout()); panel1.add(textField, BorderLayout.NORTH); frame.add(panel1, BorderLayout.NORTH); JButton btn0=new JButton("0"); btn0.addActionListener(this); JButton btn1=new JButton("1"); btn1.addActionListener(this); JButton btn2=new JButton("2"); btn2.addActionListener(this); JButton btn3=new JButton("3"); btn3.addActionListener(this); JButton btn4=new JButton("4"); btn4.addActionListener(this); JButton btn5=new JButton("5"); btn5.addActionListener(this); JButton btn6=new JButton("6"); btn6.addActionListener(this); JButton btn7=new JButton("7"); btn7.addActionListener(this); JButton btn9=new JButton("9"); btn9.addActionListener(this); JButton btn8=new JButton("8"); btn8.addActionListener(this); JButton btna=new JButton("+"); btna.addActionListener(this); JButton btnb=new JButton("-"); btnb.addActionListener(this); JButton btnc=new JButton("*"); btnc.addActionListener(this); JButton btnd=new JButton("/"); btnd.addActionListener(this); JButton btne=new JButton("="); btne.addActionListener(this); JPanel panel2=new JPanel(); panel2.setLayout(new GridLayout(4, 4)); panel2.add(btn0); panel2.add(btn1); panel2.add(btn2); panel2.add(btn3); panel2.add(btn4); panel2.add(btn5); panel2.add(btn6); panel2.add(btn7); panel2.add(btn8); panel2.add(btn9); panel2.add(btna); panel2.add(btnb); frame.add(panel2,BorderLayout.CENTER); JPanel panel3=new JPanel(); panel3.add(btnc); panel3.add(btnd); panel3.add(btne); panel3.setLayout(new GridLayout(1, 3)); frame.add(panel3, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new Numerator(); } @Override public void actionPerformed(ActionEvent e) { String comm=e.getActionCommand(); if("0123456789".indexOf(comm)!=-1){ if(append){ String temp=textField.getText(); textField.setText(temp+comm); }else{ textField.setText(comm); append=true; } } else if("+-*/".indexOf(comm)!=-1){ op1=textField.getText(); textField.setText(comm); operator=comm; append=false; } else if("=".equals(comm)){ String op2=textField.getText(); double d1=Double.parseDouble(op1); double d2=Double.parseDouble(op2); if(operator.equals("+")){ d1=d1+d2; }else if(operator.equals("-")){ d1=d1-d2; }else if(operator.equals("*")){ d1=d1*d2; }else{ d1=d1/d2; } String d3=d1+""; textField.setText(d3); append=false; } else if(".".equals(comm)){ String temp=textField.getText(); if(temp.indexOf(".")==-1){ textField.setText(temp+"."); append=true; } } } }
时间: 2024-12-26 21:39:17