?? calculatorframe.java
字號:
/*
*文件名:calculatorFrame.java
*說 明:calculator的主實現(xiàn)類
*/
package calculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// 這里引用到了第三方的包com.borland.dbswing
import com.borland.dbswing.*;
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;//存儲第一個輸入數(shù)
double endnum;//存儲第二個輸入數(shù)
boolean isResult = false;//數(shù)據(jù)輸入是否結(jié)束
boolean flagAdd = false;//加運算
boolean flagSub = false;//減運算
boolean flagMul = false;//剩運算
boolean flagDiv = false;//除運算
String strValue = null;//一個需要的字符串變量
//構(gòu)造方法
public calculatorFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//初始化成員
private void jbInit() throws Exception {
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);
}
//窗口事件監(jiān)聽器
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
if (isResult){//若為結(jié)果數(shù)據(jù),則清零
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) {
//輸入小數(shù)點
strValue = jTextField1.getText();
if(strValue.indexOf(".")<=1) {//若還沒有使用了小數(shù)點,則加入小數(shù)點
strValue=strValue+".";
jTextField1.setText(strValue);
}
}
void jButton12_actionPerformed(ActionEvent e) {
//執(zhí)行加減乘除操作
endnum = Double.parseDouble(jTextField1.getText());
//得到第二個數(shù)
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());//得到操作數(shù)
jTextField1.setText(String.valueOf(Math.sin(startnum)));//求Sin值
isResult = true;
}
void jButton16_actionPerformed(ActionEvent e) {
startnum = Double.parseDouble(jTextField1.getText());//得到操作數(shù)
jTextField1.setText(String.valueOf(Math.cos(startnum)));//求Cos值
isResult = true;
}
void jButton17_actionPerformed(ActionEvent e) {
int Mull = 1;
if(((jTextField1.getText()).indexOf(".")<1)//若沒有小數(shù)點,即為整數(shù)
&&(Integer.parseInt(jTextField1.getText())>0))//而且該整數(shù)大于零時,求階乘
{
int x = Integer.parseInt(jTextField1.getText());
for(int i=1;i<=x;i++){
Mull = Mull*i;
}
jTextField1.setText(String.valueOf(Mull));//將結(jié)果送入文本框
}
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) {
//存儲第一個數(shù),并設(shè)置flagAdd為真
startnum = Double.parseDouble(jTextField1.getText());
flagAdd=true;
isResult = true;
}
void jRadioButton2_actionPerformed(ActionEvent e) {
//存儲第一個數(shù),并設(shè)置flagSub為真
startnum = Double.parseDouble(jTextField1.getText());
flagSub = true;
isResult = true;
}
void jRadioButton3_actionPerformed(ActionEvent e) {
//存儲第一個數(shù),并設(shè)置flagMul為真
startnum = Double.parseDouble(jTextField1.getText());
flagMul = true;
isResult = true;
}
void jRadioButton4_actionPerformed(ActionEvent e) {
//存儲第一個數(shù),并設(shè)置flagDiv為真
startnum = Double.parseDouble(jTextField1.getText());
flagDiv = true;
isResult = true;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -