import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; public class MyDocument extends PlainDocument{ int maxLength =20; public MyDocument(int newMaxLength){ super(); maxLength = newMaxLength; } public MyDocument(){ this(20); } public void insertString(int offset,String str,AttributeSet a)throws BadLocationException{ if(getLength()+str.length()>maxLength){ return; } else{ super.insertString(offset,str,a); } } } import java.awt.Color; import java.awt.Container; 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.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class CounterFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; //获得内容面板 private Container con=getContentPane(); //创建菜单栏,菜单及菜单项 private JMenuBar menuBar=new JMenuBar(); private JMenu viewmenu=new JMenu("查看(V)"); private JMenu helpmenu=new JMenu("帮助(H)"); private JMenuItem general =new JMenuItem("标准型(T)",'T'); private JMenuItem color =new JMenuItem("个性型(C)",'C'); private JMenuItem about=new JMenuItem("关于(A)",'A'); //创建一个容量为30个字符的文本框 private JTextField textField=new JTextField(30); //创建JLabel private JLabel label=new JLabel(); //创建计算器的各种按钮 private JButton[] button=new JButton[27]; private String[] buttonstr={"退格", "CE", "C", "MC", "7", "8", "9", "/", "sqrt", "MR", "4", "5", "6", "*", "%", "MS", "1", "2", "3", "-", "1/x", "M+", "0", "+/-", ".", "+", "=" }; //创建各种面板 private JPanel panel=new JPanel(); private JPanel childPanel=new JPanel(); private JPanel childPanel1=new JPanel(); private JPanel childPanel2=new JPanel(); private JPanel childPanel3=new JPanel(); private JPanel childPanel4=new JPanel(); private JPanel childPanel5=new JPanel(); private JPanel childPanel6=new JPanel(); //创建布局管理器 private GridLayout gridLayout=new GridLayout(6,1,6,6); private GridLayout gridLayout1=new GridLayout(1,3,6,6); private GridLayout gridLayout2=new GridLayout(1,6,6,6); boolean tem=true; //标识0 double cache=0; //存储器 boolean temd=true; //标识小数点 boolean temm=false;//标识M+ boolean plus=false; //标识加 boolean subtract=false; //标识减 boolean mul=false; //标识乘 boolean div=false; //标识除 boolean rem=false; //百分号 public CounterFrame(){ viewmenu.setMnemonic('V'); viewmenu.add(general); general.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { childPanel.setBackground(Color.red); childPanel1.setBackground(Color.red); childPanel2.setBackground(Color.red); childPanel3.setBackground(Color.red); childPanel4.setBackground(Color.red); childPanel5.setBackground(Color.red); childPanel6.setBackground(Color.red); panel.setBackground(Color.red); } }); viewmenu.add(color); color.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { childPanel.setBackground(Color.blue); childPanel1.setBackground(Color.cyan); childPanel2.setBackground(Color.black); childPanel3.setBackground(Color.darkGray); childPanel4.setBackground(Color.green); childPanel5.setBackground(Color.lightGray); childPanel6.setBackground(Color.orange); panel.setBackground(Color.red); } }); helpmenu.setMnemonic('H'); helpmenu.add(about); about.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(CounterFrame.this,"孤风侠尘计算器1.0 作者:wustrive_2008", " ",JOptionPane.INFORMATION_MESSAGE); } }); menuBar.add(viewmenu); menuBar.add(helpmenu); this.setJMenuBar(menuBar); for(int i=0;i<27;i++){ button[i]=new JButton(buttonstr[i]); button[i].addActionListener(this); } panel.setLayout(gridLayout); panel.add(childPanel1); childPanel1.add(label); childPanel1.add(textField); label.setBounds(10, 10, 10, 10); textField.setEditable(false); textField.setDocument(new MyDocument()); textField.setText("0"); textField.setBackground(Color.WHITE); textField.setHorizontalAlignment(JTextField.RIGHT);//设置文字从右边开始显示 panel.add(childPanel2); childPanel2.setLayout(gridLayout1); childPanel2.add(button[0]); childPanel2.add(button[1]); childPanel2.add(button[2]); panel.add(childPanel3); childPanel3.setLayout(gridLayout2); childPanel3.add(button[3]); childPanel3.add(button[4]); childPanel3.add(button[5]); childPanel3.add(button[6]); childPanel3.add(button[7]); childPanel3.add(button[8]); panel.add(childPanel4); childPanel4.setLayout(gridLayout2); childPanel4.add(button[9]); childPanel4.add(button[10]); childPanel4.add(button[11]); childPanel4.add(button[12]); childPanel4.add(button[13]); childPanel4.add(button[14]); panel.add(childPanel5); childPanel5.setLayout(gridLayout2); childPanel5.add(button[15]); childPanel5.add(button[16]); childPanel5.add(button[17]); childPanel5.add(button[18]); childPanel5.add(button[19]); childPanel5.add(button[20]); panel.add(childPanel6); childPanel6.setLayout(gridLayout2); childPanel6.add(button[21]); childPanel6.add(button[22]); childPanel6.add(button[23]); childPanel6.add(button[24]); childPanel6.add(button[25]); childPanel6.add(button[26]); childPanel.add(panel); con.add(childPanel); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(400, 300, 400, 300); this.setTitle("孤风侠尘计算器"); this.setVisible(true); } //按钮事件处理 @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==button[4]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[4]); } else{ textField.setText(buttonstr[4]); tem=false; } } if(e.getSource()==button[5]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[5]); } else{ textField.setText(buttonstr[5]); tem=false; } } if(e.getSource()==button[6]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[6]); } else{ textField.setText(buttonstr[6]); tem=false; } } if(e.getSource()==button[10]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[10]); } else{ textField.setText(buttonstr[10]); tem=false; } } if(e.getSource()==button[11]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[11]); } else{ textField.setText(buttonstr[11]); tem=false; } } if(e.getSource()==button[12]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[12]); } else{ textField.setText(buttonstr[12]); tem=false; } } if(e.getSource()==button[16]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[16]); } else{ textField.setText(buttonstr[16]); tem=false; } } if(e.getSource()==button[17]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[17]); } else{ textField.setText(buttonstr[17]); tem=false; } } if(e.getSource()==button[18]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[18]); } else{ textField.setText(buttonstr[18]); tem=false; } } if(e.getSource()==button[22]){ if(tem==false){ textField.setText(textField.getText()+buttonstr[22]); } else{ textField.setText(buttonstr[22]); //tem=false; } } if(e.getSource()==button[0]){ if(textField.getText().length()>1){ textField.setText(textField.getText().substring(0,textField.getText().length()-1)); } else{ textField.setText("0"); tem=true; } } //CE键:清除当前显示的数据 if(e.getSource()==button[1]){ textField.setText("0"); tem=true; tem=true; temd=true; temm=false; plus=false; subtract=false; mul=false; div=false; rem=false; } //C键:清除所有数据 if(e.getSource()==button[2]){ textField.setText("0"); cache=0; tem=true; tem=true; cache=0; temd=true; temm=false; plus=false; subtract=false; mul=false; div=false; rem=false; } //MR键:将存于存储器中的数据显示出来 if(e.getSource()==button[9]){ textField.setText(""+cache+""); } //MC键:清除存储器中数据 if(e.getSource()==button[3]){ cache=0; label.setText(""); } //MS键:将显示的数据存储在存储器中 if(e.getSource()==button[15]){ cache=Double.parseDouble(textField.getText()); } //M+键:将显示的数据与存储器中的数据相加并进行存储 if(e.getSource()==button[21]){ label.setText("M"); cache=cache+Double.parseDouble(textField.getText()); temm=true; } //处理小数点 if(e.getSource()==button[24]){ if(tem==false&&temd==true&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){ textField.setText(textField.getText()+"."); temd=false; } } //处理1/x if(e.getSource()==button[20]){ if(temd==true&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){ textField.setText(""+1/Double.parseDouble(textField.getText())+""); tem=true; } } //处理+/- if(e.getSource()==button[23]){ if(temd==true&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){ Double dou=Double.parseDouble(textField.getText()); dou=-dou; textField.setText(""+dou+""); } } //除法 if(e.getSource()==button[7]){ if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){ textField.setText(textField.getText()+"/"); div=true; } } //乘法 if(e.getSource()==button[13]){ if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){ textField.setText(textField.getText()+"*"); mul=true; } } //百分数表示 if(e.getSource()==button[14]){ if((plus==true&&subtract==false&&mul==false&&div==false)||(plus==false&&subtract==true&&mul==false&&div==false)|| (plus==false&&subtract==false&&mul==true&&div==false)||(plus==false&&subtract==false&&mul==false&&div==true)){ if(plus==true){ int i=textField.getText().indexOf("+"); String substr1=textField.getText().substring(0,i); String substr2=textField.getText().substring(i+1,textField.getText().length()); textField.setText(""+(Double.parseDouble(substr1)+Double.parseDouble(substr1)*Double.parseDouble(substr2)/100)+""); } if(subtract==true){ int i=textField.getText().indexOf("-"); String substr1=textField.getText().substring(0,i); String substr2=textField.getText().substring(i+1,textField.getText().length()); textField.setText(""+(Double.parseDouble(substr1)-Double.parseDouble(substr1)*Double.parseDouble(substr2)/100)+""); } if(mul==true){ int i=textField.getText().indexOf("*"); String substr1=textField.getText().substring(0,i); String substr2=textField.getText().substring(i+1,textField.getText().length()); textField.setText(""+(Double.parseDouble(substr1)*(Double.parseDouble(substr1)*Double.parseDouble(substr2)/100))+""); } if(subtract==true){ int i=textField.getText().indexOf("/"); String substr1=textField.getText().substring(0,i); String substr2=textField.getText().substring(i+1,textField.getText().length()); textField.setText(""+(Double.parseDouble(substr1)/(Double.parseDouble(substr1)*Double.parseDouble(substr2)/100))+""); } } } //加法 if(e.getSource()==button[25]){ if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){ textField.setText(textField.getText()+"+"); plus=true; } } //减法 if(e.getSource()==button[19]){ if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){ textField.setText(textField.getText()+"-"); subtract=true; } } //等于 if(e.getSource()==button[26]){ if(plus==true){ int i=textField.getText().indexOf("+"); String substr1=textField.getText().substring(0,i); String substr2=textField.getText().substring(i+1,textField.getText().length()); if(substr2.length()==0){ textField.setText(""+substr1+""); } else{ double result=Double.parseDouble(substr1)+Integer.parseInt(substr2); textField.setText(""+result+""); } plus=false; tem=true; } if(subtract==true){ int i=textField.getText().indexOf("-"); String substr1=textField.getText().substring(0,i); String substr2=textField.getText().substring(i+1,textField.getText().length()); if(substr2.length()==0){ textField.setText(""+substr1+""); } else{ double result=Double.parseDouble(substr1)-Integer.parseInt(substr2); textField.setText(""+result+""); } subtract=false; tem=true; } if(mul==true){ int i=textField.getText().indexOf("*"); String substr1=textField.getText().substring(0,i); String substr2=textField.getText().substring(i+1,textField.getText().length()); if(substr2.length()==0){ textField.setText(""+substr1+""); } else{ double result=Double.parseDouble(substr1)*Integer.parseInt(substr2); textField.setText(""+result+""); } mul=false; tem=true; } if(div==true){ int i=textField.getText().indexOf("/"); String substr1=textField.getText().substring(0,i); String substr2=textField.getText().substring(i+1,textField.getText().length()); if(substr2.length()==0){ textField.setText(""+substr1+""); } else if(Double.parseDouble(substr2)==0){ textField.setText("除数不能为零"); } else{ double result=Double.parseDouble(substr1)/Double.parseDouble(substr2); textField.setText(""+result+""); } div=false; tem=true; } } //求平方根 if(e.getSource()==button[8]){ if(plus==false&&subtract==false&&mul==false&&rem==false&&div==false&&tem==false){ textField.setText(""+Math.sqrt(Double.parseDouble(textField.getText()))+""); tem=true; } } } } //源代码片段来自云代码http://yuncode.net package Experience5; import java.awt.BorderLayout; import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class SimpleCalculator extends JFrame implements ActionListener{ JTextField jText; Boolean state=false; double firstOperator,secondOperator; int operate; int count=0; //控件的定义 JPanel jUpPanel; JPanel jDownPanel; GridBagLayout gbLayout; GridBagConstraints gbConstraints; String[] jButNames={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"}; JButton[] jButs =new JButton[16]; public static void main(String args[]) { SimpleCalculator calculator=new SimpleCalculator(); } public SimpleCalculator() { super("A Simple Calculator"); //窗体的设定 this.setSize(400, 300); this.setDefaultCloseOperation(EXIT_ON_CLOSE); //this.pack(); this.setVisible(true); //框架的定义 jUpPanel=new JPanel(); jDownPanel=new JPanel(); gbLayout=new GridBagLayout(); gbConstraints=new GridBagConstraints(); this.getContentPane().setLayout(gbLayout); gbConstraints.weightx = 1; gbConstraints.weighty = 0; gbConstraints.fill = GridBagConstraints.BOTH; gbConstraints.gridx=0; gbConstraints.gridy=0; gbConstraints.gridheight=1; gbConstraints.gridwidth=4; gbLayout.setConstraints(jUpPanel, gbConstraints); this.getContentPane().add(jUpPanel); gbConstraints.weighty = 1; gbConstraints.gridx=0; gbConstraints.gridy=1; gbConstraints.gridheight=4; gbConstraints.gridwidth=4; gbLayout.setConstraints(jDownPanel, gbConstraints); this.getContentPane().add(jDownPanel); /* gbConstraints.weightx = 1; gbConstraints.weighty = 0; gbConstraints.fill = GridBagConstraints.BOTH; addComponent(jUpPanel, gbLayout, gbConstraints, 0, 0, 4, 1); gbConstraints.weighty = 1; addComponent(jDownPanel, gbLayout, gbConstraints, 1, 0, 4, 4); this.getContentPane().setLayout(gbLayout); */ //jUpPanel内容的填充 jText=new JTextField(); jText.setHorizontalAlignment(JTextField.RIGHT); jText.setEnabled(false); jText.setText("本计算器支持连续操作,即1+3-4+5=这样的操作"); jUpPanel.setLayout(new BorderLayout()); jUpPanel.add(jText); //jDownPanel内容的填充 jDownPanel.setLayout(new GridLayout(4,4)); for(int i=0;i<16;i++) { jButs[i]=new JButton(jButNames[i]); jButs[i].addActionListener(this); jButs[i].setActionCommand(jButs[i].getName()); jDownPanel.add(jButs[i]); } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==jButs[3]||e.getSource()==jButs[7]||e.getSource()==jButs[11]||e.getSource()==jButs[15]) { double temp=0.0; count++; try { temp=Double.parseDouble(jText.getText()); } catch(Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog((Component) e.getSource(), "第一个操作数不正确"); System.exit(0); } if(count>1) { if(operate==1) temp=firstOperator/temp; else if(operate==2) temp=firstOperator*temp; else if(operate==3) temp=firstOperator-temp; else temp=firstOperator+temp; jText.setText(Double.toString(temp)); state=false; } else { jText.setText(""); } if(e.getSource()==jButs[3]) operate=1; else if(e.getSource()==jButs[7]) operate=2; else if(e.getSource()==jButs[11]) operate=3; else operate=4; firstOperator=temp; } else if(e.getSource()==jButs[14]) { double answer; count=0; try { secondOperator=Double.parseDouble(jText.getText()); } catch(Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog((Component) e.getSource(), "第二个操作数不正确"); System.exit(0); } if(operate==1) answer=firstOperator/secondOperator; else if(operate==2) answer=firstOperator*secondOperator; else if(operate==3) answer=firstOperator-secondOperator; else answer=firstOperator+secondOperator; jText.setText(Double.toString(answer)); state=false; } else { if(state) jText.setText(jText.getText()+e.getActionCommand()); else { state=true; jText.setText(e.getActionCommand()); } } } /* private void addComponent(Component c, GridBagLayout g, GridBagConstraints gc, int row, int column, int width, int height) { gc.gridx = column; gc.gridy = row; gc.gridwidth = width; gc.gridheight = height; g.setConstraints(c, gc); add(c); } */ } //源代码片段来自云代码http://yuncode.net