| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 786 人关注过本帖
标题:[原创]昨天写的一个程序 我学java以来写的最长的程序
只看楼主 加入收藏
曾阿牛
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2005-10-14
收藏
 问题点数:0 回复次数:7 
[原创]昨天写的一个程序 我学java以来写的最长的程序

小弟刚开始学java 昨天用JCreater写的一个小程序 虽然简单却让我欣喜若狂 迫不及待的拿出来共享 虽然会让群里的大虾们笑话 import java.awt.*; import java.awt.event.*; import javax.swing.*;

public class jisuanqi extends JFrame{ private JButton JButton0; private JButton JButton1; private JButton JButton2; private JButton JButton3; private JButton JButton4; private JButton JButton5; private JButton JButton6; private JButton JButton7; private JButton JButton8; private JButton JButton9; private JButton JButtonjia; private JButton JButtonjian; private JButton JButtoncheng; private JButton JButtonchu; private JButton JButtondeng; private JButton JButtonqing; private JButton JButtontui; private JTextField JTextField1; static int fuhao; static long shu; public jisuanqi(){ createUserInterface(); } public void createUserInterface(){ Container contentPane = getContentPane(); contentPane.setLayout( null ); JButton1 =new JButton(); JButton1.setText("1"); JButton1.setBounds(0,60,60,60); contentPane.add(JButton1); JButton1.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton1ActionPerformed( event ); } }

); JButton2 =new JButton(); JButton2.setText("2"); JButton2.setBounds(60,60,60,60); contentPane.add(JButton2); JButton2.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton2ActionPerformed( event ); } }

); JButton3 =new JButton(); JButton3.setText("3"); JButton3.setBounds(120,60,60,60); contentPane.add(JButton3); JButton3.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton3ActionPerformed( event ); } }

); JButton4 =new JButton(); JButton4.setText("4"); JButton4.setBounds(0,120,60,60); contentPane.add(JButton4); JButton4.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton4ActionPerformed( event ); } }

); JButton5 =new JButton(); JButton5.setText("5"); JButton5.setBounds(60,120,60,60); contentPane.add(JButton5); JButton5.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton5ActionPerformed( event ); } }

); JButton6 =new JButton(); JButton6.setText("6"); JButton6.setBounds(120,120,60,60); contentPane.add(JButton6); JButton6.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton6ActionPerformed( event ); } }

); JButton7 =new JButton(); JButton7.setText("7"); JButton7.setBounds(0,180,60,60); contentPane.add(JButton7); JButton7.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton7ActionPerformed( event ); } }

); JButton8 =new JButton(); JButton8.setText("8"); JButton8.setBounds(60,180,60,60); contentPane.add(JButton8); JButton8.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton8ActionPerformed( event ); } }

); JButton9 =new JButton(); JButton9.setText("9"); JButton9.setBounds(120,180,60,60); contentPane.add(JButton9); JButton9.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton9ActionPerformed( event ); } }

); JButton0 =new JButton(); JButton0.setText("0"); JButton0.setBounds(60,240,60,60); contentPane.add(JButton0); JButton0.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButton0ActionPerformed( event ); } }

); JButtonjia =new JButton(); JButtonjia.setText("+"); JButtonjia.setBounds(180,60,60,60); contentPane.add(JButtonjia); JButtonjia.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonjiaActionPerformed( event ); } }

); JButtonjian =new JButton(); JButtonjian.setText("-"); JButtonjian.setBounds(180,120,60,60); contentPane.add(JButtonjian); JButtonjian.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonjianActionPerformed( event ); } }

); JButtoncheng =new JButton(); JButtoncheng.setText("*"); JButtoncheng.setBounds(180,180,60,60); contentPane.add(JButtoncheng); JButtoncheng.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonchengActionPerformed( event ); } }

); JButtonchu =new JButton(); JButtonchu.setText("/"); JButtonchu.setBounds(180,240,60,60); contentPane.add(JButtonchu); JButtonchu.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonchuActionPerformed( event ); } }

); JButtondeng =new JButton(); JButtondeng.setText("="); JButtondeng.setBounds(120,240,60,60); contentPane.add(JButtondeng); JButtondeng.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtondengActionPerformed( event ); } }

); JButtonqing =new JButton(); JButtonqing.setText("清0"); JButtonqing.setBounds(0,240,60,60); contentPane.add(JButtonqing); JButtonqing.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtonqingActionPerformed( event ); } }

); JButtontui =new JButton(); JButtontui.setText("退格"); JButtontui.setBounds(180,0,60,60); contentPane.add(JButtontui); JButtontui.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent event ){ JButtontuiActionPerformed( event ); } }

); JTextField1 = new JTextField(); JTextField1.setText( "0" ); JTextField1.setBounds( 20, 20, 140, 20 ); JTextField1.setHorizontalAlignment( JTextField.RIGHT ); contentPane.add( JTextField1 ); setTitle("jisuanqi"); this.setSize(240,360); this.setVisible(true);

} private void JButton1ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+1; JTextField1.setText( String.valueOf(b) ); } private void JButton2ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+2; JTextField1.setText( String.valueOf(b) ); } private void JButton3ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+3; JTextField1.setText( String.valueOf(b) ); } private void JButton4ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+4; JTextField1.setText( String.valueOf(b) ); } private void JButton5ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+5; JTextField1.setText( String.valueOf(b) ); } private void JButton6ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+6; JTextField1.setText( String.valueOf(b) ); } private void JButton7ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+7; JTextField1.setText( String.valueOf(b) ); } private void JButton8ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+8; JTextField1.setText( String.valueOf(b) ); } private void JButton9ActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); b=a*10+9; JTextField1.setText( String.valueOf(b) ); } private void JButton0ActionPerformed( ActionEvent event ){ long a=0,b=0; if( JTextField1.getText()==""||Integer.parseInt( JTextField1.getText() )==0) JTextField1.setText(String.valueOf(b)); else {a=Integer.parseInt( JTextField1.getText() );b=a*10;} JTextField1.setText( String.valueOf(b) ); } private void JButtonqingActionPerformed( ActionEvent event ){ JTextField1.setText( String.valueOf(0) ); } private void JButtontuiActionPerformed( ActionEvent event ){ long a,b; a=Integer.parseInt( JTextField1.getText() ); if(a<=10) JTextField1.setText( String.valueOf(0) ); else{b=a%10;a=(a-b)/10;JTextField1.setText( String.valueOf(a) );} } private void JButtonjiaActionPerformed( ActionEvent event ){ fuhao=1; shu=Integer.parseInt( JTextField1.getText() ); JTextField1.setText( String.valueOf(0) ); } private void JButtonjianActionPerformed( ActionEvent event ){ fuhao=2; shu=Integer.parseInt( JTextField1.getText() ); JTextField1.setText( String.valueOf(0) ); } private void JButtonchengActionPerformed( ActionEvent event ){ fuhao=3; shu=Integer.parseInt( JTextField1.getText() ); JTextField1.setText( String.valueOf(0) ); } private void JButtonchuActionPerformed( ActionEvent event ){ fuhao=4; shu=Integer.parseInt( JTextField1.getText() ); JTextField1.setText( String.valueOf(0) ); } private void JButtondengActionPerformed( ActionEvent event ){ long a; long b=Integer.parseInt( JTextField1.getText() ); if(fuhao==1) {a=b+shu;JTextField1.setText( String.valueOf(a) );} else if(fuhao==2) {a=shu-b;JTextField1.setText( String.valueOf(a) );} else if(fuhao==3) {a=b*shu;JTextField1.setText( String.valueOf(a) );} else if(fuhao==4) {a=shu/b;JTextField1.setText( String.valueOf(a) );} } public static void main( String[] args ){ jisuanqi application = new jisuanqi(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }

搜索更多相关主题的帖子: java 
2005-10-26 14:53
飘飘叶子
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:597
专家分:10
注 册:2005-8-17
收藏
得分:0 
呃……楼主想说明啥?
我提点意见吧,首先你定义的JButton JButton0,1,2……完全可以使用数组,一个for循环就解决了
再者,这么多的组件都是用AciotnListener,就不适合使用内部类了,在类上继承ActionListener接口,然后在下面写
public void actionPerformed(ActionEvent e)
{
      if(e.getSource == JButton0){...}
     else if(e.getSource == JButton1){...}
     ………………
}
这样写可以使代码更简练一些
最后想说的一点是,只要有2个以上方法调用内容一样,就应该写一个方法,然后进行调用,你那么多的方法都是相同的代码,写一个方法不是更好?以上……爱听就听……

向着软件工程师的目标前进!
2005-10-26 15:30
believefym
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2005-10-20
收藏
得分:0 
楼上说的是
楼主的代码长,主要原因是做了很多的无用功

2005-10-26 16:31
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
楼主的程序有待改进这是肯定的。 不过楼主的心情我们应该可以理解。应该鼓励和表扬。 曾阿牛, 程序写的很好,很棒。飘飘叶子 和 believefym 的经验是比你强的,你要向他们多取经啊。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-10-26 21:37
yms123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
收藏
得分:0 
这个算最长的?记得我们老师说过,windows2000的源代码把光标指向第一行,让光标向下移动一天的时间能到结尾就不错了。不过代码光长的确没用,再长的代码大部分都是在无谓的重复。效率不高没有任何意义。
2005-10-26 23:46
飘飘叶子
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:597
专家分:10
注 册:2005-8-17
收藏
得分:0 
还有……忘记说了,JButton的命名问题,楼主的代码里按钮全部定义为JButtonX,其实在Java里有自己的命名规则,JButton应该命名为btnXXX……
楼主第一次写这样的程序还是应该予以鼓励的,最后我说一句:加油!

向着软件工程师的目标前进!
2005-10-27 07:50
250697812
Rank: 1
等 级:新手上路
帖 子:112
专家分:0
注 册:2005-10-5
收藏
得分:0 
是啊     应该给鼓励的!!!!!!!!!
           继续努力吧 !!!!!!

2005-10-27 19:41
cqx2004
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2005-9-28
收藏
得分:0 
有个BUG

不能连加

2005-10-27 22:51
快速回复:[原创]昨天写的一个程序 我学java以来写的最长的程序
数据加载中...
 
   



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

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