以前写过的,不知道代码还在不在
我不是很清楚啊
package jisuanqi;
import java.awt.*;
import java.awt.MenuBar;
import javax.swing.*;
import java.awt.event.*;
//主类
class jsq
extends Frame {
//声明
double op, pa1, pa2, pa3;
String op1, op2, ll, s;
boolean isResult = false;
//设置标题
Frame p=new Frame("计算器");
//创建按钮
Panel p0 = new Panel();
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();
Panel p5 = new Panel();
Panel p6 = new Panel();
Panel p7 = new Panel();
// 创建文本框
JTextField t = new JTextField("0");
TextArea te = new TextArea();
//初始化按钮
Button b1 = new Button("Backspace");
Button b2 = new Button("CE");
Button b3 = new Button("sin");
Button b4 = new Button("MC");
Button b5 = new Button("7");
Button b6 = new Button("8");
Button b7 = new Button("9");
Button b8 = new Button("/");
Button b9 = new Button("sqrt");
Button b10 = new Button("MR");
Button b11 = new Button("4");
Button b12 = new Button("5");
Button b13 = new Button("6");
Button b14 = new Button("*");
Button b15 = new Button("%");
Button b16 = new Button("MS");
Button b17 = new Button("1");
Button b18 = new Button("2");
Button b19 = new Button("3");
Button b20 = new Button("-");
Button b21 = new Button("1/x");
Button b22 = new Button("M+");
Button b23 = new Button("0");
Button b24 = new Button("+/-");
Button b25 = new Button(".");
Button b26 = new Button("+");
Button b27 = new Button("=");
Button ba = new Button("exit");
Button a1 = new Button("n!");
Button a2 = new Button("cos");
Button a3 = new Button("tan");
Button a4 = new Button("cot");
//创建JMenuBar
MenuBar mbar = new MenuBar();
//创建Menu
Menu menu1 = new Menu("File");
Menu menu2 = new Menu("help");
Menu menu3 = new Menu("Edit");
//创建MenuItem
MenuItem me1 = new MenuItem("copy");
MenuItem me2 = new MenuItem("paste");
MenuItem me3 = new MenuItem("about");
MenuItem me4 = new MenuItem("open");
MenuItem me5 = new MenuItem("source");
public void jbInit() {
t.setHorizontalAlignment(JTextField.RIGHT);
//添加菜单项
this.setMenuBar(mbar);
mbar.add(menu1);
mbar.add(menu2);
mbar.add(menu3);
menu1.add(me1);
menu1.addSeparator();
menu1.add(me2);
menu1.addSeparator();
menu1.add(me4);
menu2.add(me3);
menu3.add(me5);
//为me1创建快捷键
me1.setShortcut(new MenuShortcut(67));
me1.setActionCommand("copy");
me2.setShortcut(new MenuShortcut(86));
me2.setActionCommand("paste");
//添加画板"
this.add(p0);
//为画板p0设置 布局
p0.setLayout(new GridLayout(9, 6, 10, 5));
p0.setBackground(Color.lightGray);
p0.add(p1);
//为画板p1设置 布局
p1.setLayout(new BorderLayout());
p1.setBackground(Color.lightGray);
p1.add(t);
//为画板p7设置 布局
p7.setLayout(new GridLayout(1, 4, 5, 5));
p7.setBackground(Color.lightGray);
//向画板P7中添加按钮
p0.add(p7);
p7.add(a1);
p7.add(a2);
p7.add(a3);
p7.add(a4);
p0.add(p2);
//为画板p0设置 布局
p2.setLayout(new GridLayout(1, 4, 5, 5));
p2.setBackground(Color.lightGray);
//向画板P2中添加按钮
p2.add(ba);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p0.add(p3);
//为画板p0设置 布局
p3.setLayout(new GridLayout(1, 6, 5, 5));
p3.setBackground(Color.lightGray);
//向画板P3中添加按钮
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
p3.add(b9);
p0.add(p4);
//为画板p0设置 布局
p4.setLayout(new GridLayout(1, 6, 5, 5));
p4.setBackground(Color.lightGray);
//向画板P4中添加按钮
p4.add(b10);
p4.add(b11);
p4.add(b12);
p4.add(b13);
p4.add(b14);
p4.add(b15);
p0.add(p5);
//为画板p0设置 布局
p5.setLayout(new GridLayout(1, 6, 5, 5));
p5.setBackground(Color.lightGray);
//向画板P5中添加按钮
p5.add(b16);
p5.add(b17);
p5.add(b18);
p5.add(b19);
p5.add(b20);
p5.add(b21);
p0.add(p6);
//为画板p0设置 布局
p6.setLayout(new GridLayout(1, 6, 5, 5));
p6.setBackground(Color.lightGray);
//向画板P6中添加按钮
p6.add(b22);
p6.add(b23);
p6.add(b24);
p6.add(b25);
p6.add(b26);
p6.add(b27);
//f.add(pp);
// f.add(te);
//调整大小并显示
this.pack();
this.setVisible(true);
t.setEditable(false);
t.setBackground(Color.white);
// 向按钮中增加监听器
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String m = t.getText();
t.setText("0");
}
});
b23.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
}
else {
t.setText(t.getText() + 0);
}
}
});
b17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("1");
}
else {
t.setText(t.getText() + 1);
}
}
});
b18.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("2");
}
else {
t.setText(t.getText() + 2);
}
}
});
b19.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("3");
}
else {
t.setText(t.getText() + 3);
}
}
});
b11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("4");
}
else {
t.setText(t.getText() + 4);
}
}
});
b12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("5");
}
else {
t.setText(t.getText() + 5);
}
}
});
b13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("6");
}
else {
t.setText(t.getText() + 6);
}
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("7");
}
else {
t.setText(t.getText() + 7);
}
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("8");
}
else {
t.setText(t.getText() + 8);
}
}
});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("9");
}
else {
t.setText(t.getText() + 9);
}
}
});
ba.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0); //设置退出按钮
}
});
b25.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s11 = t.getText();
if (s11.indexOf(".") < 0) {
s11 = s11 + ".";
t.setText(s11);
}
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String string=e.getActionCommand() ;
String mm = t.getText();
if(e.equals("弧度") ){
double nn = Double.parseDouble(mm);
nn *= Math.PI / 180;
nn = Math.sin(nn);
nn = nn * 10000;
nn = Math.rint(nn);
nn = nn / 10000;
t.setText(" " + nn);
}
else{
double num=Double.parseDouble(mm) ;
t.setText(String.valueOf(Math.sin(num) ) ) ;
}
}
});
//为sin添加监听器
a2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String mm = t.getText();
double nn = Double.parseDouble(mm); //小数化整
nn *= Math.PI / 180; //角度变为弧度
try {
nn = Math.cos(nn);
}
catch ( NumberFormatException ex ) {
}
nn = nn * 10000;
nn = Math.rint(nn);
nn = nn / 10000;
t.setText(" " + nn);
}
});
a3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String mm = t.getText();
double nn = Double.parseDouble(mm); //小数化整
nn *= Math.PI / 180; //角度变为弧度
nn = Math.tan(nn);
try {
nn = nn * 10000;
}
catch ( NumberFormatException ex) {
}
nn = Math.rint(nn);
nn = nn / 10000;
t.setText(" " + nn);
}
});
b26.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
op = '+'; //设置加号
}
});
b20.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
op = '-'; //设置减号
}
});
b14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
op = '*'; //设置乘号
}
});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
op = '/'; //设置除号
}
});
//设置清除按钮
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
// op='sin';
}
});
//设置等号按钮
b27.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op2 = t.getText();
pa1 = Double.parseDouble(op1); //字弧串变为双精度数
pa2 = Double.parseDouble(op2); //字弧串变为双精度数
//double pa3 =0;
if (op == '+')
pa3 = pa1 + pa2;
if (op == '-')
pa3 = pa1 - pa2;
if (op == '*')
pa3 = pa1 * pa2;
if (op == '/')
pa3 = pa1 / pa2;
t.setText(" " + pa3);
}
});
//增加菜单监听器
me1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = t.getText();
}
});
me2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.setText(str);
}
});
me3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog u=new dialog();
u.setSize(300,200) ;
u.show() ;
//t.setText(" ");
//t.setText(t.getText() + "这是我的计算器,我爱JAVA .");
}
});
me4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*www u=new www();
u.pack() ;
u.show() ;*/
}
}) ;
b21.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
double len =Double.parseDouble(t.getText());
len = (1 / len);
if (t.getText() != null) {
t.setText("" + len);
}
}
catch (NumberFormatException ex) {
}
}
}) ;
b24.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int len = Integer.parseInt(t.getText());
len = ~len + 1;
if (t.getText() != null) {
t.setText("" + len);
}
}
catch (NumberFormatException ex) {
System.out .print("error") ;
}
}
}) ;
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ss=t.getText() ;
int len=ss.length();
if(ss!=null){
t.setText(ss.substring(0,len-1)) ;
if(len==1){
t.setText("0");
}
}
b1_actionPerformed(e);
}
}) ;
me5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog1 m=new dialog1();
m.setSize(300,300) ;
m.show() ;
}
}) ;
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double oo=Double.parseDouble(t.getText() ) ;
t.setText(" ") ;
t.setText(String.valueOf(-Math.sqrt(oo) ) ) ;
t.setText(String.valueOf( Math.sqrt(oo) ) ) ;
}
});
t.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() ==2){
System.exit(0) ;
}
}
});
//增加窗口监听器
this.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//添加阶乘监听器
a1.addActionListener(new ActionListener() {
// try {
public void actionPerformed(ActionEvent e) {
int mull = 1;
int x = Integer.parseInt(t.getText()); //小数化整
t.setText(" ");
for (int i = 1; i <= x; i++)
mull = mull * i;
t.setText(String.valueOf(mull));
}
});
}
String str;
public jsq() {
jbInit();
}
public static void main(String[] args) {
jsq u = new jsq(); //实例化a
}
void b1_actionPerformed(ActionEvent e) {
}
}
class dialog extends JFrame{
JLabel jlabel=new JLabel();
JPanel jpanel=new JPanel();
dialog(){
this.getContentPane().add(jpanel) ;
jpanel.setLayout(new BorderLayout( )) ;
jpanel.add(jlabel) ;
jlabel.setText("本计算机的版权归个人所有!!" +
" 任何公司和个人不得侵权!!!!!") ;
}
}
class dialog1 extends Frame{
ScrollPane scrollpane=new ScrollPane();
TextArea textarea=new TextArea ();
dialog1(){
this.add(scrollpane) ;
scrollpane.add(textarea) ;
textarea.setText(" ") ;
}
}