A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小马初长成 中级黑马   /  2014-4-25 17:04  /  964 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 小马初长成 于 2014-4-27 22:16 编辑

//这个程序完成了计算器界面的开发,未完善其对应的功能。求大神完善。最后代码有点长,大神们可以帮忙优化下
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;

  6. public class A6_6 extends JFrame implements ActionListener
  7. {
  8.         
  9.         JTextField wbk;
  10.         JButton an1,an2,an3,an4,an5,
  11.         an6,an7,an8,an9,an10,
  12.         an11,an12,an13,an14,an15,an16;
  13.         JPanel mb1,mb2;
  14.         JMenuBar cd;
  15.         JMenu cd1,cd2,cd3;
  16.         JMenuItem cdx2,cdx3,cdx4,cdx5,cdx6,cdx7;
  17.         JMenu ej; JMenuItem ej1,ej2;
  18.         public static void main(String[] args){
  19.                         A6_6 Lx1=new A6_6();
  20.         }
  21.         public A6_6()
  22.         {
  23.                 cd=new JMenuBar();
  24.                 cd1=new JMenu("查看(V)");
  25.                 cd1.setMnemonic('V');
  26.                 cd2=new JMenu("编辑(E)");
  27.                 cd1.setMnemonic('E');
  28.                 cd3=new JMenu("帮助(H)");
  29.                 cd1.setMnemonic('H');
  30.                 wbk=new JTextField(19);
  31.                 JTextField display = new JTextField("0");
  32.                 mb1=new JPanel();
  33.                 mb2=new JPanel();
  34.                 an1=new JButton("1");
  35.                 an2=new JButton("2");
  36.                 an3=new JButton("3");
  37.                 an4=new JButton("4");
  38.                 an5=new JButton("5");
  39.                 an6=new JButton("6");
  40.                 an7=new JButton("7");
  41.                 an8=new JButton("8");
  42.                 an9=new JButton("9");
  43.                 an10=new JButton("0");
  44.                 an11=new JButton(".");
  45.                 an12=new JButton("Enter");
  46.                 an13=new JButton("+");
  47.                 an14=new JButton("-");
  48.                 an15=new JButton("*");
  49.                 an16=new JButton("/");
  50.                
  51.                 ej=new JMenu("新建");
  52.                 ej1=new JMenuItem("文件",new ImageIcon("image/"));
  53.                 ej2=new JMenuItem("模板");
  54.                
  55.                 cdx2=new JMenuItem("打开",new ImageIcon("image/"));
  56.                 cdx3=new JMenuItem("保存(s)",new ImageIcon("image/"));
  57.                 cdx3.setMnemonic('S');
  58.                 cdx4=new JMenuItem("另存为",new ImageIcon("image/"));
  59.                 cdx5=new JMenuItem("页面设置",new ImageIcon("image/"));
  60.                 cdx6=new JMenuItem("打印",new ImageIcon("image/"));
  61.                 cdx7=new JMenuItem("退出",new ImageIcon("image/"));
  62.                
  63.                 an1.addActionListener(this);
  64.                 an1.setActionCommand("1");
  65.                 an2.addActionListener(this);
  66.                 an2.setActionCommand("2");
  67.                 an3.addActionListener(this);
  68.                 an3.setActionCommand("3");
  69.                 an4.addActionListener(this);
  70.                 an4.setActionCommand("4");
  71.                 an5.addActionListener(this);
  72.                 an5.setActionCommand("5");
  73.                 an6.addActionListener(this);
  74.                 an6.setActionCommand("6");
  75.                 an7.addActionListener(this);
  76.                 an7.setActionCommand("7");
  77.                 an8.addActionListener(this);
  78.                 an8.setActionCommand("8");
  79.                 an9.addActionListener(this);
  80.                 an9.setActionCommand("9");
  81.                 an10.addActionListener(this);
  82.                 an10.setActionCommand("0");
  83.                 an11.addActionListener(this);
  84.                 an11.setActionCommand(".");
  85.                 an12.addActionListener(this);
  86.                 an12.setActionCommand("Enter");
  87.                 an13.addActionListener(this);
  88.                 an13.setActionCommand("+");
  89.                 an14.addActionListener(this);
  90.                 an14.setActionCommand("-");
  91.                 an15.addActionListener(this);
  92.                 an15.setActionCommand("*");
  93.                 an16.addActionListener(this);
  94.                 an16.setActionCommand("/");
  95.                
  96.                
  97.                 this.setJMenuBar(cd);
  98.                
  99.                 ej.add(ej1); ej.add(ej2);
  100.                
  101.                 cd1.add(ej); cd1.add(cdx2);        cd2.add(cdx3); cd3.add(cdx4);
  102.                 cd1.addSeparator();
  103.                 cd1.add(cdx5); cd1.add(cdx6);
  104.                 cd1.addSeparator();//二级菜单中的两条横线
  105.                 cd1.add(cdx7);
  106.                
  107.                 cd.add(cd1); cd.add(cd2); cd.add(cd3);
  108.                 this.add(wbk);
  109.                
  110.                 this.setLayout(new FlowLayout());
  111.                
  112.                 mb2.setLayout(new GridLayout(6,3,9,5));
  113.                 mb2.add(an1); mb2.add(an2); mb2.add(an3);
  114.                 mb2.add(an4); mb2.add(an5); mb2.add(an6);
  115.                 mb2.add(an7); mb2.add(an8); mb2.add(an9);
  116.                 mb2.add(an10); mb2.add(an11); mb2.add(an16);
  117.                 mb2.add(an13); mb2.add(an14); mb2.add(an15);
  118.             mb2.add(an12);
  119.                
  120.                 this.add(mb1,BorderLayout.NORTH);
  121.                 this.add(mb2,BorderLayout.CENTER);
  122.                 this.setTitle("计算器");//设置窗口标题               
  123.                 this.setSize(260,350);//括号中的数字是像素
  124.                 this.setLocation(200,200);//括号中的数字是像素位置        
  125.                 this.setResizable(false);
  126.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭界面占用资源管理语句
  127.                 this.setVisible(true);//显示界面
  128.         }
  129.         public void actionPerformed(ActionEvent e) {
  130.                 System.out.println("按钮起作用了");
  131.                 if(e.getActionCommand().equals("1"))
  132.                 {
  133.                  //这里不知道要添加什么可以让计算器实现功能,在此基础上求大神完善下
  134.                
  135.                 }
  136.                
  137.         }
  138. }
  139.         
