1、简易的计算器代码/版权所有 唐嘉宁import java.awt.*;import javax.swing.*;import java.awt.event.*;class Mycompute extends JFrame implements ActionListener /* * */ private static final long serialVersionUID = 1L; Container con; char ch=#; boolean can=false; float num1=0; JTextArea tj; JButton button1,button2,button3,b
2、utton4,button5,button6,button7, button8,button9,button10,button11,button12,button13,button14, button15,button16,button17,button18,button19,button20,button21, button22,button23,button24,button25,button26,button27,button28; public Mycompute() super(计算器); GridBagLayout gridbag = new GridBagLayout(); Gr
3、idBagConstraints c = new GridBagConstraints(); con=this.getContentPane(); c.fill = GridBagConstraints.BOTH; con.setFont(new Font(Helvetica, Font.PLAIN, 14); con.setLayout(gridbag); c.insets=new Insets(2,2,2,2); c.gridx=0; c.gridy=0; c.weightx = 1.0; c.gridwidth=5; tj=new JTextArea(0.0,5,0); gridbag.
4、setConstraints(tj,c); tj.setEditable(false); con.add(tj); button1=new JButton(MC); c.gridx=0; c.gridy=1; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button1,c); con.add(button1); button2=new JButton(MR); c.gridx=1; c.gridy=1; c.gridwidth=1; gridbag.setConstraints(button2,c); con.add(butto
5、n2); button3=new JButton(MS); c.gridx=2; c.gridy=1; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button3,c); con.add(button3); button4=new JButton(M+); c.gridx=3; c.gridy=1; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button4,c); con.add(button4); button5=new JButton(M-); c.grid
6、x=4; c.gridy=1; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button5,c); con.add(button5); button6=new JButton(); c.gridx=0; c.gridy=2; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button6,c); con.add(button6); button7=new JButton(CE); c.gridx=1; c.gridy=2; c.weightx = 1.0; c.gri
7、dwidth=1; gridbag.setConstraints(button7,c); con.add(button7); button8=new JButton(C); c.gridx=2; c.gridy=2; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button8,c); con.add(button8); button9=new JButton(); c.gridx=3; c.gridy=2; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button
8、9,c); con.add(button9); button10=new JButton(); c.gridx=4; c.gridy=2; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button10,c); con.add(button10); button11=new JButton(7); c.gridx=0; c.gridy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button11,c); con.add(button11); button12=
9、new JButton(8); c.gridx=1; c.gridy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button12,c); con.add(button12); button13=new JButton(9); c.gridx=2; c.gridy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button13,c); con.add(button13); button14=new JButton(/); c.gridx=3; c.gri
10、dy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button14,c); con.add(button14); button15=new JButton(%); c.gridx=4; c.gridy=3; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button15,c); con.add(button15); button16=new JButton(4); c.gridx=0; c.gridy=4; c.weightx = 1.0; c.gridwid
11、th=1; gridbag.setConstraints(button16,c); con.add(button16); button17=new JButton(5); c.gridx=1; c.gridy=4; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button17,c); con.add(button17); button18=new JButton(6); c.gridx=2; c.gridy=4; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(but
12、ton18,c); con.add(button18); button19=new JButton(*); c.gridx=3; c.gridy=4; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button19,c); con.add(button19); button20=new JButton(1/); c.gridx=4; c.gridy=4; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button20,c); con.add(button20); bu
13、tton21=new JButton(1); c.gridx=0; c.gridy=5; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button21,c); con.add(button21); button22=new JButton(2); c.gridx=1; c.gridy=5; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button22,c); con.add(button22); button23=new JButton(3); c.gridx=2
14、; c.gridy=5; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button23,c); con.add(button23); button24=new JButton(-); c.gridx=3; c.gridy=5; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button24,c); con.add(button24); button25=new JButton(=); c.gridx=4; c.gridy=5; c.weightx = 1.0; c.
15、gridwidth=1; c.gridheight=0; gridbag.setConstraints(button25,c); con.add(button25); button26=new JButton(0); c.gridx=0; c.gridy=6; c.weightx = 1.0; c.gridwidth=2; gridbag.setConstraints(button26,c); con.add(button26); button27=new JButton(.); c.gridx=2; c.gridy=6; c.weightx = 1.0; c.gridwidth=1; gri
16、dbag.setConstraints(button27,c); con.add(button27); button28=new JButton(+); c.gridx=3; c.gridy=6; c.weightx = 1.0; c.gridwidth=1; gridbag.setConstraints(button28,c); con.add(button28); button6.addActionListener(this); button8.addActionListener(this); button9.addActionListener(this); button10.addAct
17、ionListener(this); button11.addActionListener(this); button12.addActionListener(this); button13.addActionListener(this); button14.addActionListener(this); button15.addActionListener(this); button16.addActionListener(this); button17.addActionListener(this); button18.addActionListener(this); button19.
18、addActionListener(this); button20.addActionListener(this); button21.addActionListener(this); button22.addActionListener(this); button23.addActionListener(this); button24.addActionListener(this); button25.addActionListener(this); button26.addActionListener(this); button27.addActionListener(this); but
19、ton28.addActionListener(this); setSize(300,350); Toolkit tl=this.getToolkit(); setLocation(tl.getScreenSize().width-this.getWidth()/2,(tl.getScreenSize().height-this.getHeight()/2); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Override public void actionPerformed(ActionEvent e)
20、/ TODO Auto-generated method stub String act=e.getActionCommand(); if(e.getSource()=button6) if(tj.getText().length()1) tj.setText(tj.getText().substring(0, tj.getText().length()-1); else tj.setText(0); return; if(e.getSource()=button8) tj.setText(0); ch=#; return; if(act.equals() if(tj.getText().ch
21、arAt(0)!=-) tj.setText(-+tj.getText(); else tj.setText(tj.getText().substring(1); return;if(act.equals(.) tj.setText(tj.getText()+act); return; if(act!=1/&act.charAt(0)=0&act.charAt(0)=9)if(can) tj.setText(act); can=false; else try if(Float.parseFloat(tj.getText()=0) if(tj.getText().equals(0.) tj.se
22、tText(tj.getText()+act); else tj.setText(act); return; else tj.setText(tj.getText()+act); return; catch(NumberFormatException e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; if(act.equals(+)|act.equals(-)|act.equals(*)|act.equals(/)|act.equals(%)if(ch!=#) try
23、 num1=cacu(num1,ch,Float.parseFloat(tj.getText(); tj.setText(String.valueOf(num1); ch=act.charAt(0); can=true; return; catch(NumberFormatException e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; else try num1=Float.parseFloat(tj.getText(); ch=act.charAt(0); c
24、an=true; return; catch(NumberFormatException e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; if(act.equals() try float num=(float)Math.sqrt(Float.parseFloat(tj.getText(); tj.setText(String.valueOf(num); can=true; return; catch(NumberFormatException e1) JOptio
25、nPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; if(act.equals(1/) try float num=1/Float.parseFloat(tj.getText(); tj.setText(String.valueOf(num); can=true; return; catch(NumberFormatException e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE
26、); return; catch(ArithmeticException e1) JOptionPane.showMessageDialog(null, 除0错误!, 警告!,JOptionPane.ERROR_MESSAGE);return;if(act.equals(=) can=true; tryif(ch=#) return; float num=Float.parseFloat(tj.getText(); num1=cacu(num1,ch,num); tj.setText(String.valueOf(num1); ch=#; return; catch(NumberFormatE
27、xception e1) JOptionPane.showMessageDialog(null, 输入格式错误!, 警告!,JOptionPane.ERROR_MESSAGE); return; public float cacu(float a,char c,float b) float sum; switch(c) case +: sum=a+b; break; case -: sum=a-b; break; case *: sum=a*b; break; case /: if(b=0) JOptionPane.showMessageDialog(null, 除0错误!, 警告!,JOptionPane.ERROR_MESSAGE); return 0; sum=a/b; break;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1