分享一个计算器代码
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Calculator extends JFrame{
private JPanel pan1=new JPanel();
private JPanel pan2=new JPanel();
private JTextField tfResult=new JTextField();
private JButton btn0=new JButton();
private JButton btn1=new JButton();
private JButton btn2=new JButton();
private JButton btn3=new JButton();
private JButton btn4=new JButton();
private JButton btn5=new JButton();
private JButton btn6=new JButton();
private JButton btn7=new JButton();
private JButton btn8=new JButton();
private JButton btn9=new JButton();
private JButton addBtn=new JButton();
private JButton subtractBtn=new JButton();
private JButton multiBtn=new JButton();
private JButton divBtn=new JButton();
private JButton equalBtn=new JButton();
private JButton dotBtn=new JButton();
private GridLayout gridLayout1=new GridLayout();
private GridBagLayout gridBagLayout1=new GridBagLayout();
private GridBagLayout gridBagLayout2=new GridBagLayout();
private String recentOperation=null;
private String recentNum=null;
private boolean isNew=true;
public Calculator(){
try{
btn0.setFont(new java.awt.Font("SansSerif", 0, 12));
btn0.setForeground(Color.black);
btn0.setBorder(BorderFactory.createRaisedBevelBorder());
btn0.setText("0");
btn0.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(tfResult.getText().equals("0")) return;
if(!isNew){
tfResult.setText(tfResult.getText()+"0");
}else{
tfResult.setText("0");
}
isNew=false;
}
});
btn1.setFont(new java.awt.Font("SansSerif", 0, 12));
btn1.setForeground(Color.black);
btn1.setBorder(BorderFactory.createRaisedBevelBorder());
btn1.setText("1");
btn1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(!isNew){
tfResult.setText(tfResult.getText()+"1");
}else{
tfResult.setText("1");
}
isNew=false;
}
});
btn2.setFont(new java.awt.Font("SansSerif", 0, 12));
btn2.setForeground(Color.black);
btn2.setBorder(BorderFactory.createRaisedBevelBorder());
btn2.setText("2");
btn2.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(!isNew){
tfResult.setText(tfResult.getText()+"2");
}else{
tfResult.setText("2");
}
isNew=false;
}
});
btn3.setFont(new java.awt.Font("SansSerif", 0, 12));
btn3.setForeground(Color.black);
btn3.setBorder(BorderFactory.createRaisedBevelBorder());
btn3.setText("3");
btn3.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(!isNew){
tfResult.setText(tfResult.getText()+"3");
}else{
tfResult.setText("3");
}
isNew=false;
}
});
btn4.setFont(new java.awt.Font("SansSerif", 0, 12));
btn4.setForeground(Color.black);
btn4.setBorder(BorderFactory.createRaisedBevelBorder());
btn4.setText("4");
btn4.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(!isNew){
tfResult.setText(tfResult.getText()+"4");
}else{
tfResult.setText("4");
}
isNew=false;
}
});
btn5.setFont(new java.awt.Font("SansSerif", 0, 12));
btn5.setForeground(Color.black);
btn5.setBorder(BorderFactory.createRaisedBevelBorder());
btn5.setText("5");
btn5.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(!isNew){
tfResult.setText(tfResult.getText()+"5");
}else{
tfResult.setText("5");
}
isNew=false;
}
});
btn6.setFont(new java.awt.Font("SansSerif", 0, 12));
btn6.setForeground(Color.black);
btn6.setBorder(BorderFactory.createRaisedBevelBorder());
btn6.setText("6");
btn6.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(!isNew){
tfResult.setText(tfResult.getText()+"6");
}else{
tfResult.setText("6");
}
isNew=false;
}
});
btn7.setFont(new java.awt.Font("SansSerif", 0, 12));
btn7.setForeground(Color.black);
btn7.setBorder(BorderFactory.createRaisedBevelBorder());
btn7.setText("7");
btn7.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(!isNew){
tfResult.setText(tfResult.getText()+"7");
}else{
tfResult.setText("7");
}
isNew=false;
}
});
btn8.setFont(new java.awt.Font("SansSerif", 0, 12));
btn8.setForeground(Color.black);
btn8.setBorder(BorderFactory.createRaisedBevelBorder());
btn8.setText("8");
btn8.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(!isNew){
tfResult.setText(tfResult.getText()+"8");
}else{
tfResult.setText("8");
}
isNew=false;
}
});
btn9.setFont(new java.awt.Font("SansSerif", 0, 12));
btn9.setForeground(Color.black);
btn9.setBorder(BorderFactory.createRaisedBevelBorder());
btn9.setText("9");
btn9.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(!isNew){
tfResult.setText(tfResult.getText()+"9");
}else{
tfResult.setText("9");
}
isNew=false;
}
});
addBtn.setFont(new java.awt.Font("SansSerif", 0, 12));
addBtn.setForeground(Color.black);
addBtn.setBorder(BorderFactory.createRaisedBevelBorder());
addBtn.setText("+");
addBtn.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(tfResult.getText().equals("")) return;
if(recentOperation!=null) equalaction(e);
recentOperation="add";
recentNum=tfResult.getText();
isNew=true;
}
});
subtractBtn.setFont(new java.awt.Font("SansSerif", 0, 12));
subtractBtn.setForeground(Color.black);
subtractBtn.setBorder(BorderFactory.createRaisedBevelBorder());
subtractBtn.setText("—");
subtractBtn.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(tfResult.getText().equals("")) return;
if(recentOperation!=null) equalaction(e);
recentOperation="subtract";
recentNum=tfResult.getText();
isNew=true;
}
});
multiBtn.setFont(new java.awt.Font("SansSerif", 0, 12));
multiBtn.setForeground(Color.black);
multiBtn.setBorder(BorderFactory.createRaisedBevelBorder());
multiBtn.setText("x");
multiBtn.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(tfResult.getText().equals("")) return;
if(recentOperation!=null) equalaction(e);
recentOperation="multiply";
recentNum=tfResult.getText();
isNew=true;
}
});
divBtn.setFont(new java.awt.Font("SansSerif", 0, 12));
divBtn.setForeground(Color.black);
divBtn.setBorder(BorderFactory.createRaisedBevelBorder());
divBtn.setText("/");
divBtn.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(tfResult.getText().equals("")) return;
if(recentOperation!=null) equalaction(e);
recentOperation="divide";
recentNum=tfResult.getText();
isNew=true;
}
});
equalBtn.setFont(new java.awt.Font("SansSerif", 0, 12));
equalBtn.setForeground(Color.black);
equalBtn.setBorder(BorderFactory.createRaisedBevelBorder());
equalBtn.setText("=");
equalBtn.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
equalaction(e);
recentOperation=null;
recentNum=tfResult.getText();
isNew=true;
}
});
dotBtn.setFont(new java.awt.Font("SansSerif", 0, 12));
dotBtn.setForeground(Color.black);
dotBtn.setBorder(BorderFactory.createRaisedBevelBorder());
dotBtn.setText(".");
dotBtn.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
if(tfResult.getText().indexOf(".")!=-1) return;
if(!isNew&&tfResult.getText()!=""){
tfResult.setText(tfResult.getText()+".");
}else{
tfResult.setText("0.");
}
isNew=false;
}
});
tfResult.setBorder(BorderFactory.createLoweredBevelBorder());
tfResult.setDisabledTextColor(Color.white);
tfResult.setEditable(false);
tfResult.setText("0");
tfResult.setHorizontalAlignment(SwingConstants.RIGHT);
pan1.setLayout(gridBagLayout1);
pan1.setPreferredSize(new Dimension(333,30));
pan1.add(tfResult, new GridBagConstraints(1,0,1,1,0.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,15,0,0),155,0));
pan2.setBorder(BorderFactory.createRaisedBevelBorder());
gridLayout1.setColumns(6);
gridLayout1.setHgap(2);
gridLayout1.setRows(4);
gridLayout1.setVgap(2);
pan2.setLayout(gridLayout1);
pan2.add(btn7,null);
pan2.add(btn8,null);
pan2.add(btn9,null);
pan2.add(addBtn,null);
pan2.add(btn4,null);
pan2.add(btn5,null);
pan2.add(btn6,null);
pan2.add(subtractBtn,null);
pan2.add(btn1,null);
pan2.add(btn2,null);
pan2.add(btn3,null);
pan2.add(multiBtn,null);
pan2.add(btn0,null);
pan2.add(dotBtn,null);
pan2.add(equalBtn,null);
pan2.add(divBtn,null);
this.getContentPane().setLayout(gridBagLayout2);
this.getContentPane().add(pan2, new GridBagConstraints(0,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(1,0,1,4),190,15));
this.getContentPane().add(pan1, new GridBagConstraints(0,0,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(0,0,1,4),23,0));
this.setResizable(false);
this.setTitle("Calculator");
this.addWindowListener(new java.awt.event.WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
Calculator mf=new Calculator();
mf.setBounds(300,200,240,172);
mf.show();
}
void equalaction(ActionEvent e){
if(recentOperation==null||recentNum==null||tfResult.getText().equals("")) return;
double last=0,now=0;
try{
last=Double.parseDouble(recentNum);
now=Double.parseDouble(tfResult.getText());
}catch(NumberFormatException ne){
recentOperation=null;
recentNum=null;
tfResult.setText("输入的数据不合法");
System.out.println("输入的数据不合法");
isNew=true;
return;
}
if(recentOperation.equals("add")){
int r=(int)(last+now);
if(r==last+now){
tfResult.setText(""+r);
}else{
tfResult.setText(""+(last+now));
}
}
if(recentOperation.equals("subtract")){
int r=(int)(last-now);
if(r==last-now){
tfResult.setText(""+r);
}else{
tfResult.setText(""+(last-now));
}
}
if(recentOperation.equals("multiply")){
int r=(int)(last*now);
if(r==last*now){
tfResult.setText(""+r);
}else{
tfResult.setText(""+(last*now));
}
}
if(recentOperation.equals("divide")){
int r=(int)(last/now);
if(r==last/now){
tfResult.setText(""+r);
}else{
tfResult.setText(""+(last/now));
}
}
}
}