资源预览内容
第1页 / 共27页
第2页 / 共27页
第3页 / 共27页
第4页 / 共27页
第5页 / 共27页
第6页 / 共27页
第7页 / 共27页
第8页 / 共27页
第9页 / 共27页
第10页 / 共27页
亲,该文档总共27页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
用Java Swing实现简单的计算器功能 班级: 姓名: 学号: 计算机科学与工程学院 网络工程系一:源代码如下:package com.xinzhan.demoFrame;import java.awt.Font;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class CalculatorFrame extends JFrame implements ActionListenerpublic JTextField textField = new JTextField();private final JPanel panel = new JPanel();private final JButton b12 = new JButton();private final JButton b13 = new JButton();private final JButton s5 = new JButton();private final JButton s1 = new JButton();private final JButton b7 = new JButton();private final JButton b8 = new JButton();private final JButton b9 = new JButton();private final JButton s2 = new JButton();private final JButton b4 = new JButton();private final JButton b5 = new JButton();private final JButton b6 = new JButton();private final JButton s3 = new JButton();private final JButton b1 = new JButton();private final JButton b2 = new JButton();private final JButton b3 = new JButton();private final JButton s4 = new JButton();private final JButton b0 = new JButton();private final JButton b11 = new JButton();private final JButton bn = new JButton();private final Image icon1 = Toolkit.getDefaultToolkit().getImage(image/cal.png);private boolean add,sub,mul,div;private boolean end = true;private double num1;/第一个操作数private double num2;/第二个操作数private double result;/存放结果/* * Launch the application * param args */public static void main(String args) try CalculatorFrame frame = new CalculatorFrame();frame.setVisible(true); catch (Exception e) e.printStackTrace();/* * Create the frame * return */public CalculatorFrame() super();this.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width/2-553/2,Toolkit.getDefaultToolkit().getScreenSize().height/2-487/2, 553, 487);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);try jbInit(); catch (Throwable e) e.printStackTrace();private void jbInit() throws Exception getContentPane().setLayout(null);setFont(new Font(黑体, Font.PLAIN, 20);setTitle(计算机Jhon2.0版);this.setIconImage(icon1);getContentPane().add(textField);textField.setFont(new Font(楷体, Font.PLAIN, 20);textField.setBounds(47, 29, 448, 83);textField.setHorizontalAlignment(JTextField.RIGHT);textField.setEditable(false);textField.setText(0);getContentPane().add(panel);panel.setLayout(null);panel.setBounds(10, 141, 517, 308);panel.add(b12);b12.setFont(new Font(楷体, Font.PLAIN, 20);b12.setBounds(10, 10, 106, 40);b12.setText(Cancel);b12.addActionListener(this);panel.add(b13);b13.setFont(new Font(楷体, Font.PLAIN, 30);b13.setText(CE);b13.setBounds(143, 10, 106, 40);b13.addActionListener(this);panel.add(s5);s5.setText(EXIT);s5.setFont(new Font(楷体, Font.PLAIN, 30);s5.setBounds(270, 10, 106, 40);s5.addActionListener(this);panel.add(s1);s1.setFont(new Font(楷体, Font.PLAIN, 30);s1.setText(+);s1.setBounds(401, 10, 106, 40);s1.addActionListener(this);panel.add(b7);b7.setFont(new Font(楷体, Font.PLAIN, 30);b7.setText(7);b7.setBounds(10, 70, 106, 40);b7.addActionListener(this);panel.add(b8);b8.setFont(new Font(楷体, Font.PLAIN, 30);b8.setText(8);b8.setBounds(143, 70, 106, 40);b8.addActionListener(this);panel.add(b9);b9.setFont(new Font(楷体, Font.PLAIN, 30);b9.setText(9);b9.setBounds(270, 70, 106, 40);b9.addActionListener(this);panel.add(s2);s2.setFont(new Font(, Font.PLAIN, 30);s2.setText(-);s2.setBounds(401, 70, 106, 40);s2.addActionListener(this);panel.add(b4);b4.setFont(new Font(楷体, Font.PLAIN, 30);b4.setText(4);b4.setBounds(10, 133, 106, 40);b4.addActionListener(this);panel.add(b5);b5.setFont(new Font(楷体, Font.PLAIN, 30);b5.setText(5);b5.setBounds(143, 133, 106, 40);b5.addActionListener(this);panel.add(b6);b6.setFont(new Font(楷体, Font.PLAIN, 30);b6.setText(6);b6.setBounds(270, 133, 106, 40);b6.addActionListener(this);panel.add(s3);s3.setFont(new Font(楷体, Font.PLAIN, 30);s3.setText(*);s3.setBounds(401, 133, 106, 40);s3.addActionListener(this);panel.add(b1);b1.setFont(new Font(楷体, Font.PLAIN, 30);b1.setText(1);b1.setBounds(10, 195, 106, 40);b1.addActionListener(this);panel.add(b2);b2.setFont(new Font(楷体, Font.PLAIN, 30);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号