复制代码

4 个回复

倒序浏览
我去,这................................
心有余而力不足啊。:P
回复 使用道具 举报
想帮你写下去的话,恐怕要累死人的,这样写代码量太大了。我给你一个例子去参考一下,祝你成功!!!
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. public class Calculate
  4. {
  5.         public static void main(String[] args)
  6.         {
  7.                 new Calculater().launch();
  8.         }       
  9. }

  10. @SuppressWarnings("serial")
  11. class Calculater extends Frame
  12. {
  13.        
  14.         private boolean firstFlag = true;
  15.         private String str1 = "";
  16.         private String str2 = "";
  17.         private TextField tf = null;
  18.         private String strOper = null;  
  19.        
  20.         public void launch()
  21.         {
  22.                 setTitle("计算器");
  23.                 tf = new TextField(30);
  24.                 tf.setBackground(Color.white);
  25.                 add(tf, BorderLayout.NORTH);
  26.                 Panel p = new Panel(new GridLayout(4, 4, 5, 5));
  27.                 for (int i=0; i<10; ++i)
  28.                 {
  29.                         Button bn = new Button("" + i);
  30.                         bn.setActionCommand("数字");
  31.                         p.add(bn);
  32.                         bn.addActionListener(new MyMonitor());                       
  33.                 }
  34.                
  35.                 Button bnAdd = new Button("+");  
  36.                 p.add(bnAdd);
  37.                 bnAdd.setActionCommand("算术操作");
  38.                 bnAdd.addActionListener(new MyMonitor());
  39.                
  40.                 Button bnSub = new Button("-");  
  41.                 p.add(bnSub);
  42.                 bnSub.setActionCommand("算术操作");
  43.                 bnSub.addActionListener(new MyMonitor());
  44.                
  45.                 Button bnMult = new Button("*");
  46.                 bnMult.addActionListener(new MyMonitor());
  47.                 p.add(bnMult);
  48.                 bnMult.setActionCommand("算术操作");
  49.                
  50.                 Button bnDiv = new Button("/");  
  51.                 p.add(bnDiv);
  52.                 bnDiv.setActionCommand("算术操作");
  53.                 bnDiv.addActionListener(new MyMonitor());
  54.                
  55.                 Button bnEq = new Button("=");  
  56.                 p.add(bnEq);
  57.                 bnEq.setActionCommand("=");
  58.                 bnEq.addActionListener(new MyMonitor());
  59.                
  60.                 Button bnClear = new Button("清零");  
  61.                 p.add(bnClear);
  62.                 bnClear.setActionCommand("清零");
  63.                 bnClear.addActionListener(new MyMonitor());
  64.                
  65.                 p.add(bnAdd);
  66.                 p.add(bnSub);
  67.                 p.add(bnMult);
  68.                 p.add(bnDiv);
  69.                 p.add(bnEq);
  70.                 p.add(bnClear);               
  71.                
  72.                 add(p, BorderLayout.CENTER);
  73.                 setBounds(300, 300,300, 300);
  74.                 setBackground(Color.WHITE);
  75.                 setVisible(true);
  76.                 addWindowListener(  new WindowAdapter()
  77.                                                           {
  78.                                                                         @Override
  79.                                                                         public void windowClosing(WindowEvent e)
  80.                                                                         {
  81.                                                                                 System.exit(-1);
  82.                                                                         }                                
  83.                                                           }               
  84.                 );
  85.         }
  86.        
  87.         class MyMonitor implements ActionListener
  88.         {
  89.                 public void actionPerformed(ActionEvent e)
  90.                 {
  91.                        
  92.                         String strAc = e.getActionCommand();
  93.                         Button bn = (Button)e.getSource();
  94.                         String strLb = bn.getLabel();
  95.                        
  96.                         if (strAc.equals("数字"))
  97.                         {
  98.                                 if (firstFlag)
  99.                                 {
  100.                                         str1 = str1 + strLb;   
  101.                                         tf.setText(str1);
  102.                                 }
  103.                                 else
  104.                                 {
  105.                                         str2 = str2 + strLb;  
  106.                                         tf.setText(str2);
  107.                                 }
  108.                         }
  109.                         else if (strAc.equals("算术操作"))
  110.                         {
  111.                                 strOper = strLb;  
  112.                                 firstFlag = false;
  113.                         }
  114.                         else if (strAc.equals("="))
  115.                         {
  116.                                 result();
  117.                         }
  118.                         else if (strAc.equals("清零"))
  119.                         {
  120.                                 str1 = str2 = "";
  121.                                 tf.setText("");
  122.                                 firstFlag = true;
  123.                         }                               
  124.                 }
  125.         }
  126.        
  127.         public void result()
  128.         {
  129.                 double num1 = Double.parseDouble(str1);
  130.                 double num2 = Double.parseDouble(str2);
  131.                 double num3 = 0;
  132.                
  133.                 if (strOper.equals("+"))
  134.                 {
  135.                         num3 = num1 + num2;
  136.                 }
  137.                 else if (strOper.equals("-"))
  138.                 {
  139.                         num3 = num1 - num2;
  140.                 }
  141.                 else if (strOper.equals("*"))
  142.                 {
  143.                         num3 = num1 * num2;
  144.                 }
  145.                 else if (strOper.equals("/"))
  146.                 {
  147.                         num3 = num1 / num2;
  148.                 }
  149.                
  150.                 tf.setText("" + num3);
  151.                 str1 = String.valueOf(num3);   
  152.                 str2 = "";
  153.                
  154.         }       
  155. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

回复 使用道具 举报
今天刚刚学GUI
回复 使用道具 举报
eternallove 发表于 2014-4-25 19:13
想帮你写下去的话,恐怕要累死人的,这样写代码量太大了。我给你一个例子去参考一下,祝你成功!!! ...

多谢了,这就去研究研究
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马