king431
发贴: 13
积分: 0
|
于 2005-11-08 20:30
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Count { public static void main(String args[]) { JFrame Myframe=new JFrame(); Myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Myframe.show(); } } class Myframe extends JFrame { public static final int HEIGHT=200; public static final int WIDHT=300; public Myframe() { setTitle("计算器"); setSize(WIDHT,HEIGHT); Container con=getContentPane(); CountPanel countpanel=new CountPanel(); con.add(countpanel,BorderLayout.CENTER); } } class CountPanel extends JPanel { private JPanel panel=new JPanel(); private String lastcommand="="; private double result=0; private boolean start=true; setLayout(new GridBagLayout()); private JTextField Numshow=new JTextField("0",15); private JButton CE=new JButton("CE"); private JButton Backspace=new JButton("Backspace"); private JButton Num0=new JButton("0"); private JButton Num1=new JButton("1"); private JButton Num2=new JButton("2"); private JButton Num3=new JButton("3"); private JButton Num4=new JButton("4"); private JButton Num5=new JButton("5"); private JButton Num6=new JButton("6"); private JButton Num7=new JButton("7"); private JButton Num8=new JButton("8"); private JButton Num9=new JButton("9"); private JButton Add=new JButton("+"); private JButton Sub=new JButton("-"); private JButton Mul=new JButton("*"); private JButton Div=new JButton("/"); private JButton Equ=new JButton("="); ActionListener numAction=new NumAction(); ActionListener Command=new CommandAction(); GridBagConstraints constraints=new GridBagConstraints(); constraints.fill=GridBagConstraints.HORIZONTAL; constraints.weightx = 6; constraints.weighty = 4; addText(Numshow,constraints,0,0,1,4); addButton(CE,constraints,1,0,1,2,Command); addButton(Backspace,constraints,1,2,1,2,Command); addButton(Num7,constraints,2,0,1,1,numAction); addButton(Num8,constraints,2,1,1,1,numAction); addButton(Num9,constraints,2,2,1,1,numAction); addButton(Div,constraints,2,3,1,1,Command); addButton(Num4,constraints,3,0,1,1,numAction); addButton(Num5,constraints,3,1,1,1,numAction); addButton(Num5,constraints,3,2,1,1,numAction); addButton(Mul,constraints,3,3,1,1,Command); addButton(Num1,constraints,4,0,1,1,numAction); addButton(Num2,constraints,4,1,1,1,numAction); addButton(Num3,constraints,4,2,1,1,numAction); addButton(Sub,constraints,4,3,1,1,Command); addButton(Num0,constraints,5,0,1,1,numAction); addButton(Equ,constraints,5,1,1,2,Command); addButton(Add,constraints,5,3,1,1,Command); } public void addText(JTextField c,GridBagConstraints constraints,int x,int y,int w,int h) { constraints.gridx=x; constraints.gridy=y; constraints.gridwidth=w; constraints.gridheight=h; add(c,constraints); } public void addButton(Button button,GridBagConstraints constraints,int x,int y,int w,int h,ActionListener listener) { constraints.gridx=x; constraints.gridy=y; constraints.gridwidth=w; constraints.gridheight=h; button.addActionListener(listener); add(button,constraints); } private class NumAction implements ActionListener { public void actionPerformed(ActionEvent event) { String input=event.getActionCommand(); if(start) { Numshow.setText(""); start=false; } Numshow.setText(Numshow.getText() +input); } } private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent evt) { String command=evt.getActionCommand(); if(start) { if(command.equals("-")) { Numshow.setText(command); start=false; } else lastcommand=command; } else { calculate(Integer.parseInt(Numshow.getText())); lastcommand=command; start=true; } } } public void calculate (int x) { if(lastcommand.equals("+")) result+=x; else if(lastcommand.equals("-")) result-=x; else if(lastcommand.equals("*")) result*=x; else if(lastcommand.equals("/")) result/=x; else if(lastcommand.equals("=")) result=x; else if(lastcommand.equals("CE")) result=0; else if(lastcommand.equals("Backspace")) result=result/10; Numshow.setText(""+result); } }
|