这是上学期Java的期末设计啦,给大家互相借鉴下,加上搜集资料和自己的理解整合处来的一个多功能计算器,
下面看图片及代码,还有有什么问题可以留言哦,
嗯,这里一共分为五个类来写,第一个来类是JSQ是主界面,然后剩下的类都是功能类
package qm;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSQ extends Frame implements ActionListener, WindowListener
{
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField; //计算结果显示区
private String lastCommand; //保存+,-,*,/,=命令0
private double result; //保存计算结果
private boolean start; //判断是否为数字的开始
private JMenuBar menubar;
private JMenuItem m_exit;
private JMenuItem m2_ejz;
private JMenuItem m2_bjz;
private Dialog dialog;
private Label label_dialog;
private JButton button_sqrt;
private JButton button_plusminus;
private JButton button_CE;
private JButton button_cancel;
private JButton button_1;
private JButton button_2;
private JButton button_3;
private JButton button_4;
private JButton button_5;
private JButton button_6;
private JButton button_7;
private JButton button_8;
private JButton button_9;
private JButton button_0;
private JButton button_plus;
private JButton button_minus;
private JButton button_multiply;
private JButton button_divide;
private JButton button_point;
private JButton button_equal;
private JButton button_log;
private JButton button_tan;
private JButton button_cos;
private JButton button_sin;
private JButton button_exp;
public JSQ() //构造方法设置布局、为按钮注册事件监听器
{
super( "科学计算器" );
this.setLocation( 300,200 );
this.setSize( 328,424);
this.setResizable( true );
this.setLayout( new GridLayout( 7,1 ) );//网格布局
this.addmyMenu(); //调用成员方法添加菜单
displayField = new JTextField( 30 );
displayField.setBackground(new Color(233,240,247));
displayField.setForeground(new Color(30,57,91));
// 设置outputField内的默认内容,应该显示为“0”
displayField.setText("0");
displayField.setFont(new Font("宋体",Font.BOLD,25));
// 禁止从界面(键盘)向outputField输入信息,其内容只能通过程序内部改变
displayField.setEditable(false);
this.setResizable(false);
// displayField.setBackground(Color.blue);
this.add( displayField );
//displayField.setEditable( true );
start = true;
result = 0;
lastCommand = "=";
JPanel panel0 = new JPanel();
panel0.setLayout( new GridLayout( 1,4,4,4 ) );
JPanel panel1 = new JPanel();
panel1.setLayout( new GridLayout( 1,5,4,4 ) );
this.add( panel1 );
button_sqrt = new JButton( "sqrt" );//根号
button_sqrt.setBackground(new Color(233,240,247));
//button_sqrt.setForeground(new Color(30,57,91));
// button_sqrt.setBackground(Color.blue);
button_plusminus = new JButton( "+/-" );
button_plusminus.setBackground(new Color(233,240,247));
//button_plusminus.setForeground(new Color(30,57,91));
//button_plusminus.setBackground(Color.blue);
button_exp = new JButton( "exp" );//底数e的n次幂
button_exp.setBackground(new Color(233,240,247));
//button_exp.setForeground(new Color(30,57,91));
// button_exp.setBackground(Color.blue);
button_CE = new JButton( "退位");
button_CE.setBackground(new Color(233,240,247));
button_CE.setForeground(new Color(30,57,91));
// button_CE.setBackground(Color.blue);
button_cancel = new JButton( "CE" );//清除
button_cancel.setBackground(new Color(233,240,247));
button_cancel.setForeground(new Color(30,57,91));
// button_cancel.setBackground(Color.blue);
JPanel panel2 = new JPanel();
panel2.setLayout( new GridLayout( 1,5,4,4 ) );
this.add( panel2 );
button_7 = new JButton( "7" );
button_7.setBackground(new Color(233,240,247));
button_7.setForeground(new Color(30,57,91));
// button_7.setBackground(Color.blue);
button_8 = new JButton( "8" );
button_8.setBackground(new Color(233,240,247));
button_8.setForeground(new Color(30,57,91));
// button_8.setBackground(Color.blue);
button_9 = new JButton( "9" );
button_9.setBackground(new Color(233,240,247));
button_9.setForeground(new Color(30,57,91));
// button_9.setBackground(Color.blue);
button_log = new JButton( "log" );//对数
button_log.setBackground(new Color(233,240,247));
button_log.setForeground(new Color(30,57,91));
// button_log.setBackground(Color.blue);
button_divide = new JButton( "/" );//除
button_divide.setBackground(new Color(233,240,247));
button_divide.setForeground(new Color(30,57,91));
// button_divide.setBackground(Color.blue);
JPanel panel3 = new JPanel();
panel3.setLayout( new GridLayout(1,5,4,4) );
this.add( panel3 );
button_4 = new JButton( "4" );
button_4.setBackground(new Color(233,240,247));
button_4.setForeground(new Color(30,57,91));
// button_4.setBackground(Color.blue);
button_5 = new JButton( "5" );
button_5.setBackground(new Color(233,240,247));
button_5.setForeground(new Color(30,57,91));
// button_5.setBackground(Color.blue);
button_6 = new JButton( "6" );
button_6.setBackground(new Color(233,240,247));
button_6.setForeground(new Color(30,57,91));
// button_6.setBackground(Color.blue);
button_tan = new JButton( "tan" );//正切
button_tan.setBackground(new Color(233,240,247));
button_tan.setForeground(new Color(30,57,91));
//.setBackground(Color.blue);
button_multiply = new JButton( "*" );//乘法
button_multiply.setBackground(new Color(233,240,247));
button_multiply.setForeground(new Color(30,57,91));
// button_multiply.setBackground(Color.blue);
JPanel panel4=new JPanel();
panel4.setLayout( new GridLayout( 1,5,4,4 ) );
this.add(panel4);
button_1 = new JButton( "1" );
button_1.setBackground(new Color(233,240,247));
button_1.setForeground(new Color(30,57,91));
// button_1.setBackground(Color.blue);
button_2 = new JButton( "2" );
button_2.setBackground(new Color(233,240,247));
button_2.setForeground(new Color(30,57,91));
// button_2.setBackground(Color.blue);
button_3 = new JButton( "3" );
button_3.setBackground(new Color(233,240,247));
button_3.setForeground(new Color(30,57,91));
// button_3.setBackground(Color.blue);
button_cos = new JButton( "cos");//余弦
button_cos.setBackground(new Color(233,240,247));
button_cos.setForeground(new Color(30,57,91));
// button_cos.setBackground(Color.blue);
button_minus = new JButton( "-" );
button_minus.setBackground(new Color(233,240,247));
button_minus.setForeground(new Color(30,57,91));
// button_minus.setBackground(Color.blue);
JPanel panel5 = new JPanel();
panel5.setLayout( new GridLayout( 1,5,4,4 ) );
this.add( panel5 );
button_point=new JButton( "." );
button_point.setBackground(new Color(233,240,247));
button_point.setForeground(new Color(30,57,91));
button_0 = new JButton( "0" );
button_0.setBackground(new Color(233,240,247));
button_0.setForeground(new Color(30,57,91));
// button_0.setBackground(Color.blue);
// button_point=new JButton( "." );
// button_point.setBackground(new Color(233,240,247));
// button_point.setForeground(new Color(30,57,91));
//button_point.setBackground(Color.blue);
button_equal = new JButton( "=" );
button_equal.setBackground(new Color(233,240,247));
button_equal.setForeground(new Color(30,57,91));
// button_equal.setBackground(Color.blue);
button_sin = new JButton( "sin" );//正弦
button_sin.setBackground(new Color(233,240,247));
button_sin.setForeground(new Color(30,57,91));
// button_sin.setBackground(Color.blue);
button_plus = new JButton( "+" );
button_plus.setBackground(new Color(233,240,247));
button_plus.setForeground(new Color(30,57,91));
// button_plus.setBackground(Color.blue);
panel1.add( button_sqrt );
panel1.add( button_plusminus );
panel1.add( button_exp );
panel1.add( button_CE );
panel1.add( button_cancel );
panel2.add( button_7 );
panel2.add( button_8 );
panel2.add( button_9 );
panel2.add( button_log );
panel2.add( button_divide );
panel3.add( button_4 );
panel3.add( button_5 );
panel3.add( button_6 );
panel3.add( button_tan );
panel3.add( button_multiply );
panel4.add( button_1 );
panel4.add( button_2 );
panel4.add( button_3 );
panel4.add( button_cos );
panel4.add( button_minus );
panel5.add( button_point );
panel5.add( button_0 );
// panel5.add( button_point );
panel5.add( button_equal );
panel5.add( button_sin );
panel5.add( button_plus) ;
button_sqrt.addActionListener( this );
button_plusminus.addActionListener( this );
button_exp.addActionListener( this );
button_CE.addActionListener( this );
button_cancel.addActionListener( this );
button_7.addActionListener( this );
button_8.addActionListener( this );
button_9.addActionListener( this );
button_log.addActionListener( this );
button_divide.addActionListener( this );
button_4.addActionListener( this );
button_5.addActionListener( this );
button_6.addActionListener( this );
button_tan.addActionListener( this );
button_multiply.addActionListener( this );
button_1.addActionListener( this );
button_2.addActionListener( this );
button_3.addActionListener( this );
button_cos.addActionListener( this );
button_minus.addActionListener( this );
button_0.addActionListener( this );
button_point.addActionListener( this );
button_equal.addActionListener( this );
button_sin.addActionListener( this );
button_plus.addActionListener( this );
this.addWindowListener( new WinClose() ); //注册窗口监听器
this.setVisible( true );
}
private void addmyMenu() //菜单的添加
{
JMenuBar menubar = new JMenuBar();
this.add( menubar );
JMenu m1 = new JMenu( "查看" );
JMenu m2 = new JMenu( "进制转换" );
JMenuItem m1_standardcalculator=new JMenuItem("标准型计算器");
m1_standardcalculator.addActionListener(this);
JMenuItem m1_exit = new JMenuItem( "退出" );
m1_exit.addActionListener( this );
JMenuItem m2_ejz = new JMenuItem( "二进制" );
m2_ejz.addActionListener( this );
JMenuItem m2_ternary=new JMenuItem("三进制");
m2_ternary.addActionListener(this);
JMenuItem m2_bjz = new JMenuItem("八进制");
m2_bjz.addActionListener( this );
JMenuItem m2_sljz = new JMenuItem("十六进制");
m2_sljz.addActionListener( this );
JMenu m3 = new JMenu( "帮助" );
JMenuItem m3_Help = new JMenuItem( "用法" );
m3_Help.addActionListener( this );
// dialog = new Dialog( this, "提示" , true ); //模式窗口
// dialog.setSize( 240,80 );
//label_dialog = new Label("", Label.CENTER ); //标签的字符串为空,居中对齐
// dialog.add( label_dialog );
//dialog.addWindowListener( this ); //为对话框注册窗口事件监听器
JMenu m4 =new JMenu("关于");
JMenuItem m4_developer=new JMenuItem("开发者");
m4_developer.addActionListener(this);
// dialog =new Dialog(this,"",true);
// dialog.setSize(380,100);
// label_dialog =new Label("",Label.CENTER);
// dialog.add(label_dialog);
//dialog.addWindowListener(this);
JMenuItem m4_developmentTime=new JMenuItem("开发时间");
m4_developmentTime.addActionListener(this);
dialog =new Dialog(this,"",true); //模式窗口
dialog.setSize(380,100);
label_dialog =new Label("",Label.CENTER); //标签的字符串为空,居中对齐
dialog.add(label_dialog);
dialog.addWindowListener(this);//为对话框注册窗口事件监听器
JMenuItem m5_unitconversion = new JMenuItem( "单位转换器" );
m5_unitconversion .addActionListener( this );
JMenuItem m6_Temperature = new JMenuItem( "温度转换器" );
m6_Temperature.addActionListener( this );
m6_Temperature.setBackground(new Color(233,240,247));
m6_Temperature.setForeground(new Color(30,57,91));
m6_Temperature.setBackground(new Color(233,240,247));
m6_Temperature.setForeground(new Color(30,57,91));
m6_Temperature.setBackground(new Color(233,240,247));
m6_Temperature.setForeground(new Color(30,57,91));
m1.add(m1_standardcalculator);
m1.add(m5_unitconversion);
m1.add(m6_Temperature);
m1.add( m1_exit );
menubar.add( m1 );
m2.add( m2_ejz );
m2.add(m2_ternary);
m2.add( m2_bjz );
m2.add( m2_sljz );
menubar.add( m2 );
m3.add( m3_Help );
menubar.add( m3 );
m4.add(m4_developer);
m4.add(m4_developmentTime);
menubar.add(m4);
m6_Temperature.setBackground(new Color(233,240,247));
m6_Temperature.setForeground(new Color(30,57,91));
}
public void actionPerformed(ActionEvent e) //按钮的单击事件处理方法
{
if(
e.getSource().equals( button_1 )||e.getSource().equals( button_2 )||
e.getSource().equals( button_3 )||e.getSource().equals( button_4 )||
e.getSource().equals( button_5 )|| e.getSource().equals( button_6 )||
e.getSource().equals( button_7 )|| e.getSource().equals( button_8 )||
e.getSource().equals( button_9 ) ||e.getSource().equals( button_0 )||
e.getSource().equals( button_point )||e.getSource().equals( button_plusminus )||
e.getSource().equals( button_cancel )||e.getSource().equals( button_CE )
)
{ //非运算符的处理方法
String input = e.getActionCommand();
if ( start )
{
displayField.setText("");
start = false;
if( input.equals( "+/-" ) )
displayField.setText( displayField.getText()+ "-" );
}
if( !input.equals( "+/-" ) )
{
String str = displayField.getText();
if( input.equals( "退位") ) //退格键的实现方法
{
if( str.length() > 0 )
displayField.setText( str.substring( 0,str.length() - 1 ) );
}
else if( input.equals( "CE" ) ) //清零键的实现方法
{
displayField.setText( "0");
start = true;
}
else
displayField.setText( displayField.getText() + input );
}
}
else if ( e.getActionCommand()=="二进制") //二进制的转换
{
int n = Integer.parseInt( displayField.getText() );
displayField.setText(Integer.toBinaryString(n));
//displayField.setText( Integer.toBinaryString( n ) );
//SysConvert2 a=new SysConvert2();
//a.main(null);
//SysConvert2 convert = new SysConvert2();
//convert.setVisible(true);
//this.dispose();
}
else if(e.getActionCommand()=="三进制")//三进制的转换
{
int n=Integer.parseInt(displayField.getText());
displayField.setText(Integer.toString(n,3));
}
else if ( e.getActionCommand() == "八进制" ) //八进制的转换
{
int n = Integer.parseInt( displayField.getText() );
displayField.setText( Integer.toOctalString( n ) );
//displayField.setText(Integer.toBinaryString(n));
}
else if ( e.getActionCommand() == "十六进制" ) //十六进制的转换
{
int n = Integer.parseInt( displayField.getText() );
displayField.setText( Integer.toHexString( n ) );
}
else if(e.getActionCommand()=="标准型计算器")
{
this.dispose();
StandardCalculator sc=new StandardCalculator();
sc.main(null);
}
else if(e.getActionCommand()=="单位转换器")
{
this.dispose();
UnitTransfer un=new UnitTransfer();
un.main(null);
}
else if(e.getActionCommand()=="温度转换器")
{
WenDuChang wd=new WenDuChang();
this.dispose();
}
else if ( e.getActionCommand() == "退出" ) //选项中退出的处理方法
{
System.exit( 0 );
}
else if ( e.getActionCommand() == "用法" ) //按下'帮助'菜单栏中用法的处理方法
{
label_dialog.setText( "sqrt,exp等键是先输运算符再输数字,先输入数字再点进制转换\n" );
dialog.setLocation( 400,250 );
dialog.setVisible( true );
}
else if(e.getActionCommand()=="开发者")
{
label_dialog.setText( "182017331,陈锦贤 182017505, 吴洪缤 182017190,王浩铸" );
dialog.setLocation( 400,250 );
dialog.setVisible( true );
}
else if(e.getActionCommand()=="开发时间")
{
label_dialog.setText( "开发于2019年6月17号" );
dialog.setLocation( 400,250 );
dialog.setVisible( true );
}
else //各运算符的识别
{
String command = e.getActionCommand();
if( start )
{
lastCommand = command;
}
else
{
calculate( Double.parseDouble( displayField.getText() ) );
lastCommand = command;
start = true;
}
}
}
public void calculate( double x ) //各运算符的具体运算方法
{
double d = 0;
if ( lastCommand.equals( "+" ) )
result += x;
else if (lastCommand.equals( "-" ) )
result -= x;
else if ( lastCommand.equals( "*" ) )
result *= x;
else if ( lastCommand.equals( "/" ) )
result /= x;
else if ( lastCommand.equals( "=" ) )
result = x;
else if ( lastCommand.equals( "sqrt" ) )
{
d = Math.sqrt( x );
result = d;
}
else if ( lastCommand.equals( "exp" ) )
{
d = Math.exp( x );
result = d;
}
else if ( lastCommand.equals( "log" ) )
{
d = Math.log( x );
result = d;
}
else if ( lastCommand.equals( "tan" ) )
{
d = Math.tan(x);
result = d;
}
else if ( lastCommand.equals( "cos" ) )
{
d = Math.cos( x );
result = d;
}
else if ( lastCommand.equals( "sin" ) )
{
d = Math.sin( x );
result = d;
}
displayField.setText( ""+ result );
}
public void windowClosing( WindowEvent e )
{
if( e.getSource() == dialog )
dialog.setVisible( false ); //隐藏对话框
else
System.exit( 0 );
}
public void windowOpened( WindowEvent e ) { }
public void windowActivated( WindowEvent e ) { }
public void windowDeactivated( WindowEvent e ) { }
public void windowClosed( WindowEvent e ) { }
public void windowIconified( WindowEvent e ) { }
public void windowDeiconified( WindowEvent e ) { }
public static void main( String args[] )
{
JSQ calculator = new JSQ();
}
}
class WinClose implements WindowListener
{
public void windowClosing( WindowEvent e ) //单击窗口关闭按钮时触发并执行实现窗口监听器接口中的方法
{
System.exit( 0 ); //结束程序运行
}
public void windowOpened( WindowEvent e ){ }
public void windowActivated( WindowEvent e ){}
public void windowDeactivated( WindowEvent e){ }
public void windowClosed( WindowEvent e ){ }
public void windowIconified( WindowEvent e ){ }
public void windowDeiconified( WindowEvent e ){ }
}
//第二个类
package qm;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class StandardCalculator extends JFrame{
private JTextField display1,display2,display3;
private JButton num[],operate[];
private String name[]={"MC","MR","MS","M+","M-","←","CE","C","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+"};
private operateNum op1=new operateNum(),op2=new operateNum();//操作数1,操作数2
private storageField storage=new storageField();//存储区
private String action="op1";//表示要操作的对象 "op1"操作第一个操作数,"op2"操作第二个操作数
private String sign="";//运算符,默认为空
private String screen1,screen2;
private boolean Disable=false;
public StandardCalculator()
{
super("标准计算器");
this.setLayout(null);
this.setSize(328, 424);
this.setResizable(false);
this.setLocation(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
display1=new JTextField(30);
// this.add(display1);
// display1.setEditable(true);
final StandardCalculator a = this;
JPanel jp=new JPanel(null);
//jp.setBounds(10,15,getWidth()-24,getHeight());
jp.setBounds(10,15,getWidth()-24,getHeight());
jp.setBackground(new Color(217,228,241));
this.getContentPane().setBackground(new Color(217,228,241));
/**显示屏**/
display1=new JTextField("");
display2=new JTextField("0");
display3=new JTextField("");
jp.add(display1);
jp.add(display3);
jp.add(display2);
this.add(jp);
display1.setEnabled(false);
display2.setEnabled(false);
display3.setEnabled(false);
display1.setBounds(0, 0, 200, 34);
display2.setBounds(20, 20, 180, 34);
display3.setBounds(0, 20, 30, 34);
display1.setHorizontalAlignment(JLabel.RIGHT);
display2.setHorizontalAlignment(JLabel.RIGHT);
display3.setHorizontalAlignment(JLabel.CENTER);
display1.setFont(new Font("宋体",Font.PLAIN,12));
display2.setFont(new Font("宋体",Font.BOLD,20));
display3.setFont(new Font("宋体",Font.PLAIN,20));
display1.setDisabledTextColor(Color.BLACK);
display2.setDisabledTextColor(Color.BLACK);
display3.setDisabledTextColor(Color.BLACK);
display1.setBorder(new LineBorder(new Color(242,247,252)));
display2.setBorder(new LineBorder(new Color(242,247,252)));
display3.setBorder(new LineBorder(new Color(242,247,252)));
display1.setBackground(new Color(242,247,252));
display2.setBackground(new Color(242,247,252));
display3.setBackground(new Color(242,247,252));
/**按钮 35px*29px 28个***/
int i;
operate=new JButton[28];
/**前24个**/
for(i=0;i<24;i++)
{
operate[i]=new JButton(name[i]);
operate[i].setMargin(new java.awt.Insets(0,0,0,0));
operate[i].setBounds(i%5*(35+7+15), 60+i/5*(29+5+15)+5, 35, 29);
jp.add(operate[i]);
}
JButton jb1= new JButton("返回"); //返回复杂计算器
jb1.setBackground(new Color(233,240,247));
jb1.setForeground(new Color(30,57,91));
jb1.setVisible(true);
jb1.setBounds(210, 0, 110, 40);
jb1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
a.setVisible(false);
JSQ calculator = new JSQ();
}
});
jp.add(jb1);
//添加按钮
/***=号***/
operate[i]=new JButton(name[i]);
operate[i].setMargin(new java.awt.Insets(0,0,0,0));
operate[i].setBounds(i%5*(35+7+15), 60+i/5*(29+5+15)+5, 35, 29*2+5);
jp.add(operate[i]);
i++;
/***0号***/
operate[i]=new JButton(name[i]);
operate[i].setMargin(new java.awt.Insets(0,0,0,0));
operate[i].setBounds(i%5*(35+7+15), 60+i/5*(29+5+15)+5, 35*2+7, 29);
jp.add(operate[i]);
/**前24个**/
for(i=i+1;i=16)//计算器屏幕所能显示数据的最大长度
{
display2.setFont(new Font("宋体",Font.BOLD,14));//缩小字体输出
display2.setText(str.substring(0, 16));
}
else
display2.setText(str);
}
public void cut()
{
run("-");
}
public void add()
{
run("+");
}
public void sum()
{
display2.setFont(new Font("宋体",Font.BOLD,20));
int d1=op1.dotWei,d2=op2.dotWei,i;
if(sign.equals("+")){
op1.value=op1.value+op2.value;
}
if(sign.equals("-")){
op1.value=op1.value-op2.value;
}
if(sign.equals("*")){
op1.value=op1.value*op2.value;
}
if(sign.equals("/")){
op1.value=op1.value/op2.value;
}
if(sign.equals("%")){
op1.value=op1.value%op2.value;
}
// switch(sign)
// { //运算后 结果保存到op1中
// case "+":op1.value=op1.value+op2.value;break;
// case "-":op1.value=op1.value-op2.value;break;
// case "*":op1.value=op1.value*op2.value;break;
// case "/":op1.value=op1.value/op2.value;break;
// case "%":op1.value=op1.value%op2.value;break;
// }
if(op2.value==0&&sign=="/")//除数为0
{
Disable=true;
display2.setText(op1.value+"");
display1.setText(null);
action="op1";
return ;
}
if(op1.value==Math.floor(op1.value))//结果为整数
{
display2.setText((int)op1.value+"");
op1.dotWei=0;
op1.isFloat=false;
}
else{//结果为小数
String str=op1.value+"";
//准确控制算术运算结果的精度,加,减,取模运算,小数点后的有效数字最多为max(d1,d2)位
if(sign.equals("+")||sign.equals("-")||sign.equals("%"))
{
i=d1>d2?d1:d2;
str=op1.value+"";
str=str.substring(0, str.indexOf(".")+i+1);//取i位输出
}
//准确控制算术运算结果的精度,乘法运算,小数点后的有效数字最多为d1+d2位
else if(sign.equals("*"))
{
i=d1+d2;
BigDecimal bd=new BigDecimal(op1.value);
op1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue();
str=op1.value+"";//更新修改后的str
}
//结果超过显示数据的最大长度
if(str.length()>=16)
{
display2.setFont(new Font("宋体",Font.BOLD,14));
str=str.substring(0, 16);
}
display2.setText(str);
op1.dotWei=str.length()-str.indexOf(".")-1;//更新op1w值
}
display1.setText(null);
action="op1";
op1.clear=true;//开始新的表达式运算时,op1要先重置
op2.clear=true;//开始新的表达式运算时,op2要先重置
sign="";
}
public void dot()
{
if(action=="op1")
op1.isFloat=true;
else
op2.isFloat=true;
display2.setText(display2.getText()+".");
}
public void run(String SIGN)
{
display2.setFont(new Font("宋体",Font.BOLD,20));
action="op2";
int d1=op1.dotWei,d2=op2.dotWei,i;
if(!sign.equals(""))//检测是否为以为表达式的第一运算
{
if(sign.equals("+")){
op1.value=op1.value+op2.value;
}
if(sign.equals("-")){
op1.value=op1.value-op2.value;
}
if(sign.equals("*")){
op1.value=op1.value*op2.value;
}
if(sign.equals("/")){
op1.value=op1.value/op2.value;
}
if(sign.equals("%")){
op1.value=op1.value%op2.value;
}
// switch(sign)
// {//运算后 结果保存到op1中
// case "+":op1.value=op1.value+op2.value;break;
// case "-":op1.value=op1.value-op2.value;break;
// case "*":op1.value=op1.value*op2.value;break;
// case "/":op1.value=op1.value/op2.value;break;
// case "%":op1.value=op1.value%op2.value;break;
// }
}
String temp=isSpecileHandle();
if(temp==null)
temp=display2.getText();//先保存display2文本框里的数据
if(op2.value==0&&sign=="/")//除数为0
{
Disable=true;
display2.setText(op1.value+"");
display1.setText(display1.getText()+op1.value);
}
if(op1.value==Math.floor(op1.value))//结果为整数
{
display2.setText((int)op1.value+"");
op1.dotWei=0;
op1.isFloat=false;
}
else{
String str=op1.value+"";
//准确控制算术运算结果的精度,加,减,取模运算,小数点后的有效数字最多为max(d1,d2)位
if(sign.equals("+")||sign.equals("-")||sign.equals("%"))
{
i=d1>d2?d1:d2;
BigDecimal bd=new BigDecimal(op1.value);
op1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue();
str=op1.value+"";//更新修改后的str
}
//准确控制算术运算结果的精度,乘法运算,小数点后的有效数字最多为d1+d2位
else if(sign.equals("*"))
{
i=d1+d2;
BigDecimal bd=new BigDecimal(op1.value);
op1.value=bd.setScale(i,BigDecimal.ROUND_DOWN ).doubleValue();
str=op1.value+"";//更新修改后的str
}
//结果超过显示数据的最大长度
if(str.length()>=16)
{
display2.setFont(new Font("宋体",Font.BOLD,14));
str=str.substring(0, 16);
}
display2.setText(str);
op1.dotWei=str.length()-str.indexOf(".")-1;//每次加完后,如果结果op1的值为小数则更新op1w的值
}
sign=SIGN;
display1.setText(display1.getText()+temp+sign);
op2.value=op1.value;//运算后,操作数op2默认的值为op1的值
op2.clear=true;//下一次键入数据,op2要重置
op1.clear=false;//下一次键入数据,op1不要重置
}
/*
* isSpecileHandle()
* 操作数是否sqrt()或1/x过,
* 如果有,则返回"sqrt(x)"或"1/x"字符串
*/
public String isSpecileHandle()
{
String temp=null;
if(op1.isSqrted)
{
temp=op1.sqrtedString;
op1.isSqrted=false;
}
else if(op2.isSqrted)
{
temp=op2.sqrtedString;
op2.isSqrted=false;
}
if(op1.isInverted)
{
temp=op1.invertedString;
op1.isInverted=false;
}
else if(op2.isInverted)
{
temp=op2.invertedString;
op2.isInverted=false;
}
return temp;
}
class keyAdapter extends KeyAdapter{
public void keyPressed(KeyEvent e)
{
int keycode=e.getKeyCode();
if((keycode>=96&&keycode<=105)||(keycode>=48&&keycode<=57))//数字键
{
if(keycode>=96)
keycode-=48;
switch(keycode)
{
case 48:read(0);break;
case 49:read(1);break;
case 50:read(2);break;
case 51:read(3);break;
case 52:read(4);break;
case 53:read(5);break;
case 54:read(6);break;
case 55:read(7);break;
case 56:read(8);break;
case 57:read(9);break;
}
}
//运算符键+ - * / =和. 以及退格键(* =号)
else if((keycode==110||keycode==46)||(keycode==111||keycode==47)||(keycode==45||keycode==109)||keycode==107||keycode==106||keycode==61||keycode==10||keycode==8)
{
if((keycode==110||keycode==46))
dot();
else if(keycode==111||keycode==47)
run("/");
else if(keycode==45||keycode==109)
run("-");
else if(keycode==107)
run("+");
else if(keycode==106)
run("*");
else if(keycode==61||keycode==10)//=号
sum();
else if(keycode==8)
cutEnd();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//StandardCalculator sc=new StandardCalculator();
}
}
//第三个类
package qm;
public class StandardCalculatorText {
public static void main(String[] args) {
// TODO Auto-generated method stub
//StandardCalculator sc=new StandardCalculator();
}
}
class operateNum {//操作数类
public double value;//操作数的实际值
public int dotWei;//操作数如果是小数,记录小数点后的位数
public String invertedString;//1/x字符串
public String sqrtedString;//sqrt(x)字符串
public boolean isSqrted,isInverted;//做标记是否√,1/x过,用于后续判断
public boolean clear;//clear为真表示是否重置操作数
public boolean isFloat;//isFloat为真,表示操作数是小数
public operateNum()
{
value=0;
dotWei=0;
sqrtedString=null;
invertedString=null;
isSqrted=false;
isInverted=false;
clear=false;
isFloat=false;
}
public void reset()//重置操作数
{
value=0;
dotWei=0;
sqrtedString=null;
invertedString=null;
isSqrted=false;
isInverted=false;
clear=false;
isFloat=false;
}
}
class storageField {//存储区
public double storageNum;//存储区存储的数据
public boolean storageMode;//是否启动存储区
public storageField()
{
storageNum=0;
storageMode=false;
}
}
//第四个类
package qm;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class UnitTransfer extends JFrame{
private final int WIDTH = 324, HEIGHT = 424;//窗口默认的宽度、高度
private JLabel lblInUnit=new JLabel("输入单位");
private JComboBox cboIn=new JComboBox(new String[]{"", ""});
private JLabel lblIn=new JLabel("输入数值");
private JTextField txtIn=new JTextField("10");
private JLabel lblOutUnit=new JLabel("输出单位");
private JLabel lblResult=new JLabel("显示结果");
private JLabel txtResult=new JLabel("结果");
private JComboBox cboOut=new JComboBox(new String[]{"", ""});
private JButton btnTrans = new JButton("转换");
private JButton btnClear = new JButton("清空");
private JButton btnreturn=new JButton("返回");
private JRadioButton rdLeng = new JRadioButton("长度");
private JRadioButton rdWeig = new JRadioButton("时间");
private String [] lengthUnit={"米", "分米", "厘米", "毫米"};
private String [] timeUnit={"天", "时", "分", "秒"};
public UnitTransfer(){
super("单位转换器 ");
this.setSize(WIDTH, HEIGHT);
this.setLocation( 300,200 );
this.setResizable(false);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonGroup group = new ButtonGroup();
group.add(rdLeng);
group.add(rdWeig);
this.getContentPane().add(rdLeng);
this.getContentPane().add(rdWeig);
this.getContentPane().add(btnTrans);
this.getContentPane().add(btnClear);
this.getContentPane().add(btnreturn);
this.getContentPane().add(lblIn);
this.getContentPane().add(txtIn);
this.getContentPane().add(lblInUnit);
this.getContentPane().add(cboIn);
this.getContentPane().add(lblResult);
this.getContentPane().add(txtResult);
this.getContentPane().add(lblOutUnit);
this.getContentPane().add(cboOut);
txtIn.setFont(new Font("宋体",Font.BOLD,20));
lblInUnit.setFont(new Font("宋体",Font.BOLD,15));
lblIn.setFont(new Font("宋体",Font.BOLD,15));
lblOutUnit.setFont(new Font("宋体",Font.BOLD,15));
lblResult.setFont(new Font("宋体",Font.BOLD,15));
btnTrans.setFont(new Font("宋体",Font.BOLD,15));
btnClear.setFont(new Font("宋体",Font.BOLD,15));
btnreturn.setBackground(new Color(233,240,247));
btnreturn.setForeground(new Color(30,57,91));
btnClear.setBackground(new Color(233,240,247));
btnClear.setForeground(new Color(30,57,91));
btnTrans.setBackground(new Color(233,240,247));
btnTrans.setForeground(new Color(30,57,91));
cboIn.setBackground(new Color(233,240,247));
cboIn.setForeground(new Color(30,57,91));
btnreturn.setBackground(new Color(233,240,247));
cboOut.setForeground(new Color(30,57,91));
this.setVisible(true);
this.doLayout();
btnTrans.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
doConvert();
}
});
btnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txtIn.setText("0");
txtResult.setText("0");
}
});
btnreturn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JSQ calculator = new JSQ();
boolean flase = false;
setVisible(flase);
}
});
rdLeng.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cboIn.setModel(new DefaultComboBoxModel(lengthUnit));
cboOut.setModel(new DefaultComboBoxModel(lengthUnit));
}
});
rdWeig.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cboIn.setModel(new DefaultComboBoxModel(timeUnit));
cboOut.setModel(new DefaultComboBoxModel(timeUnit));
}
});
rdLeng.setSelected(true);
cboIn.setModel(new DefaultComboBoxModel(lengthUnit));
cboOut.setModel(new DefaultComboBoxModel(timeUnit));
}
final int offX=100;
public void doLayout(){
super.doLayout();
rdLeng.setBounds(offX, 15, 60, 20);
rdWeig.setBounds(rdLeng.getX()+rdLeng.getWidth()+5, 15, 60, 20);
lblInUnit.setBounds(offX, rdLeng.getY()+rdLeng.getHeight()+20, 80, 20);
cboIn.setBounds(lblInUnit.getX()+lblInUnit.getWidth()+5, lblInUnit.getY(), 80, 20);
lblIn.setBounds(offX, lblInUnit.getY()+lblInUnit.getHeight()+5, 80, 20);
txtIn.setBounds(lblIn.getX()+lblIn.getWidth()+5, lblIn.getY(), 80, 20);
lblOutUnit.setBounds(offX, lblIn.getY()+lblIn.getHeight()+30, 80, 20);
cboOut.setBounds(lblOutUnit.getX()+lblOutUnit.getWidth()+5, lblOutUnit.getY(), 80, 20);
lblResult.setBounds(offX, cboOut.getY()+cboOut.getHeight()+5, 80, 20);
txtResult.setBounds(lblResult.getX()+lblResult.getWidth()+5, lblResult.getY(), 100, 20);
int w=getWidth ();
int x=(w-70*2-5)/2;//水平居中
btnTrans.setBounds(x, lblResult.getY()+lblResult.getHeight()+30, 70, 25);
btnClear.setBounds(btnTrans.getX()+btnTrans.getWidth()+3, btnTrans.getY(), 70, 25);
//btnreturn.setBounds(x, lblResult.getY()+lblResult.getHeight()+80, 70, 65);
btnreturn.setBounds(15,12, 70, 25);
}
public void doConvert(){
double v=0;
try{
v= Double.parseDouble(txtIn.getText());
}catch(Exception ex){
txtIn.setText("0");
return;
}
//"米", "分米", "厘米", "毫米"
if(rdLeng.isSelected()){
switch(cboIn.getSelectedIndex()){
case 0:
break;
case 1:
v=v/10;
break;
case 2:
v=v/100;
break;
case 3:
v=v/1000;
break;
default:
return;
}
//v 现在是标准单位:米
switch(cboOut.getSelectedIndex()){
case 0:
break;
case 1:
v=v*10;
break;
case 2:
v=v*100;
break;
case 3:
v=v*1000;
break;
default:
return;
}
if(v<0.01){
txtResult.setText(String.format("%2.8f", v));
}else{
txtResult.setText(String.format("%2.2f", v));
}
}else{
//"天", "时", "分", "秒"
switch(cboIn.getSelectedIndex()){
case 0:
v=v*24;
break;
case 1:
break;
case 2:
v=v/60;
break;
case 3:
v=v/3600;
break;
default:
return;
}
//v 现在是标准单位:小时
switch(cboOut.getSelectedIndex()){
case 0:
v=v/24;
break;
case 1:
break;
case 2:
v=v*60;
break;
case 3:
v=v*3600;
break;
default:
return;
}
if(v<0.01){
txtResult.setText(String.format("%2.8f", v));
}else{
txtResult.setText(String.format("%2.8f", v));
}
}
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
public void run() {
//UnitTransfer un= new UnitTransfer();
}
});
}
}
//最后一个类
package qm;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import java.awt.Button;
import java.text.DecimalFormat;
public class WenDuChang extends JFrame implements ActionListener{
private JTextField textField;
private JTextField textField_1;
private JLabel label;
private JLabel label_1;
private JLabel label_2;
private JRadioButton[] radioButton;
private JButton button;
private JButton button_1;
private ButtonGroup bg;
public static void main( String args[] )
{
//WenDuChang wd=new WenDuChang();
}
public WenDuChang() {
//获取你的屏幕的宽和高
//int width = Toolkit.getDefaultToolkit().getScreenSize().width;
// int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// setLocation(width/2-200, height/2-150);
setTitle("温度转换器");
getContentPane().setLayout(null);
textField = new JTextField();//摄氏温度
textField.setText("0.0");
textField.setFont(new Font("宋体",Font.BOLD,20));
//textField.setBounds(50, 63, 84, 27);
textField.setBounds(170, 33, 104, 37);
getContentPane().add(textField);
textField.setColumns(10);
label = new JLabel("摄氏温度");
//label.setBounds(137,69,54,15);
label.setBounds(60,35,104,37);
getContentPane().add(label);
label_1 = new JLabel("<<==>>");
//label_1.setBounds(201, 69, 54, 15);
label_1.setBounds(130, 84, 104, 45);
getContentPane().add(label_1);
textField_1 = new JTextField();//华氏温度
textField_1.setText("0.0");
textField_1.setFont(new Font("宋体",Font.BOLD,20));
//textField_1.setBounds(, 66, 90, 24);
textField_1.setBounds(170, 140, 104, 37);
getContentPane().add(textField_1);
textField_1.setColumns(10);
label_2 = new JLabel("华氏温度");
//label_2.setBounds(323, 69, 104, 37);
label_2.setBounds(60, 150, 54, 15);
getContentPane().add(label_2);
radioButton = new JRadioButton[2];
radioButton[0] = new JRadioButton("摄氏转华氏",true);
//radioButton[0].setBounds(100, 142, 121, 23);
radioButton[0].setBounds(55, 210, 121, 23);
getContentPane().add(radioButton[0]);
radioButton[1] = new JRadioButton("华氏转摄氏");
//radioButton[1].setBounds(223, 142, 121, 23);
radioButton[1].setBounds(180, 210, 121, 23);
getContentPane().add(radioButton[1]);
bg=new ButtonGroup();
bg.add(radioButton[0]);
bg.add(radioButton[1]);
button = new JButton("温度转化");
//button.setBounds(100, 196, 93, 23);
button.setBounds(50, 270, 93, 23);
button.setBackground(new Color(233,240,247));
button.setForeground(new Color(30,57,91));
button.addActionListener(this);
getContentPane().add(button);
button_1 = new JButton("返回");
// button_1.setBounds(237,196,93,23);
button_1.setBounds(180,270,93,23);
button_1.setBackground(new Color(233,240,247));
button_1.setForeground(new Color(30,57,91));
button_1.addActionListener(this);
getContentPane().add(button_1);
//setSize(450, 300);
this.setLocation( 300,200 );
this.setSize( 328,424);
this.setResizable( true );
this.setResizable(false);
setVisible(true);
}
//用于温度转换的方法-----业务处理
public String WenDuChang(final double tc,final double tf,boolean flag){
double wdc=0.0;
//小数格式化,引号中的0.000表示保留小数点后二位(第四位四舍五入)
DecimalFormat df = new DecimalFormat("0.00");
String str;
if(flag){
wdc=9*(tc-10)/5+50; //摄氏温度转化为华氏温度
}
else{
wdc=5*(tf-50)/9+10; //华氏温度转化为摄氏温度
}
str=df.format(wdc);
return str;
}
@Override
public void actionPerformed(ActionEvent e) {
double tc = Double.parseDouble(textField.getText().trim());
double tf = Double.parseDouble(textField_1.getText().trim());
if(e.getSource()==button){
//System.out.println("按钮被选中.....");
if(radioButton[0].isSelected()){//判断radioButton是否被选中
textField_1.setText(WenDuChang(tc,tf,true));
}
if(radioButton[1].isSelected()){
textField.setText(WenDuChang(tc,tf,false));
}
}
if(e.getSource()==button_1){
//System.exit(0);
JSQ calculator = new JSQ();
this.dispose();
}
}
}