| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 498 人关注过本帖
标题:高手们,进来帮帮忙!
只看楼主 加入收藏
zhiwen128
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-6-19
收藏
 问题点数:0 回复次数:1 
高手们,进来帮帮忙!

高手们,能帮我加加注释呀! 以下是程序! ************************************************************* import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*;

class Calculator{ public static void main(String[] args) { MyFrameSix myFrame=new MyFrameSix(); myFrame.show(); } }

class zhiwen extends JFrame implements ActionListener{ private JLabel myLabel=new JLabel("简易计算器"); private JTextField myResult=new JTextField("0",15); private JButton mybs=new JButton("退格"); \\生成退格按钮 private JButton mycl=new JButton("清零"); \\生成清零按钮 private JButton num0=new JButton("0"); \\生成0到9按钮 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 minus=new JButton("-"); private JButton multi=new JButton("*"); private JButton divid=new JButton("/"); private JButton equal=new JButton("="); private long tmp=0; private long opt1,opt2; private int flag; zhiwen(){ Container myContentPane=getContentPane(); GridBagLayout mygbl=new GridBagLayout(); GridBagConstraints mygbc=new GridBagConstraints(); myContentPane.setLayout(mygbl); myLabel.setForeground(Color.blue); mygbc.gridx=1;mygbc.gridy=1; mygbc.gridwidth=GridBagConstraints.REMAINDER; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.NONE; mygbc.anchor=GridBagConstraints.CENTER; mygbc.insets=new Insets(2,1,1,1); mygbl.setConstraints(myLabel,mygbc); myContentPane.add(myLabel); myResult.setEditable(false); myResult.setHorizontalAlignment(JTextField.RIGHT); mygbc.gridx=1;mygbc.gridy=2; mygbc.gridwidth=GridBagConstraints.REMAINDER; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.anchor=GridBagConstraints.CENTER; mygbc.insets=new Insets(2,1,1,1); mygbl.setConstraints(myResult,mygbc); myContentPane.add(myResult); mybs.setForeground(Color.red);//添加删除和退格 mybs.addActionListener(this); mygbc.gridx=1;mygbc.gridy=3; mygbc.gridwidth=2; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(mybs,mygbc); myContentPane.add(mybs); mycl.setForeground(Color.red); mycl.addActionListener(this); mygbc.gridx=3;mygbc.gridy=3; mygbc.gridwidth=2; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(mycl,mygbc); myContentPane.add(mycl); num7.addActionListener(this); mygbc.gridx=1;mygbc.gridy=4;//添加7、8、9、/ mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num7,mygbc); myContentPane.add(num7); num8.addActionListener(this); mygbc.gridx=2;mygbc.gridy=4; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num8,mygbc); myContentPane.add(num8); num9.addActionListener(this); mygbc.gridx=3;mygbc.gridy=4; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num9,mygbc); myContentPane.add(num9); divid.addActionListener(this); divid.setForeground(Color.red); mygbc.gridx=4;mygbc.gridy=4; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(divid,mygbc); myContentPane.add(divid); num4.addActionListener(this); mygbc.gridx=1;mygbc.gridy=5;//添加4、5、6、* mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num4,mygbc); myContentPane.add(num4); num5.addActionListener(this); mygbc.gridx=2;mygbc.gridy=5; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num5,mygbc); myContentPane.add(num5); num6.addActionListener(this); mygbc.gridx=3;mygbc.gridy=5; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num6,mygbc); myContentPane.add(num6); multi.addActionListener(this); multi.setForeground(Color.red); mygbc.gridx=4;mygbc.gridy=5; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(multi,mygbc); myContentPane.add(multi); num1.addActionListener(this); mygbc.gridx=1;mygbc.gridy=6;//添加1、2、3、- mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num1,mygbc); myContentPane.add(num1); num2.addActionListener(this); mygbc.gridx=2;mygbc.gridy=6; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num2,mygbc); myContentPane.add(num2); num3.addActionListener(this); mygbc.gridx=3;mygbc.gridy=6; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num3,mygbc); myContentPane.add(num3); minus.addActionListener(this); minus.setForeground(Color.red); mygbc.gridx=4;mygbc.gridy=6; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(minus,mygbc); myContentPane.add(minus); num0.addActionListener(this); mygbc.gridx=1;mygbc.gridy=7;//添加0、=、+ mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(num0,mygbc); myContentPane.add(num0); equal.addActionListener(this); equal.setForeground(Color.red); mygbc.gridx=2;mygbc.gridy=7; mygbc.gridwidth=2; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(equal,mygbc); myContentPane.add(equal); add.addActionListener(this); add.setForeground(Color.red); mygbc.gridx=4;mygbc.gridy=7; mygbc.gridwidth=1; mygbc.gridheight=1; mygbc.fill=GridBagConstraints.HORIZONTAL; mygbc.insets=new Insets(2,1,1,1); mygbc.anchor=GridBagConstraints.CENTER; mygbl.setConstraints(add,mygbc); myContentPane.add(add); setSize(200,250); setResizable(false); addWindowListener(new QuitWindow()); } public void actionPerformed(ActionEvent e){ if(e.getSource()==mybs&&myResult.getText().length()>0){ tmp=(tmp-tmp%10)/10; myResult.setText(String.valueOf(tmp)); } if(e.getSource()==mycl){ myResult.setText("0"); tmp=0; } if((e.getSource()==num0||e.getSource()==num1||e.getSource()==num2|| e.getSource()==num3||e.getSource()==num4||e.getSource()==num5|| e.getSource()==num6||e.getSource()==num7||e.getSource()==num8|| e.getSource()==num9)&&myResult.getText().length()<10){ tmp=10*tmp+Long.valueOf(e.getActionCommand()).longValue(); myResult.setText(String.valueOf(tmp)); } if(e.getSource()==add||e.getSource()==minus||e.getSource()==multi||e.getSource()==divid){ opt1=Long.valueOf(myResult.getText()).longValue(); tmp=0; if(e.getSource()==add) flag=1; if(e.getSource()==minus) flag=2; if(e.getSource()==multi) flag=3; if(e.getSource()==divid) flag=4; } if(e.getSource()==equal){ if(tmp==0) opt2=Long.valueOf(myResult.getText()).longValue(); else opt2=tmp; if(flag==1){ opt1=opt1+opt2; myResult.setText(String.valueOf(opt1)); } if(flag==2){ opt1=opt1-opt2; myResult.setText(String.valueOf(opt1)); } if(flag==3){ opt1=opt1*opt2; myResult.setText(String.valueOf(opt1));} if(flag==4) try{ opt1=opt1/opt2; myResult.setText(String.valueOf(opt1)); }catch(ArithmeticException exception){ opt1=opt2=0; tmp=0; myResult.setText("除数不能为零"); } } } }

class QuitWindow extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } } ************************************************************************* 谢谢拉!

搜索更多相关主题的帖子: 计算器 private public import 
2005-06-19 20:31
zhiwen128
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-6-19
收藏
得分:0 
这是一个“简单的计算器”的程序!
      我看不懂!
          希望有一位高手帮我加加注释!让我看明白!
 在这里谢谢拉!

2005-06-19 21:05
快速回复:高手们,进来帮帮忙!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017043 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved