求Java计算器标准的布局代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求Java计算器标准的布局代码相关的知识,希望对你有一定的参考价值。
要求把计算器的布局做成电脑附件里的计算器布局一样,不需要其他算法,只需计算器的布局代码
强调一下不是写在html文件里的,是在Calculator.java里编译的,布局一定要和电脑里附带的那个差不多,不能差太远,最好一样
高手帮忙下,如果满意我会追加分的!~谢谢高手!~!~
import java.awt.*;
import javax.swing.*;
public class Conputer extends JFrame
protected Container con = getContentPane();// 指向内容面板
protected JMenuBar a = new JMenuBar();// 菜单条
protected JMenu a1 = new JMenu("编辑(E)");// 菜单1
protected JMenu a2 = new JMenu("查看(V)");// 菜单2
protected JMenu a3 = new JMenu("帮助(H)");// 菜单3
protected JMenuItem a11 = new JMenuItem("复制(C)", 'C');// 菜单1的菜单项
protected JMenuItem a12 = new JMenuItem("粘贴(P)", 'P');// 菜单1的菜单项
protected JMenuItem a21 = new JMenuItem("标准型(T)", 'T');// 菜单2的菜单项
protected JMenuItem a22 = new JMenuItem("科学型(S)", 'S');// 菜单2的菜单项
protected JMenuItem a31 = new JMenuItem("帮助主题(H)", 'H');// 菜单3的菜单项
protected JTextField jtf = new JTextField(30);// 文本框
protected JButton[] jb = new JButton[27];
protected String[] arr = "Backspace", "CE", "C", "MC", "7", "8", "9", "/", "sqrt",
"MR", "4", "5", "6", "*", "%", "MS", "1", "2", "3", "-", "1/x",
"M+", "0", "+/-", ".", "+", "=" ;
protected JPanel jp = new JPanel();// 主面板
protected JPanel m = new JPanel();// 次面板
protected JPanel m1 = new JPanel();// 次次面板
protected JPanel m2 = new JPanel();// 次次面板
protected JPanel m3 = new JPanel();// 次次面板
protected JPanel m4 = new JPanel();// 次次面板
protected JPanel m5 = new JPanel();// 次次面板
protected JPanel m6 = new JPanel();// 次次面板
protected GridLayout glo = new GridLayout(6, 1, 3, 3);// 主网格布局
protected GridLayout glo1 = new GridLayout(1, 3, 3, 3);// 次网格布局
protected GridLayout glo2 = new GridLayout(1, 6, 3, 3);// 次网格布局
protected Conputer(String s)
super(s);
a1.setMnemonic('E');
a1.add(a11);
a1.add(a12);
a2.setMnemonic('V');
a2.add(a21);
a2.add(a22);
a3.setMnemonic('H');
a3.add(a31);
a.add(a1);
a.add(a2);
a.add(a3);
this.setJMenuBar(a);// 菜单条完成
for (int i = 0; i < 27; i++)
jb[i] = new JButton(arr[i]);
jp.setLayout(glo);
jp.add(m1);
m1.add(jtf);
jtf.setEditable(false); jtf.setText("0."); jtf.setForeground(Color.BLUE); jtf.setBackground(Color.WHITE);
jtf.setHorizontalAlignment(JTextField.RIGHT);//文本显示在右边
jp.add(m2);
m2.setLayout(glo1);
m2.add(jb[0]);
m2.add(jb[1]);
m2.add(jb[2]);
jp.add(m3);
m3.setLayout(glo2);
m3.add(jb[3]);
m3.add(jb[4]);
m3.add(jb[5]);
m3.add(jb[6]);
m3.add(jb[7]);
m3.add(jb[8]);
jp.add(m4);
m4.setLayout(glo2);
m4.add(jb[9]);
m4.add(jb[10]);
m4.add(jb[11]);
m4.add(jb[12]);
m4.add(jb[13]);
m4.add(jb[14]);
jp.add(m5);
m5.setLayout(glo2);
m5.add(jb[15]);
m5.add(jb[16]);
m5.add(jb[17]);
m5.add(jb[18]);
m5.add(jb[19]);
m5.add(jb[20]);
jp.add(m6);
m6.setLayout(glo2);
m6.add(jb[21]);
m6.add(jb[22]);
m6.add(jb[23]);
m6.add(jb[24]);
m6.add(jb[25]);
m6.add(jb[26]);
m.add(jp);
con.add(m);
this.setResizable(false);// 不能用鼠标拉伸窗体
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗体
this.setSize(380, 275);
this.setVisible(true);
protected Conputer()
this("计算器");
public static void main(String[] args)
new Conputer("计算器");
参考技术A //自己刚写完一个,代码有点长,,,但是,,,,,看看吧
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener
///////////////菜单
JMenuBar mb;
JMenu mEdit,mFind,mHelp;
JMenuItem mEditCopy,mEditPaste;
JMenuItem mFindStandarded,mFindScience,mFindGroup;
JMenuItem mHelpTopic,mHelpAbout;
JPanel pan;
/////////////按钮
JTextField tf;
JButton butnull;
JButton butBackspace,butCE,butC;
JButton butMC,butMR,butMS,butM;
JButton but1,but2,but3,but4,but5,but6,but7,but8,but9,but0;
JButton butChu,butSqrt,butCheng,butYu,butJian,but1x,butDot,butJia,butEqual,butJJ;
Calculator()
mb =new JMenuBar();
mEdit =new JMenu("编辑(E)");
mEdit.setMnemonic('E');
mFind =new JMenu("查看(F)");
mFind.setMnemonic('F');
mHelp =new JMenu("帮助(H)");
mFind.setMnemonic('H');
mEditCopy =new JMenuItem("复制(C)");
mEditCopy.setMnemonic('C');
mEditPaste =new JMenuItem("粘贴(P)");
mEditCopy.setMnemonic('P');
mFindStandarded=new JMenuItem("标准型(T)");
mFindStandarded.setMnemonic('T');
mFindScience=new JMenuItem("科学型(S)");
mFindScience.setMnemonic('S');
mFindGroup =new JMenuItem("数字分组(I)");
mFindGroup.setMnemonic('I');
mHelpTopic =new JMenuItem("帮助主题(H)");
mHelpTopic.setMnemonic('H');
mHelpAbout =new JMenuItem("关于我们(A)");
mHelpAbout.setMnemonic('A');
////////////////////////////添加mb组件beginning
mb.add(mEdit);
mb.add(mFind);
mb.add(mHelp);
mEdit.add(mEditCopy);
mEdit.add(mEditPaste);
mFind.add(mFindStandarded);
mFind.add(mFindScience);
mFind.add(mFindGroup);
mHelp.add(mHelpTopic);
mHelp.add(mHelpAbout);//////////////ending
////////////////////////***创建计算机按钮***////////////////////
Color red =new Color(250,0,0);
Color green =new Color(0,127,0);
butnull =new JButton("?");
butnull.setForeground(red);
butnull.addActionListener(this);
butBackspace =new JButton("Backspace");
butBackspace.setForeground(green);
butCE =new JButton("CE");
butCE.setForeground(green);
butC =new JButton("C");
butC.addActionListener(this);
butC.setForeground(red);
butMC =new JButton("MC");
butMC.setForeground(green);
butMR =new JButton("MR");
butMR.setForeground(green);
butMS =new JButton("MS");
butMS.setForeground(green);
butM =new JButton("M+");
butM.setForeground(green);
butChu =new JButton("/");
butChu.addActionListener(this);
butChu.setForeground(red);
butCheng =new JButton("*");
butCheng.addActionListener(this);
butCheng.setForeground(red);
butJian =new JButton("-");
butJian.addActionListener(this);
butJian.setForeground(red);
butJia =new JButton("+");
butJia.addActionListener(this);
butJia.setForeground(red);
butEqual =new JButton("=");
butEqual.addActionListener(this);
butEqual.setForeground(red);
Color blue =new Color(0,0,250);
butSqrt =new JButton("sqrt");
butSqrt.setForeground(green);
butYu =new JButton("%");
butYu.setForeground(green);
but1x =new JButton("1/x");
but1x.setForeground(green);
butDot =new JButton(".");
butDot.setForeground(blue);
butDot.addActionListener(this);
butJJ =new JButton("+/-");
butJJ.setForeground(blue);
butJJ.addActionListener(this);
but1 =new JButton("1");
but1.addActionListener(this);
but1.setForeground(blue);
but2 =new JButton("2");
but2.addActionListener(this);
but2.setForeground(blue);
but3 =new JButton("3");
but3.addActionListener(this);
but3.setForeground(blue);
but4 =new JButton("4");
but4.addActionListener(this);
but4.setForeground(blue);
but5 =new JButton("5");
but5.addActionListener(this);
but5.setForeground(blue);
but6 =new JButton("6");
but6.addActionListener(this);
but6.setForeground(blue);
but7 =new JButton("7");
but7.addActionListener(this);
but7.setForeground(blue);
but8 =new JButton("8");
but8.addActionListener(this);
but8.setForeground(blue);
but9 =new JButton("9");
but9.addActionListener(this);
but9.setForeground(blue);
but0 =new JButton("0");
but0.addActionListener(this);
but0.setForeground(blue);///////////////////////////////ending
pan =new JPanel(null);//创建最大的panel
tf =new JTextField("0");//输入显示框
tf.setBounds(5,0,337,25);//显示框设置大小位置
tf.setBackground(Color.white);
tf.setHorizontalAlignment(JTextField.RIGHT);//输入框设置位子居右显示
tf.setEditable(false);
JPanel pa=new JPanel(new BorderLayout());//所有按钮的panel
pa.setBounds(5,35,340,240);
JPanel pp=new JPanel(new BorderLayout());//null,MC,MR,MS,M+的panel;pa的西
JPanel aa=new JPanel(new BorderLayout());//其余所有Button的panel;pa的中
JPanel p =new JPanel(new GridLayout(4,1));//MC,MR,MS,M+的Panel,pp的中 北是一个butnull
JPanel a =new JPanel(new GridLayout(1,3));//backspace,ce,c的panel,aa的北
JPanel a2=new JPanel(new GridLayout(4,5));//其余的button的panel,aa的中
pa.add(pp,BorderLayout.WEST);
pa.add(aa,BorderLayout.CENTER);
pp.add(butnull,BorderLayout.NORTH);
pp.add(p,BorderLayout.CENTER);
p.add(butMC);
p.add(butMR);
p.add(butMS);
p.add(butM);
aa.add(a,BorderLayout.NORTH);
aa.add(a2,BorderLayout.CENTER);
a.add(butBackspace);
a.add(butCE);
a.add(butC);
a2.add(but7);
a2.add(but8);
a2.add(but9);
a2.add(butChu);
a2.add(butSqrt);
a2.add(but4);
a2.add(but5);
a2.add(but6);
a2.add(butCheng);
a2.add(butYu);
a2.add(but1);
a2.add(but2);
a2.add(but3);
a2.add(butJian);
a2.add(but1x);
a2.add(but0);
a2.add(butJJ);
a2.add(butDot);
a2.add(butJia);
a2.add(butEqual);
this.add(pan);
pan.add(tf);
pan.add(pa);
this.setTitle("我的计算器");
this.setJMenuBar(mb);
this.setBounds(200,300,353,330);//设置窗口大小
this.setResizable(false);
this.setVisible(true);
public static void main(String args[])
try
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
catch (Exception ex)
ex.printStackTrace();
new Calculator();
////////////////////********以下是监听设置和运算*******///////////////////
boolean b=true;//初始值设置为真未点运算符号
String fuhao="";//保存最后一次运算符号,用于进行上次结果的运算
String lastCommand="";//保存按等号前的最后的运算符号
double lastValue=0.0;//保存按等号钱的最后的输入框里的值
double value=0.0;//原值,当你点完运算符的时候计算之前的值
double fuzhi=0.0;
public void actionPerformed(ActionEvent click)/////////////////////行为监听设置
if(click.getSource()==butJJ)
fuzhi=Double.parseDouble(tf.getText());
if(tf.getText().equals("0")||tf.getText().equals("0."))
boolean b=true;
String fuhao="";
double value=0.0;
tf.setText("0");
else if(fuzhi<0)
tf.setText((-fuzhi)+"");
else
tf.setText("-"+tf.getText());
if(click.getSource()==butnull)
tf.setText("本计算器版权所有归青软培训--胡迅--2008年10月18日著 ");
if(click.getSource()==butC)
tf.setText("0");
lastCommand="";
lastValue=0.0;
b=true;
fuhao="";
value=0.0;
if(click.getSource()==but1)
if(b==false)
tf.setText("1");
b=true;
else if(tf.getText().equals("0"))
tf.setText("1");
else
tf.setText(tf.getText()+1);
if(click.getSource()==but2)
if(b==false)
tf.setText("2");
b=true;
else if(tf.getText().equals("0"))
tf.setText("2");
else
tf.setText(tf.getText()+2);
if(click.getSource()==but3)
if(b==false)
tf.setText("3");
b=true;
else if(tf.getText().equals("0"))
tf.setText("3");
else
tf.setText(tf.getText()+3);
if(click.getSource()==but4)
if(b==false)
tf.setText("4");
b=true;
else if(tf.getText().equals("0"))
tf.setText("4");
else
tf.setText(tf.getText()+4);
if(click.getSource()==but5)
if(b==false)
tf.setText("5");
b=true;
else if(tf.getText().equals("0"))
tf.setText("5");
else
tf.setText(tf.getText()+5);
if(click.getSource()==but6)
if(b==false)
tf.setText("6");
b=true;
else if(tf.getText().equals("0"))
tf.setText("6");
else
tf.setText(tf.getText()+6);
if(click.getSource()==but7)
if(b==false)
tf.setText("7");
b=true;
else if(tf.getText().equals("0"))
tf.setText("7");
else
tf.setText(tf.getText()+7);
if(click.getSource()==but8)
if(b==false)
tf.setText("8");
b=true;
else if(tf.getText().equals("0"))
tf.setText("8");
else
tf.setText(tf.getText()+8);
if(click.getSource()==but9)
if(b==false)
tf.setText("9");
b=true;
else if(tf.getText().equals("0"))
tf.setText("9");
else
tf.setText(tf.getText()+9);
if(click.getSource()==but0)
if(b==false)
tf.setText("0");
b=true;
else if(tf.getText().equals("0"))
tf.setText("0");
else
tf.setText(tf.getText()+0);
if(click.getSource()==butDot)
/////////code/////////////////////////////////////////////////////////////////////////////////////////////////////////???
int izhi=Integer.parseInt(tf.getText());
double dzhi=Double.parseDouble(tf.getText());
if(tf.getText().equals("0"))
tf.setText("0.");
else
if(tf.getText().equals("0."))
tf.setText("0.");
else
if(dzhi>0)
izhi=izhi;
dzhi=dzhi;
if(dzhi==izhi)
tf.setText(tf.getText()+".");
else
izhi=-izhi;
dzhi=-dzhi;
if(dzhi==izhi)
tf.setText(tf.getText()+".");
if(click.getSource()==butJia)///////////////////////////////////////////////"+"
if(tf.getText().equals("0"))
else if(fuhao=="")
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=tfvaluejia;
tf.setText(""+value);
b=false;
fuhao="+";
else if(fuhao=="*")
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
else if(fuhao=="/")
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
else if(fuhao=="-")
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
else if(fuhao=="=")
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=tfvaluejia;
b=false;
else
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());//2
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
lastValue=Double.parseDouble(tf.getText());
fuhao="+";
lastCommand="+";
if(click.getSource()==butCheng)////////////////////////////////////////////////////////////"*"
if(tf.getText().equals("0"))
else if(fuhao=="")
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
tf.setText(""+value);
b=false;
fuhao="*";
else if(fuhao=="+")
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
else if(fuhao=="/")
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
else if(fuhao=="-")
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
else if(fuhao=="=")
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
b=false;
else
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
lastValue=Double.parseDouble(tf.getText());
fuhao="*";
lastCommand="*";
if(click.getSource()==butChu)//////////////////////////////////////////////"/"
if(tf.getText().equals("0"))
else if(fuhao=="")
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=tfvalueChu;
tf.setText(""+value);
b=false;
fuhao="/";
else if(fuhao=="+")
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
else if(fuhao=="*")
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
else if(fuhao=="-")
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
else if(fuhao=="=")
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
b=false;
else
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
lastValue=Double.parseDouble(tf.getText());
fuhao="/";
lastCommand="/";
if(click.getSource()==butJian)////////////////////////////////////////////////////////////////////////"-"
if(tf.getText().equals("0"))
else if(fuhao=="")
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=tfvaluejian;
tf.setText(""+value);
b=false;
fuhao="/";
else if(fuhao=="+")
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
else if(fuhao=="*")
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
else if(fuhao=="/")
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
else if(fuhao=="=")
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
b=false;
else
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
lastValue=Double.parseDouble(tf.getText());
fuhao="-";
lastCommand="-";
if(click.getSource()==butEqual)//////////////////////////////////////////"="
if(tf.getText().equals("0"))
if(fuhao=="+")
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
else if(fuhao=="*")
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
else if(fuhao=="/")
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
else if(fuhao=="-")
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
else if(fuhao=="=")
if(lastCommand=="+")
value = value +lastValue;
tf.setText(""+value);
b=false;
else if(lastCommand=="-")
value = value - lastValue;
tf.setText(""+value);
b=false;
if(lastCommand=="*")
value = value *lastValue;
tf.setText(""+value);
b=false;
if(lastCommand=="/")
value = value /lastValue;
tf.setText(""+value);
b=false;
fuhao="=";
参考技术B import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
class Calculator
private Frame frame;
private Panel panel[] = new Panel[6];
private Button botton[] = new Button[25];
private TextField textField;
private final String string[] = "1", "2", "3", "clear", "exit", "4", "5",
"6", "+", "-", "7", "8", "9", "*", "/", "0", ".", "sqr", "%",
"+/-", "sin", "cos", "tan", "PI", "=" ;
private String str1, str2, optype;
private final Label label = new Label(
"Contact me--QQ:120355282 , mail:fsolsh@gmail.com");
private final String STR1 = "Please enter your data!";
private final String STR2 = "Arithmetic data can't be '0'!";
private double result = 0;
private void go()
frame = new Frame("Welcome ! Author:Fsol-sh JSface");
frame.setResizable(false);
frame.setLayout(new GridLayout(7, 1, 5, 5));
for (int i = 0; i < botton.length; i++)
botton[i] = new Button(string[i]);
botton[i].setForeground(Color.BLUE);
botton[i].setFont(new Font("", Font.BOLD, 12));
textField = new TextField(40);
textField.setFont(new Font("", Font.BOLD, 12));
textField.setEditable(false);
label.setAlignment(1);
label.setFont(new Font("", Font.BOLD, 12));
label.setForeground(Color.BLUE);
for (int i = 0; i < panel.length; i++)
panel[i] = new Panel();
frame.add(panel[i]);
frame.add(label);
panel[0].add(textField);
for (int i = 1; i < 6; i++)
panel[i].setLayout(new GridLayout(1, 5, 5, 5));
for (int j = (i - 1) * 5; j < i * 5; j++)
panel[i].add(botton[j]);
for (int i = 0; i < 3; i++)
for (int j = (i) * 5; j < i * 5 + 3; j++)
buttonSet(botton[j]);
botton[3].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
textField.setText("");
);
botton[4].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
System.exit(0);
);
operateTypeSet(botton[8]);
operateTypeSet(botton[9]);
operateTypeSet(botton[13]);
operateTypeSet(botton[14]);
operateTypeSet(botton[18]);
botton[15].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (check(textField.getText())
|| textField.getText().equals("0"))
textField.setText("0");
else
textField.setText(textField.getText() + "0");
);
botton[16].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (check(textField.getText()))
textField.setText(STR1);
else if (textField.getText().indexOf(".") == -1)
textField.setText(textField.getText() + ".");
else
textField.setText(textField.getText());
);
botton[17].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (check(textField.getText()))
textField.setText(STR1);
else
textField.setText(""
+ Math
.sqrt(Double.parseDouble(textField
.getText())));
);
botton[19].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
String str = textField.getText();
if (check(str))
textField.setText(STR1);
else if (isZero(str))
textField.setText(str);
else if (str.indexOf("-") != -1)
StringBuffer sb = new StringBuffer(str);
sb = sb.deleteCharAt(sb.indexOf("-"));
str = new String(sb);
textField.setText(str);
else
textField.setText("-" + str);
);
botton[20].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (check(textField.getText()))
textField.setText(STR1);
else
textField
.setText(""
+ Math.sin(Double.parseDouble(textField
.getText())));
);
botton[21].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (check(textField.getText()))
textField.setText(STR1);
else
textField
.setText(""
+ Math.cos(Double.parseDouble(textField
.getText())));
);
botton[22].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (check(textField.getText()))
textField.setText(STR1);
else
textField
.setText(""
+ Math.tan(Double.parseDouble(textField
.getText())));
);
botton[23].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
textField.setText("" + Math.PI);
);
botton[24].addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
str2 = textField.getText();
if (check(str2))
textField.setText(STR1);
else if (isZero(str2) && "/".equals(optype))
textField.setText(STR2);
else
operate(str1, str2);
textField.setText("" + result);
str1 = "";
str2 = "";
optype = "=";
);
frame.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);
);
frame.pack();
frame.setVisible(true);
private void operateTypeSet(final Button opbutton)
opbutton.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
str1 = textField.getText();
if (check(str1))
str1 = "";
optype = null;
textField.setText(STR1);
else
textField.setText("");
optype = opbutton.getLabel();
);
private void buttonSet(final Button b)
b.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (check(textField.getText()))
textField.setText(b.getLabel());
else
textField.setText(textField.getText() + b.getLabel());
);
private boolean check(String param)
return isNullOrEmpty(param) || STR1.equals(param) || STR2.equals(param);
private boolean isZero(String param)
if (!isNullOrEmpty(param))
BigDecimal bigDecimal = new BigDecimal(param);
return bigDecimal.compareTo(new BigDecimal(0)) == 0;
else
return false;
private boolean isNullOrEmpty(String param)
return param == null || param.trim().length() == 0;
private double operate(String param1, String param2)
if (optype == "+")
result = Double.parseDouble(param1) + Double.parseDouble(param2);
else if (optype == "-")
result = Double.parseDouble(param1) - Double.parseDouble(param2);
else if (optype == "*")
result = Double.parseDouble(param1) * Double.parseDouble(param2);
else if (optype == "/")
result = Double.parseDouble(param1) / Double.parseDouble(param2);
else if (optype == "%")
result = Double.parseDouble(param1) % Double.parseDouble(param2);
else
result = Double.parseDouble(param2);
return this.result;
public static void main(String[] args)
Calculator calculator = new Calculator();
calculator.go();
参考技术C 前段时间自己在做,这是我放在自己百度空间里的,代码很粗糙,现在GUI部分问题不大,已有轮廓,具体功能还没实现……
与标准程序相比还差一些,我是放在一个循环中来添加按钮的,你可以稍微修改一下,但我想基本能满足你的要求……
代码片如下:
import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Gui extends Applet implements ActionListener
JTextField inTxt;
//JScrollPane sp;
JPanel pan;
public void init()
inTxt = new JTextField("");
inTxt.setEditable(false);
//sp=new JScrollPane(inTxt); //此处处理的不好,BUG here!!
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gCon = new GridBagConstraints();
pan = new JPanel();
pan.setFont(new Font("SansSerif", Font.PLAIN, 14));
pan.setLayout(gb);
gCon.fill = GridBagConstraints.BOTH;
gCon.weightx=1.0;
addElements(gb,gCon,"Ext");
addElements(gb,gCon,"");
addElements(gb,gCon,"");
gCon.gridwidth=GridBagConstraints.REMAINDER;
addElements(gb,gCon,"Cl");
gCon.gridwidth=1; //To Kill the Fun //GridBagConstraints.REMAINDER,MY-God!!!The //very HEAVY step!!ALL copy right reserved by wanpg
gCon.weightx=1.0;
addElements(gb,gCon,"1");
addElements(gb,gCon,"2");
addElements(gb,gCon,"3");
gCon.gridwidth=GridBagConstraints.REMAINDER;
addElements(gb,gCon,"+");
gCon.gridwidth=1;
gCon.weightx=1.0;
addElements(gb,gCon,"4");
addElements(gb,gCon,"5");
addElements(gb,gCon,"6");
gCon.gridwidth=GridBagConstraints.REMAINDER;
addElements(gb,gCon,"-");
gCon.gridwidth=1;
gCon.weightx=1.0;
addElements(gb,gCon,"7");
addElements(gb,gCon,"8");
addElements(gb,gCon,"9");
gCon.gridwidth=GridBagConstraints.REMAINDER;
addElements(gb,gCon,"x");
gCon.weightx=1.0;
gCon.gridwidth=1;
addElements(gb,gCon,"+/-");
addElements(gb,gCon,"0");
addElements(gb,gCon,".");
gCon.gridwidth=GridBagConstraints.REMAINDER;
addElements(gb,gCon,"/");
setLayout(new BorderLayout());
add(pan,BorderLayout.CENTER);
add(inTxt,BorderLayout.NORTH);
setSize(400,180);
void addElements(GridBagLayout gb,GridBagConstraints gCon,String name)
JButton btn = new JButton(name);
btn.addActionListener(this);
gb.setConstraints(btn,gCon);
pan.add(btn);
public void actionPerformed(ActionEvent ae)
String lbl=((JButton)ae.getSource()).getText();
inTxt.setText(inTxt.getText()+lbl);
public static void main(String[] args)
Gui main = new Gui();
final JFrame f = new JFrame("ComP");
f.addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent we)
f.setVisible(false);
f.dispose();
);
main.init();
f.add(main,BorderLayout.CENTER);
f.pack();
f.setResizable(false);
f.setSize(f.getPreferredSize());
f.setVisible(true);
参考技术D 那写在html 里的要不?我正好有个js的代码嘿嘿
<html>
<head>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" width="400">
<tr>
<td width="100%" valign="top"><table border="2" width="200" cellspacing="0"
cellpadding="0" bgcolor="#000000" style="border-color:yello"
onClick="previouskey=event.srcElement.innerText">
<tr>
<td width="100%" bgcolor="#FFFFFF" id="result"
style="font:bold 20px Verdana;color:black;text-align='right'">0</td>
</tr>
<tr>
<td width="100%" valign="middle" align="center"><table border="0" width="100%"
cellspacing="0" cellpadding="0" style="font:bold 20px Verdana;color:white">
<tr>
<td width="80%" align="center"><table border="1" width="100%" cellspacing="0"
cellpadding="0" style="cursor:hand;font:bold 20px Verdana;color:white"
onMouseover="if (event.srcElement.tagName=='TD')event.srcElement.style.color='yellow'"
onMouseout="event.srcElement.style.color='white'" onselectStart="return false"
onClick="calculate()" height="82">
<tr>
<td width="25%" align="center" height="17">7</td>
<td width="25%" align="center" height="17">8</td>
<td width="25%" align="center" height="17">9</td>
<td width="25%" align="center" height="17">/</td>
</tr>
<tr>
<td width="25%" align="center" height="19">4</td>
<td width="25%" align="center" height="19">5</td>
<td width="25%" align="center" height="19">6</td>
<td width="25%" align="center" height="19">*</td>
</tr>
<tr>
<td width="25%" align="center" height="19">1</td>
<td width="25%" align="center" height="19">2</td>
<td width="25%" align="center" height="19">3</td>
<td width="25%" align="center" height="19">-</td>
</tr>
<tr>
<td width="25%" align="center" height="19">0</td>
<td width="25%" align="center" height="19"
onClick="pn();previouskey=1;event.cancelBubble=true">+/-</td>
<td width="25%" align="center" height="19">.</td>
<td width="25%" align="center" height="19">+</td>
</tr>
</table>
</td>
<td width="20%"><div align="left"><table border="1" width="100%" cellspacing="0"
cellpadding="0">
<tr>
<td width="100%" style="cursor:hand;font:bold 20px Verdana;color:white;text-align:center"
onClick="result.innerText=0;results=''">C</td>
</tr>
</table>
</div><div align="left"><table border="1" width="100%" cellspacing="0" cellpadding="0"
height="81">
<tr>
<td width="100%" style="cursor:hand;font:bold 32px Verdana;color:white;text-align:center"
onMouseover="event.srcElement.style.color='yellow'"
onMouseout="event.srcElement.style.color='white'" onClick="calculateresult()">=<script
language="javascript1.2">
var results=''
var previouskey=''
var re=/(\/|\*|\+|-)/
var re2=/(\/|\*|\+|-)2$/
var re3=/.+(\/|\*|\+|-).+/
var re4=/\d|\./
var re5=/^[^\/\*\+].+\d$/
var re6=/\./
function calculate()
if (event.srcElement.tagName=="TD")
if (event.srcElement.innerText.match(re4)&&previouskey=="=")
results=''
if (result.innerText.match(re3)&&event.srcElement.innerText.match(re))
if (!results.match(re5))
result.innerText="Error!"
return
results=eval(results)
if (results.toString().length>=12&&results.toString().match(re6))
results=results.toString().substring(0,12)
result.innerText=results
results+=event.srcElement.innerText
if (results.match(re2))
results=results.substring(0,results.length-2)+results.charAt(results.length-1)
result.innerText=results
function calculateresult()
if (!results.match(re5))
result.innerText="Error!"
return
results=eval(results)
if (results.toString().length>=12&&results.toString().match(re6))
results=results.toString().substring(0,12)
result.innerText=results
function pn()
if (result.innerText.charAt(0)!='-')
result.innerText=results='-'+result.innerText
else if (result.innerText.charAt(0)=='-')
result.innerText=results=result.innerText*(-1)
</script></td>
</tr>
</table>
</div></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
算法 A 求稳健平均值和稳健标准差
算法原理
算法 A 来自 GB/T 6379.5。应用此算法计算得到数据平均值和标准差的稳健值。稳健性是估计算法的特点,而不是其产生的估计值的特点,因此严格来说,称由此算法计算的平均值和标准差是稳健的是不确切的。然而,为避免使用繁琐的术语,“稳健均值”和“稳健标准差”应理解为利用稳健算法计算的总体均值和总体标准差的均值估计。
设数据为
x
i
,
i
∈
(
1
,
2
,
⋯
,
n
)
x_i, i\\in(1,2,\\cdots,n)
xi,i∈(1,2,⋯,n),记稳健平均值为
x
∗
,
s
∗
x^*, s^*
x∗,s∗,首先是计算初始值:
x
∗
=
med
x
i
s
∗
=
1.483
×
med
∣
x
i
−
x
∗
∣
\\begin{array}{c} x^{*}=\\operatorname{med} x_{i} \\\\ s^{*}=1.483 \\times \\operatorname{med}\\left|x_{i}-x^{*}\\right| \\end{array}
x∗=medxis∗=1.483×med∣xi−x∗∣
对每个
x
i
x_i
xi,有:
x
i
∗
=
{
x
∗
−
δ
,
若
x
i
<
x
∗
−
δ
x
∗
+
δ
,
若
x
i
>
x
∗
+
δ
x
i
,
其他
x_{i}^{*}=\\left\\{\\begin{array}{cc} x^{*}-\\delta, & \\text { 若 } x_{i}<x^{*}-\\delta \\\\ x^{*}+\\delta, & \\text { 若 } x_{i}>x^{*}+\\delta \\\\ x_{i}, & \\text { 其他 } \\end{array}\\right.
xi∗=⎩⎨⎧x∗−δ,x∗+δ,xi, 若 xi<x∗−δ 若 xi>x∗+δ 其他
其中:
δ
=
1.5
s
∗
\\delta=1.5 s^{*}
δ=1.5s∗,再次计算:
x
∗
=
∑
x
i
∗
/
p
s
∗
=
1.134
∑
(
x
i
∗
−
x
∗
)
2
/
(
p
−
1
)
\\begin{array}{c} x^{*}=\\sum x_{\\mathrm{i}}^{*} / p \\\\ s^{*}=1.134 \\sqrt{\\sum\\left(x_{\\mathrm{i}}^{*}-x^{*}\\right)^{2} /(p-1)} \\end{array}
x∗=∑xi∗/ps∗=1.134∑(xi∗−x∗)2/(p−1)
重复:
x
i
∗
=
{
x
∗
−
δ
,
若
x
i
<
x
∗
−
δ
x
∗
+
δ
,
若
x
i
>
x
∗
+
δ
x
i
,
其他
x_{i}^{*}=\\left\\{\\begin{array}{cc} x^{*}-\\delta, & \\text { 若 } x_{i}<x^{*}-\\delta \\\\ x^{*}+\\delta, & \\text { 若 } x_{i}>x^{*}+\\delta \\\\ x_{i}, & \\text { 其他 } \\end{array}\\right.
xi∗=⎩⎨⎧x∗−δ,x∗+δ,xi, 若 xi<x∗−δ 若 xi>x∗+δ 其他
直到 s ∗ s^* s∗ 的 第三位有效数字和 x ∗ x^* x∗ 的对应数字在连续两次迭代中不变。
代码
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
def location_corresponding(x_str, s_str):
'''
s 的第三位有效数字,以及对应 x 的相应的数字
'''
digit_location = s_str.find('.')
if digit_location >= 3:
# 如果小数点在字符串的第 4 位或大于第四位,也即数字至少在 100 以上
# 此时第三位有效数字,肯定在小数点前。
significant_num = s_str[2]
# 有效位数于小数点的相对位置
# 0 代表在小数点前, 3-1 是指从小数点前数起的位数
str_len = len(s_str[:digit_location])
sig_loc = (0, str_len-3-1)
elif s_str[0] == '0':
# 若整数部分是0,则有效数字的位置在小数点后
s_without_int = s_str[digit_location:]
while '0' in s_without_int:
s_without_int = s_without_int.replace('0', '')
digit_location += 1
significant_num = s_str[digit_location+3]
# 1 表示有效数字在小数点后
sig_loc = (1, 3)
elif digit_location == -1 and len(s_str) >= 3:
# 若只有整数,没有小数部分
str_len = len(s_str)
sig_loc = (0, str_len-3)
significant_num = s_str[2]
elif digit_location == -1 and len(s_str) < 3:
significant_num = '0'
else:
# 若整数部分小于 2 位,则
sig_loc = (1, 3-digit_location)
#
significant_num = s_str[sig_loc[1]+digit_location]
# x 对应的数字
x_digit_location = x_str.find('.')
if sig_loc[0] == 0:
if x_digit_location == -1 and len(x_str) >= 3:
x_significant_num = x_str[2]
elif x_digit_location == -1 and len(x_str) < 3:
x_significant_num = '0'
else:
x_significant_num = x_str[x_digit_location-sig_loc[1]]
print(sig_loc[1])
print('前')
else:
x_significant_num = x_str[x_digit_location+sig_loc[1]]
return x_significant_num, significant_num
def coverage_critiria(x_list, s_list):
'''
收敛准则
其中 s_list 是一个长度为 3 的 list, 包含当前迭代的 s* 和之前两个迭代的 s*
其中 x_list 也一样
难点在于:如何找出 s 的第三位有效数字,对应 x 的数位呢?
这里的解决办法是:找出 s 三位有效数字,在小数点的位置,从而应用于 x 中
'''
s_numbers = []
x_numbers = []
for i in range(3):
s = s_list[i]
# s 的字符串
s_str = str(s)
x = x_list[i]
# x 的字符串
x_str = str(x)
x_sig_num, s_sig_num = location_corresponding(x_str, s_str)
s_numbers.append(s_sig_num)
x_numbers.append(x_sig_num)
s_equal_flag = (s_numbers[0] == s_numbers[1]) and (s_numbers[0] == s_numbers[2])
x_equal_flag = (x_numbers[0] == x_numbers[1]) \\
and (x_numbers[0] == x_numbers[2])
if s_equal_flag and x_equal_flag:
return True
else:
return False
def perform_algorithm_A(x):
'''
x 是一个向量
'''
if type(x) is list:
x = np.array(x)
# 稳健平均和稳健标准差初始值
x_star = np.median(x)
x_diff = np.absolute(x-x_star)
s_star = 1.483*np.median(x_diff)
#
delta = 1.5*s_star
higher_bound = x_star + delta
lower_bound = x_star - delta
x_list = []
s_list = []
while True:
x[x>higher_以上是关于求Java计算器标准的布局代码的主要内容,如果未能解决你的问题,请参考以下文章