javagui设计——计算器_第1页
javagui设计——计算器_第2页
javagui设计——计算器_第3页
javagui设计——计算器_第4页
javagui设计——计算器_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第 1 页 共 12 页Java GUI 设计运用 Java Swing 技术实现一个简单的“计算器” ,界面如下:第 2 页 共 12 页以上测试 6/3=2.0主要实现代码如下:package domain;import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;public class calculator extends JFrame implements ActionListener JFrame frame;private JButton jia = new JButton(“+“);private JButton jian = new JButton(“-“);private JButton cheng = new JButton(“*“);private JButton chu = new JButton(“/“);private JButton qiuyi = new JButton(“%“);private JButton deng = new JButton(“=“);private JButton fu = new JButton(“+/-“);private JButton dian = new JButton(“.“);private JButton kai = new JButton(“sqrt“);private JButton diao = new JButton(“1/x“);private TextField k1 = new TextField();private objConversion convert = new objConversion();JMenuItem copy, paste, help, about;JButton backspace, ce, c, num0, num1, num2, num3, num4, num5, num6, num7,第 3 页 共 12 页num8, num9;Container cp;JTextField text;String copycontent = “;boolean clickable = true, clear = true;int all = 0;double qian;String fuhao;int first = 1;public calculator() setTitle(“计算器“);setSize(400, 300);setLocation(250, 200);text = new JTextField(25);text.setText(“0.“);text.setHorizontalAlignment(JTextField.RIGHT);/ 从右到左JPanel cp1 = new JPanel();JPanel cp2 = new JPanel();JPanel cp3 = new JPanel();cp = getContentPane();cp.add(cp1, “North“);cp.add(cp2, “Center“);cp.add(cp3, “South“);cp1.setLayout(new GridLayout(1, 6);cp2.setLayout(new GridLayout(1, 3);cp3.setLayout(new GridLayout(6, 6);ButtonGroup btg = new ButtonGroup();JTextField t3 = new JTextField(25);cp1.add(text);text.setEditable(false);text.setBackground(new Color(255, 255, 255);backspace = new JButton(“Backspace“);backspace.setForeground(new Color(255, 0, 0);backspace.addActionListener(this);ce = new JButton(“CE“);第 4 页 共 12 页ce.setForeground(new Color(255, 0, 0);ce.addActionListener(this);c = new JButton(“C“);c.setForeground(new Color(255, 0, 0);c.addActionListener(this);k1.setVisible(false);cp2.add(k1);cp2.add(backspace);cp2.add(ce);cp2.add(c);num0 = new JButton(“0“);num1 = new JButton(“1“);num2 = new JButton(“2“);num3 = new JButton(“3“);num4 = new JButton(“4“);num5 = new JButton(“5“);num6 = new JButton(“6“);num7 = new JButton(“7“);num8 = new JButton(“8“);num9 = new JButton(“9“);cp3.add(num7);num7.addActionListener(this);cp3.add(num8);num8.addActionListener(this);cp3.add(num9);num9.addActionListener(this);cp3.add(num6);num6.addActionListener(this);cp3.add(num5);num5.addActionListener(this);cp3.add(num4);num4.addActionListener(this);cp3.add(num3);num3.addActionListener(this);cp3.add(num2);num2.addActionListener(this);cp3.add(num1);num1.addActionListener(this);diao.addActionListener(this);cp3.add(num0);num0.addActionListener(this);cp3.add(chu);第 5 页 共 12 页chu.setForeground(new Color(255, 0, 0);chu.addActionListener(this);cp3.add(kai);kai.addActionListener(this);cp3.add(cheng);cheng.setForeground(new Color(255, 0, 0);cheng.addActionListener(this);cp3.add(qiuyi);qiuyi.addActionListener(this);cp3.add(jian);jian.setForeground(new Color(255, 0, 0);jian.addActionListener(this);cp3.add(diao);cp3.add(fu);fu.addActionListener(this);cp3.add(dian);dian.addActionListener(this);cp3.add(jia);jia.setForeground(new Color(255, 0, 0);jia.addActionListener(this);cp3.add(deng);deng.setForeground(new Color(255, 0, 0);deng.addActionListener(this);JMenuBar mainMenu = new JMenuBar();setJMenuBar(mainMenu);JMenu editMenu = new JMenu(“编辑“);JMenu helpMenu = new JMenu(“帮助“);mainMenu.add(editMenu);mainMenu.add(helpMenu);copy = new JMenuItem(“ 复制“);paste = new JMenuItem(“ 粘贴“);KeyStroke copyks = KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK);copy.setAccelerator(copyks);/ 设置退出菜单选项加上快捷键KeyStroke pasteks = KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK);paste.setAccelerator(pasteks);/ 设置退出菜单选项加上快捷键editMenu.add(copy);editMenu.add(paste);copy.addActionListener(this);paste.addActionListener(this);help = new JMenuItem(“ 帮助主题“);第 6 页 共 12 页about = new JMenuItem(“ 关于计算器“);helpMenu.add(help);helpMenu.add(about);help.addActionListener(this);about.addActionListener(this);addWindowListener(new WindowDestroyer();/ 结束窗口public void actionPerformed(ActionEvent e)/ 响应动作代码if (first = 1)text.setText(“);first = 0;/ 第一次把文本框 0.清空Object temp = e.getSource();if (temp = copy) copycontent = text.getText();if (temp = paste) text.setText(text.getText() + copycontent);if (temp = help) / 打开系统帮助文件 要查资料try String filePath = “C:/WINDOWS/Help/calc.chm“;Runtime.getRuntime().exec(“cmd.exe /c “ + filePath); catch (Exception eeee) System.out.println(“打开系统的计算器出错 “);if (temp = about) JOptionPane.showMessageDialog(frame,“ Java 计算器n n07 计算机 3 班 20072481 刘学龙“, “ 关于计算器“,JOptionPane.INFORMATION_MESSAGE);try String dec = text.getText();num2.setEnabled(true);第 7 页 共 12 页num3.setEnabled(true);num4.setEnabled(true);num5.setEnabled(true);num6.setEnabled(true);num7.setEnabled(true);num8.setEnabled(true);num9.setEnabled(true); catch (Exception ee) System.out.println(“转换出错,可能你没有输入任何字符 “);text.setText(“转换出错 “);clear = false;if (temp = backspace)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论