📄 calculator.java
字号:
String tempStr=displayField.getText();
tempStr=tempStr.replace(",","");
double num=0;
try{
num=java.lang.Double.parseDouble(tempStr);
if(num>=0){
decimalismFormat.setMaximumFractionDigits(31);
displayField.setText(decimalismFormat.format(Math.sqrt(num)));
}
else{
displayField.setText("函数输入无效。");
isError=true;
}
}
catch(NumberFormatException nfe){
}
isDot=false;
isOperandBegin=true;
}
});
//创建%按钮
btnPercent=new JButton("%");
btnPercent.setForeground(Color.BLUE);
btnPercent.setMargin(new Insets(0,1,2,1));
container.add(btnPercent);
btnPercent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(isError==true){
return;
}
try{
String displayStr=displayField.getText();
DecimalFormat df=new DecimalFormat();
Object obj=df.parse(displayStr);
BigDecimal bd=new BigDecimal(obj.toString());
String tempStr=Arith.div(bd.toString(),"100").toString();
displayField.setText(decimalismFormat.format(Arith.mul(result,tempStr)));
}
catch(NumberFormatException e){
}
catch (ParseException pe) {
pe.printStackTrace();
}
isDot=false;
isOperandBegin=true;
}
});
//创建1/x按钮
btnReciprocal=new JButton("1/x");
btnReciprocal.setMargin(new Insets(0,1,2,1));
container.add(btnReciprocal);
btnReciprocal.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(isError==true){
return;
}
try{
String displayStr=displayField.getText();
double num=Double.parseDouble(displayStr);
if(num!=0){
BigDecimal bd;
if(isFirstReciprocal==true){
bd=Arith.div("1",displayStr.replace(",",""));
bottomNum=bd.toString();
isFirstReciprocal=false;
}else{
bd=Arith.div("1",bottomNum);
isFirstReciprocal=true;
}
if(bd.toString().length()>=31){
displayField.setText(new DecimalFormat("0.###############################E0").format(bd));
String testStr=displayField.getText();
String strscale=testStr.substring(testStr.indexOf("E")+1,testStr.length());
int scale=Integer.parseInt(strscale);
if(scale>=-32&&scale<31){
displayField.setText(decimalismFormat.format(bd));
}
isOperandBegin=true;
return;
}
displayField.setText(decimalismFormat.format(bd));
}
else{
displayField.setText("除数不能为0。");
isError=true;
}
}
catch(NumberFormatException nfe){
}
isOperandBegin=true;
isDot=false;
}
});
//创建pi按钮
btnPI=new JButton("pi");
btnPI.setForeground(Color.BLUE);
btnPI.setMargin(new Insets(0,1,2,1));
container.add(btnPI);
btnPI.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(isError==true){
return;
}
if(cbInv.isSelected()==true){
displayField.setText(2*Math.PI+"");
cbInv.setSelected(false);
}
else{
displayField.setText(Math.PI+"");
}
isOperandBegin=true;
isDot=true;
}
});
//创建Mod按钮
btnMod=new JButton("Mod");
btnMod.setForeground(Color.RED);
btnMod.setMargin(new Insets(0,1,2,1));
container.add(btnMod);
btnMod.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(viewMenu_standarType.isSelected()){
setOperatorForStandar(e);
}
else{
setOperatorForScience(e);
}
}
});
//创建Or按钮
btnOr=new JButton("Or");
btnOr.setForeground(Color.RED);
btnOr.setMargin(new Insets(0,1,2,1));
container.add(btnOr);
btnOr.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(viewMenu_standarType.isSelected()){
setOperatorForStandar(e);
}
else{
setOperatorForScience(e);
}
}
});
//创建Lsh按钮
btnLsh=new JButton("Lsh");
btnLsh.setForeground(Color.RED);
btnLsh.setMargin(new Insets(0,1,2,1));
container.add(btnLsh);
btnLsh.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(viewMenu_standarType.isSelected()){
setOperatorForStandar(e);
}
else{
setOperatorForScience(e);
}
}
});
//创建And按钮
btnAnd=new JButton("And");
btnAnd.setForeground(Color.RED);
btnAnd.setMargin(new Insets(0,1,2,1));
container.add(btnAnd);
btnAnd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(viewMenu_standarType.isSelected()){
setOperatorForStandar(e);
}
else{
setOperatorForScience(e);
}
}
});
//创建Xor按钮
btnXor=new JButton("Xor");
btnXor.setForeground(Color.RED);
btnXor.setMargin(new Insets(0,1,2,1));
container.add(btnXor);
btnXor.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(viewMenu_standarType.isSelected()){
setOperatorForStandar(e);
}
else{
setOperatorForScience(e);
}
}
});
//创建Not按钮
btnNot=new JButton("Not");
btnNot.setForeground(Color.RED);
btnNot.setMargin(new Insets(0,1,2,1));
container.add(btnNot);
btnNot.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(isError){
return;
}
String str=displayField.getText();
str=str.replace(",","");
if(str.indexOf(".")!=-1){
str=str.substring(0,str.indexOf("."));
}
if(rbHexadecimal.isSelected()){
str=CarryConvert.todec(str,16).toString();
}
else if(rbOctal.isSelected()){
str=CarryConvert.todec(str,8).toString();
}
else if(rbBinary.isSelected()){
str=CarryConvert.todec(str,2).toString();
}
try{
int num=Integer.parseInt(str);
num=~num;//进行取反操作
//setGroupingState();
if(rbHexadecimal.isSelected()){
str=Integer.toHexString(num);
}
else if(rbOctal.isSelected()){
str=Integer.toOctalString(num);
}
else if(rbBinary.isSelected()){
str=Integer.toBinaryString(num);
}
else{
str=num+"";
}
displayField.setText(str);
isOperandBegin=true;
isDot=false;
}
catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null,"对不起,该数值已超出本计算器的计算范围!计算中止!","提示",JOptionPane.ERROR_MESSAGE);
displayField.setText("计算操作中止。");
isError=true;
return;
}
}
});
//创建Int按钮
btnInt=new JButton("Int");
btnInt.setForeground(Color.RED);
btnInt.setMargin(new Insets(0,1,2,1));
container.add(btnInt);
btnInt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
if(isError){
return;
}
String str=displayField.getText();
str=str.replace(",","");
double num=Double.parseDouble(str);
if(cbInv.isSelected()){
displayField.setText((num-Math.floor(num))+"");
cbInv.setSelected(false);
}else{
displayField.setText(Math.floor(num)+"");
}
isOperandBegin=true;
isDot=false;
}
});
//改变标题栏窗口左侧默认图标
Toolkit tk=Toolkit.getDefaultToolkit();
Image image=tk.createImage("icons/calculator.png");
this.setIconImage(image);
//创建统计框
dlgStatistic=new JFrame("统计框");
dlgStatistic.setSize(227,163);
dlgStatistic.setLocationRelativeTo(this);
dlgStatistic.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//清空JList里的内容
vecListData.removeAllElements();
btnAve.setEnabled(false);
btnSum.setEnabled(false);
btnS.setEnabled(false);
btnDat.setEnabled(false);
dlgStatistic.setVisible(false);
}
});
dlgcon=dlgStatistic.getContentPane();
dlgcon.setLayout(null);
//创建数据显示列表
vecListData=new Vector();
datalist=new JList(vecListData);
datalist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
datalist.setVisibleRowCount(5);
datalist.setFixedCellWidth(39);
datalist.setFixedCellHeight(12);
datalist.setAutoscrolls(true);
datalist.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me) {
// TODO 自动生成方法存根
if(me.getClickCount()==2){
displayField.setText(datalist.getSelectedValue().toString());
}
}
});
scroll=new JScrollPane(datalist);
scrollbar=scroll.getVerticalScrollBar();
scroll.setWheelScrollingEnabled(true);
scroll.setBounds(4,5,208,64);
dlgcon.add(scroll);
//创建返回按钮
btnReturn=new JButton("返回(R)");
btnReturn.setMnemonic('R');
btnReturn.setForeground(Color.BLACK);
btnReturn.setMargin(new Insets(0,1,2,0));
dlgcon.add(btnReturn);
btnReturn.setBounds(5,87,50,20);
btnReturn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
//主窗口获取焦点
mainFrameGetFocus();
}
});
//创建加载按钮
btnLoad=new JButton("加载(L)");
btnLoad.setMnemonic('L');
btnLoad.setForeground(Color.BLACK);
btnLoad.setMargin(new Insets(0,1,2,0));
dlgcon.add(btnLoad);
btnLoad.setBounds(59,87,50,20);
btnLoad.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
// TODO 自动生成方法存根
displayField.setText(datalist.getSelectedValue().toString());
}
});
//创建清零按钮
btnClearSelect=new JButton("清零(C)");
btnClearSelect.setMnemonic('C');
btnClearSelect.setForeground(Color.BLACK);
btnClearSelect.setMargin(new Insets(0,1,2,0));
dlgcon.add(btnClearSelect);
btnClearSelect.setBounds(113,87,50,20);
btnClearSelect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
int index=datalist.getSelectedIndex();
vecListData.removeElementAt(index);
datalist.setListData(vecListData);
if(index>=vecListData.size()){
datalist.setSelectedIndex(index-1);
}else{
datalist.setSelectedIndex(index);
}
lblDataCount.setText("n="+vecListData.size());
}
});
//创建全清按钮
btnClearAll=new JButton("全清(A)");
btnClearAll.setMnemonic('A');
btnClearAll.setForeground(Color.BLACK);
btnClearAll.setMargin(new Insets(0,1,2,0));
dlgcon.add(btnClearAll);
btnClearAll.setBounds(167,87,50,20);
btnClearAll.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根 ;
vecListData.removeAllElements();
datalist.setListData(vecListData);
lblDataCount.setText("n="+vecListData.size());
}
});
//创建显示数据个数的标签
lblDataCount=new JLabel("n=0");
dlgcon.add(lblDataCount);
lblDataCount.setBounds(98,111,60,15);
dlgStatistic.setIconImage(image);
//创建帮助提示信息的对话框(无边框)
dlgTips=new JDialog();
dlgtipscon=dlgTips.getContentPane();
lblTips=new JEditorPane();
lblTips.setEditable(false);
Icon shadowicon=new ImageIcon("icons/shadow.png");
lblTips.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0,0,6,7,shadowicon),BorderFactory.createMatteBorder(1,1,1,1,Color.BLACK)));
lblTips.addMouseListener(this);
dlgtipscon.add(lblTips);
lblTips.setSize(330,80);
lblTips.setBackground(new Color(255,255,238));
dlgTips.setUndecorated(true);
dlgTips.addMouseListener(this);
dlgTips.addFocusListener(this);
///////////////////////////////////////////////////////////
//btnBackspace注册右键菜单事件
rbHexadecimal.addMouseListener(this);
rbDecimalism.addMouseListener(this);
rbOctal.addMouseListener(this);
rbBinary.addMouseListener(this);
rbAngle.addMouseListener(this);
rbRadian.addMouseLi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -