📄 calculatorframe.java
字号:
package calculator;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.dbswing.*;/** * <p>Title: calculator Demo</p> * <p>Description: This is a calculator demo</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: d6-125</p> * @author Liujun * @version 1.0 */public class calculatorFrame extends JFrame { JPanel contentPane; GridBagLayout gridBagLayout1 = new GridBagLayout(); JTextField jTextField1 = new JTextField(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); JButton jButton4 = new JButton(); JButton jButton5 = new JButton(); JButton jButton6 = new JButton(); JButton jButton7 = new JButton(); JButton jButton8 = new JButton(); JButton jButton9 = new JButton(); JButton jButton10 = new JButton(); JButton jButton11 = new JButton(); JButton jButton12 = new JButton(); JButton jButton13 = new JButton(); JButton jButton14 = new JButton(); ButtonGroup buttonGroup1 = new ButtonGroup(); JRadioButton jRadioButton1 = new JRadioButton(); JRadioButton jRadioButton2 = new JRadioButton(); JRadioButton jRadioButton3 = new JRadioButton(); JRadioButton jRadioButton4 = new JRadioButton(); JButton jButton15 = new JButton(); JButton jButton16 = new JButton(); JButton jButton17 = new JButton(); //定义变量,初始化 double startnum;//存储第一个输入数 double endnum;//存储第二个输入数 boolean isResult = false;//数据输入是否结束 boolean flagAdd = false;//加运算 boolean flagSub = false;//减运算 boolean flagMul = false;//剩运算 boolean flagDiv = false;//除运算 String strValue = null;//一个需要的字符串变量 //Construct the frame public calculatorFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(calculatorFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(gridBagLayout1); this.setSize(new Dimension(348, 223)); this.setTitle("Calculator Frame Demo"); jButton1.setText("1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton2.setText("2"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton3.setText("3"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton4.setText("4"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton5.setText("5"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton6.setText("6"); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton7.setText("7"); jButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton8.setText("8"); jButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton9.setText("9"); jButton9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton10.setText("0"); jButton10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton11.setText("."); jButton11.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton11_actionPerformed(e); } }); jButton12.setText("="); jButton12.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton12_actionPerformed(e); } }); jButton13.setText("重置"); jButton13.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton13_actionPerformed(e); } }); jTextField1.setColumns(10);// jComboBox1.setEditable(false); jButton14.setText("清除"); jButton14.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton14_actionPerformed(e); } }); jRadioButton1.setText("+"); jRadioButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jRadioButton1_actionPerformed(e); } }); jRadioButton2.setText("-"); jRadioButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jRadioButton2_actionPerformed(e); } }); jRadioButton3.setText("*"); jRadioButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jRadioButton3_actionPerformed(e); } }); jRadioButton4.setText("/"); jRadioButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jRadioButton4_actionPerformed(e); } }); jButton15.setText("sin"); jButton15.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton15_actionPerformed(e); } }); jButton16.setText("cos"); jButton16.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton16_actionPerformed(e); } }); jButton17.setText("n!"); jButton17.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton17_actionPerformed(e); } }); contentPane.add(jButton1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton2, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton3, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton4, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton5, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton6, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton7, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton8, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton9, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton10, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jTextField1, new GridBagConstraints(0, 0, 5, 1, 0.0, 0.0 ,GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 133, 0)); contentPane.add(jButton11, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton12, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton13, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton14, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton1, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton2, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton3, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jRadioButton4, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton15, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton16, new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); contentPane.add(jButton17, new GridBagConstraints(4, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); buttonGroup1.add(jRadioButton1); buttonGroup1.add(jRadioButton2); buttonGroup1.add(jRadioButton3); buttonGroup1.add(jRadioButton4); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void jButton1_actionPerformed(ActionEvent e) { if (isResult){//若为结果数据,则清零 jTextField1.setText("0"); isResult = !isResult; } strValue = jTextField1.getText(); if (strValue.equals("0")){ //若初始字符串为0,则覆盖该字符串 jTextField1.setText(e.getActionCommand()); } else{//否则,在字符串后面追加字符 strValue=strValue+e.getActionCommand(); jTextField1.setText(strValue); } } void jButton11_actionPerformed(ActionEvent e) { //输入小数点 strValue = jTextField1.getText(); if(strValue.indexOf(".")<=1) {//若还没有使用了小数点,则加入小数点 strValue=strValue+"."; jTextField1.setText(strValue); } } void jButton12_actionPerformed(ActionEvent e) { //执行加减乘除操作 endnum = Double.parseDouble(jTextField1.getText()); //得到第二个数 if(flagAdd){//处理加法 jTextField1.setText(String.valueOf(startnum+endnum)); isResult = true; flagAdd = !flagAdd; } if(flagSub){//处理减法 jTextField1.setText(String.valueOf(startnum-endnum)); isResult = true; flagSub = !flagSub; } if(flagMul){//处理乘法 jTextField1.setText(String.valueOf(startnum*endnum)); isResult = true; flagMul = !flagMul; } if((flagDiv)&&(endnum!=0)){//处理除法 jTextField1.setText(String.valueOf(startnum/endnum)); isResult = true; flagDiv = !flagDiv; } } void jButton15_actionPerformed(ActionEvent e) { startnum = Double.parseDouble(jTextField1.getText());//得到操作数 jTextField1.setText(String.valueOf(Math.sin(startnum)));//求Sin值 isResult = true; } void jButton16_actionPerformed(ActionEvent e) { startnum = Double.parseDouble(jTextField1.getText());//得到操作数 jTextField1.setText(String.valueOf(Math.cos(startnum)));//求Cos值 isResult = true; } void jButton17_actionPerformed(ActionEvent e) { int Mull = 1; if(((jTextField1.getText()).indexOf(".")<1)//若没有小数点,即为整数 &&(Integer.parseInt(jTextField1.getText())>0))//而且该整数大于零时,求阶乘 { int x = Integer.parseInt(jTextField1.getText()); for(int i=1;i<=x;i++){ Mull = Mull*i; } jTextField1.setText(String.valueOf(Mull));//将结果送入文本框 } isResult = true; } void jButton14_actionPerformed(ActionEvent e) { //回退字符 String str=jTextField1.getText();//得到文本框的字符串 int length=str.length();//得到字符串长度 if(!str.equals(""))//若字符串不为空 jTextField1.setText(str.substring(0,length-1));//回退一个字符 } void jButton13_actionPerformed(ActionEvent e) { jTextField1.setText("");//清空文本框 isResult = true; } void jRadioButton1_actionPerformed(ActionEvent e) { //存储第一个数,并设置flagAdd为真 startnum = Double.parseDouble(jTextField1.getText()); flagAdd=true; isResult = true; } void jRadioButton2_actionPerformed(ActionEvent e) { //存储第一个数,并设置flagSub为真 startnum = Double.parseDouble(jTextField1.getText()); flagSub = true; isResult = true; } void jRadioButton3_actionPerformed(ActionEvent e) { //存储第一个数,并设置flagMul为真 startnum = Double.parseDouble(jTextField1.getText()); flagMul = true; isResult = true; } void jRadioButton4_actionPerformed(ActionEvent e) { //存储第一个数,并设置flagDiv为真 startnum = Double.parseDouble(jTextField1.getText()); flagDiv = true; isResult = true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -