/*
* 创建日期 2006-1-24
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JSQ extends JFrame implements ActionListener {
private JButton myJButton1,myJButton2,myJButton3,myJButton4,myJButton5,myJButton6,myJButton7,myJButton8,myJButton9 ;
private JButton myJButton10,myJButton11,myJButton12,myJButton13,myJButton14,myJButton15,myJButton0,myJButton16,myJButton17;
private JTextField mytext = new JTextField("0");
//mytext.setHorizontalAlignment(SwingConstants.RIGHT);
//mytext.setEnabled(false); //文本框不能编辑
public JSQ(){
createComponents();
layoutComponents();
setTitle("计数器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public void createComponents(){
myJButton0 = new JButton("0");
myJButton1 = new JButton("1");
myJButton2 = new JButton("2");
myJButton3 = new JButton("3");
myJButton4 = new JButton("4");
myJButton5 = new JButton("5");
myJButton6 = new JButton("6");
myJButton7 = new JButton("7");
myJButton8 = new JButton("8");
myJButton9 = new JButton("9");
myJButton10 = new JButton(".");
myJButton11 = new JButton("+");
myJButton12 = new JButton("-");
myJButton13 = new JButton("*");
myJButton14 = new JButton("/");
myJButton15 = new JButton("=");
myJButton16 = new JButton("cel");
myJButton17 = new JButton(".");
mytext = new JTextField(40);
mytext.setEnabled(false); //文本框不能编辑
mytext.setHorizontalAlignment(SwingConstants.RIGHT);//将输入的数字或得到的结果在text的右边显示
}
public void layoutComponents(){
Container m = this.getContentPane();
m.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(mytext);
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(6,3,4,4));//设置布局,添加组件
panel2.add(myJButton0);
panel2.add(myJButton1);
panel2.add(myJButton2);
panel2.add(myJButton3);
panel2.add(myJButton4);
panel2.add(myJButton5);
panel2.add(myJButton6);
panel2.add(myJButton7);
panel2.add(myJButton8);
panel2.add(myJButton10);
panel2.add(myJButton16);
panel2.add(myJButton11);
panel2.add(myJButton12);
panel2.add(myJButton9);
panel2.add(myJButton13);
panel2.add(myJButton14);
panel2.add(myJButton15);
panel2.add(myJButton17);
m.add(panel1,BorderLayout.NORTH);
m.add(panel2,BorderLayout.CENTER); //注册事件监听器
myJButton1.addActionListener(this);
myJButton2.addActionListener(this);
myJButton3.addActionListener(this);
myJButton4.addActionListener(this);
myJButton5.addActionListener(this);
myJButton6.addActionListener(this);
myJButton7.addActionListener(this);
myJButton8.addActionListener(this);
myJButton9.addActionListener(this);
myJButton10.addActionListener(this);
myJButton11.addActionListener(this);
myJButton12.addActionListener(this);
myJButton13.addActionListener(this);
myJButton14.addActionListener(this);
myJButton17.addActionListener(this);
myJButton16.addActionListener(this);
myJButton15.addActionListener(this);
}
//当点击按钮+、-、*、/时,max = true
boolean max = false;
//当i=0时说明是我们第一次输入,字符串result不会累加
int i = 0;
//存放text的内容
String result = "";
//存放点击按钮+、-、*、/之前的数值
double t = 0;
//+、-、*、/的代号分别为1,2,3,4
int s = 0;
public void actionPerformed(ActionEvent e) {
/*如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么max=true
* 如果没有max= false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;
**/
if (e.getSource() ==myJButton1)
{
if (max || i == 0)
{
mytext.setText("1");
max = false;
i = 1;
}
else
{
result = mytext.getText();
mytext.setText(result + "1");
}
}
else if (e.getSource() == myJButton2)
{
if (max || i == 0)
{
mytext.setText("2");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "2");
}
}
else if (e.getSource() == myJButton3)
{
if (max || i == 0)
{
mytext.setText("3");
max = false;
i = 1;
}
else
{
result = mytext.getText();
mytext.setText(result + "3");
}
}
else if (e.getSource() == myJButton4)
{
if (max || i == 0)
{
mytext.setText("4");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "4");
}
}
else if (e.getSource() == myJButton5)
{
if (max || i == 0)
{
mytext.setText("5");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "5");
}
}
else if (e.getSource() == myJButton6)
{
if (max || i == 0)
{
mytext.setText("6");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result+ "6");
}
}
else if (e.getSource() == myJButton7)
{
if (max || i == 0)
{
mytext.setText("7");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "7");
}
}
else if (e.getSource() == myJButton8)
{
if (max || i == 0)
{
mytext.setText("8");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "8");
} }
else if (e.getSource() == myJButton9)
{ if (max || i == 0)
{
mytext.setText("9");
max = false;
i = 1;
}
else
{
result=mytext.getText();
mytext.setText(result + "9");
} } /* 添加0按钮
*/
else if (e.getSource() == myJButton0)
{
if (max || i == 0)
{
mytext.setText("0");
max = false;
i = 1;
}
else
{
result=mytext.getText();
if (Float.parseFloat(result) > 0 || Float.parseFloat(result) < 0)
{
mytext.setText(result+ "0");
}
else
{
if (result.trim().indexOf(".") == -1)
{
mytext.setText(result);
}
else
{
mytext.setText(result+ "0");
}
}
}
}
else if (e.getSource() == myJButton16)
{
mytext.setText("0");
i = 0;
max = true;
}
/*本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加,
* 不存在就添加.
*/
else if (e.getSource() == myJButton17)
{
if (max || i == 0)
{
mytext.setText("0.");
max = false;
i = 1;
}
else
{
result=mytext.getText();
if (result.trim().indexOf(".") == -1)
{
mytext.setText(result + ".");
}
else
{
mytext.setText(result);
}
}
}
//获得点击+之前的数值
else if (e.getSource() == myJButton11)
{
max = true;
i = 0;
t = Double.parseDouble(mytext.getText());
s= 1; }//获得点击-之前的数值
else if (e.getSource() == myJButton10)
{
max = true;
i = 0;
t = Double.parseDouble(mytext.getText());
s = 2;
}//获得点击*之前的数值
else if (e.getSource() == myJButton13)
{
max = true;
i = 0;
t = Double.parseDouble(mytext.getText());
System.out.println(t);
s = 3; }//获得点击/之前的数值
else if (e.getSource() == myJButton14)
{
max= true;
i = 0;
t = Double.parseDouble(mytext.getText());
s = 4; }
else if (e.getSource() == myJButton15)
{
switch (s)
{
case 1 ://计算加法
{
double ad =
t + Double.parseDouble(mytext.getText());
mytext.setText(ad + "");
i = 0;
result= "";
break;
}
case 2 ://计算减法
{
double ad = t- Double.parseDouble(mytext.getText()); mytext.setText(String.valueOf(ad));
i = 0;
result = "";
break;
}
case 3 ://计算乘法
{
double ad =
t * Double.parseDouble(mytext.getText());
mytext.setText(ad + "");
i = 0;
result = "";
break;
}
case 4 ://计算除法
{
double ad =
t/ Double.parseDouble(mytext.getText());
mytext.setText(ad + "");
i = 0;
result= "";
break;
}
}
System.out.println(max);
}
}
public static void main(String[] args){ JFrame.setDefaultLookAndFeelDecorated(true);
JSQ frame= new JSQ();
frame.setVisible(true);
}
}
* 创建日期 2006-1-24
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JSQ extends JFrame implements ActionListener {
private JButton myJButton1,myJButton2,myJButton3,myJButton4,myJButton5,myJButton6,myJButton7,myJButton8,myJButton9 ;
private JButton myJButton10,myJButton11,myJButton12,myJButton13,myJButton14,myJButton15,myJButton0,myJButton16,myJButton17;
private JTextField mytext = new JTextField("0");
//mytext.setHorizontalAlignment(SwingConstants.RIGHT);
//mytext.setEnabled(false); //文本框不能编辑
public JSQ(){
createComponents();
layoutComponents();
setTitle("计数器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public void createComponents(){
myJButton0 = new JButton("0");
myJButton1 = new JButton("1");
myJButton2 = new JButton("2");
myJButton3 = new JButton("3");
myJButton4 = new JButton("4");
myJButton5 = new JButton("5");
myJButton6 = new JButton("6");
myJButton7 = new JButton("7");
myJButton8 = new JButton("8");
myJButton9 = new JButton("9");
myJButton10 = new JButton(".");
myJButton11 = new JButton("+");
myJButton12 = new JButton("-");
myJButton13 = new JButton("*");
myJButton14 = new JButton("/");
myJButton15 = new JButton("=");
myJButton16 = new JButton("cel");
myJButton17 = new JButton(".");
mytext = new JTextField(40);
mytext.setEnabled(false); //文本框不能编辑
mytext.setHorizontalAlignment(SwingConstants.RIGHT);//将输入的数字或得到的结果在text的右边显示
}
public void layoutComponents(){
Container m = this.getContentPane();
m.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(mytext);
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(6,3,4,4));//设置布局,添加组件
panel2.add(myJButton0);
panel2.add(myJButton1);
panel2.add(myJButton2);
panel2.add(myJButton3);
panel2.add(myJButton4);
panel2.add(myJButton5);
panel2.add(myJButton6);
panel2.add(myJButton7);
panel2.add(myJButton8);
panel2.add(myJButton10);
panel2.add(myJButton16);
panel2.add(myJButton11);
panel2.add(myJButton12);
panel2.add(myJButton9);
panel2.add(myJButton13);
panel2.add(myJButton14);
panel2.add(myJButton15);
panel2.add(myJButton17);
m.add(panel1,BorderLayout.NORTH);
m.add(panel2,BorderLayout.CENTER); //注册事件监听器
myJButton1.addActionListener(this);
myJButton2.addActionListener(this);
myJButton3.addActionListener(this);
myJButton4.addActionListener(this);
myJButton5.addActionListener(this);
myJButton6.addActionListener(this);
myJButton7.addActionListener(this);
myJButton8.addActionListener(this);
myJButton9.addActionListener(this);
myJButton10.addActionListener(this);
myJButton11.addActionListener(this);
myJButton12.addActionListener(this);
myJButton13.addActionListener(this);
myJButton14.addActionListener(this);
myJButton17.addActionListener(this);
myJButton16.addActionListener(this);
myJButton15.addActionListener(this);
}
//当点击按钮+、-、*、/时,max = true
boolean max = false;
//当i=0时说明是我们第一次输入,字符串result不会累加
int i = 0;
//存放text的内容
String result = "";
//存放点击按钮+、-、*、/之前的数值
double t = 0;
//+、-、*、/的代号分别为1,2,3,4
int s = 0;
public void actionPerformed(ActionEvent e) {
/*如果是点击数字按钮那么先要判断是否在此之前点击了+、-、*、/、=,如果是那么max=true
* 如果没有max= false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;
**/
if (e.getSource() ==myJButton1)
{
if (max || i == 0)
{
mytext.setText("1");
max = false;
i = 1;
}
else
{
result = mytext.getText();
mytext.setText(result + "1");
}
}
else if (e.getSource() == myJButton2)
{
if (max || i == 0)
{
mytext.setText("2");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "2");
}
}
else if (e.getSource() == myJButton3)
{
if (max || i == 0)
{
mytext.setText("3");
max = false;
i = 1;
}
else
{
result = mytext.getText();
mytext.setText(result + "3");
}
}
else if (e.getSource() == myJButton4)
{
if (max || i == 0)
{
mytext.setText("4");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "4");
}
}
else if (e.getSource() == myJButton5)
{
if (max || i == 0)
{
mytext.setText("5");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "5");
}
}
else if (e.getSource() == myJButton6)
{
if (max || i == 0)
{
mytext.setText("6");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result+ "6");
}
}
else if (e.getSource() == myJButton7)
{
if (max || i == 0)
{
mytext.setText("7");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "7");
}
}
else if (e.getSource() == myJButton8)
{
if (max || i == 0)
{
mytext.setText("8");
max = false;
i = 1;
}
else
{
result= mytext.getText();
mytext.setText(result + "8");
} }
else if (e.getSource() == myJButton9)
{ if (max || i == 0)
{
mytext.setText("9");
max = false;
i = 1;
}
else
{
result=mytext.getText();
mytext.setText(result + "9");
} } /* 添加0按钮
*/
else if (e.getSource() == myJButton0)
{
if (max || i == 0)
{
mytext.setText("0");
max = false;
i = 1;
}
else
{
result=mytext.getText();
if (Float.parseFloat(result) > 0 || Float.parseFloat(result) < 0)
{
mytext.setText(result+ "0");
}
else
{
if (result.trim().indexOf(".") == -1)
{
mytext.setText(result);
}
else
{
mytext.setText(result+ "0");
}
}
}
}
else if (e.getSource() == myJButton16)
{
mytext.setText("0");
i = 0;
max = true;
}
/*本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加,
* 不存在就添加.
*/
else if (e.getSource() == myJButton17)
{
if (max || i == 0)
{
mytext.setText("0.");
max = false;
i = 1;
}
else
{
result=mytext.getText();
if (result.trim().indexOf(".") == -1)
{
mytext.setText(result + ".");
}
else
{
mytext.setText(result);
}
}
}
//获得点击+之前的数值
else if (e.getSource() == myJButton11)
{
max = true;
i = 0;
t = Double.parseDouble(mytext.getText());
s= 1; }//获得点击-之前的数值
else if (e.getSource() == myJButton10)
{
max = true;
i = 0;
t = Double.parseDouble(mytext.getText());
s = 2;
}//获得点击*之前的数值
else if (e.getSource() == myJButton13)
{
max = true;
i = 0;
t = Double.parseDouble(mytext.getText());
System.out.println(t);
s = 3; }//获得点击/之前的数值
else if (e.getSource() == myJButton14)
{
max= true;
i = 0;
t = Double.parseDouble(mytext.getText());
s = 4; }
else if (e.getSource() == myJButton15)
{
switch (s)
{
case 1 ://计算加法
{
double ad =
t + Double.parseDouble(mytext.getText());
mytext.setText(ad + "");
i = 0;
result= "";
break;
}
case 2 ://计算减法
{
double ad = t- Double.parseDouble(mytext.getText()); mytext.setText(String.valueOf(ad));
i = 0;
result = "";
break;
}
case 3 ://计算乘法
{
double ad =
t * Double.parseDouble(mytext.getText());
mytext.setText(ad + "");
i = 0;
result = "";
break;
}
case 4 ://计算除法
{
double ad =
t/ Double.parseDouble(mytext.getText());
mytext.setText(ad + "");
i = 0;
result= "";
break;
}
}
System.out.println(max);
}
}
public static void main(String[] args){ JFrame.setDefaultLookAndFeelDecorated(true);
JSQ frame= new JSQ();
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class calculator extends JFrame implements ActionListener
{
private JTextField text_result;
private JButton button_1,button_2,button_3,button_4,button_5,button_6,button_7
,button_8,button_9,button_0,button_sqrt,button_asb,button_CE,button_C,button_divide,
button_multiply,button_difference,button_equal,button_star,button_plus;
private char c='#',;
private int top=1,basic=1;
private float number[]=new float[3],temp;
private String string="",table;
public calculator()
{
super("Calculator");
this.setSize(300,200);
// this.setLocale(200,300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
text_result=new JTextField();
text_result.setBackground(Color.white);
text_result.setSize(1,1);
text_result.setEditable(false);
text_result.setHorizontalAlignment(JTextField.RIGHT);
//text_result.;
//text_result.setCaretPosition(JTextField.RIGHT);
this.getContentPane().add(text_result,BorderLayout.CENTER);
text_result.show();
JPanel panel=new JPanel(new GridLayout(5,4,5,6));
this.getContentPane().add(panel,BorderLayout.SOUTH);
button_1=new JButton("1");
button_1.addActionListener(this);
//button_1.setBackground(Color.red);
button_2=new JButton("2");
button_2.addActionListener(this);
button_3=new JButton("3");
button_3.addActionListener(this);
button_4=new JButton("4");
button_4.addActionListener(this);
button_5=new JButton("5");
button_5.addActionListener(this);
button_6=new JButton("6");
button_6.addActionListener(this);
button_7=new JButton("7");
button_7.addActionListener(this);
button_8=new JButton("8");
button_8.addActionListener(this);
button_9=new JButton("9");
button_9.addActionListener(this);
button_0=new JButton("0");
button_0.addActionListener(this);
button_sqrt=new JButton("sqrt");
button_sqrt.addActionListener(this);
button_asb=new JButton("&-/+");
button_asb.addActionListener(this);
button_CE=new JButton("CE");
button_CE.addActionListener(this);
button_C=new JButton("C");
button_C.addActionListener(this);
button_divide=new JButton("/");
button_divide.addActionListener(this);
button_multiply=new JButton("*");
button_multiply.addActionListener(this);
button_difference=new JButton("-");
button_difference.addActionListener(this);
button_plus=new JButton("+");
button_plus.addActionListener(this);
button_equal=new JButton("=");
button_equal.addActionListener(this);
button_star=new JButton(".");
button_star.addActionListener(this);
panel.add(button_sqrt);
panel.add(button_asb);
panel.add(button_CE);
panel.add(button_C);
panel.add(button_7);
panel.add(button_8);
panel.add(button_9);
panel.add(button_divide);
panel.add(button_4);
panel.add(button_5);
panel.add(button_6);
panel.add(button_multiply);
panel.add(button_1);
panel.add(button_2);
panel.add(button_3);
panel.add(button_difference);
panel.add(button_0);
panel.add(button_star);
panel.add(button_equal);
panel.add(button_plus);
this.setLocation(400,300);
this.setVisible(true);
//this.getContentPane().isEnabled(false);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
try {
table=e.getActionCommand();
//text_result.setText(table);
=table.charAt(0);
//String string1=text_result.getText();
switch()
{
case '1':string+=button_1.getText();text_result.setText(string);break;
case '2':string+=button_2.getText();text_result.setText(string);break;
case '3':string+=button_3.getText();text_result.setText(string);break;
case '4':string+=button_4.getText();text_result.setText(string);break;
case '5':string+=button_5.getText();text_result.setText(string);break;
case '6':string+=button_6.getText();text_result.setText(string);break;
case '7':string+=button_7.getText();text_result.setText(string);break;
case '8':string+=button_8.getText();text_result.setText(string);break;
case '9':string+=button_9.getText();text_result.setText(string);break;
case '0':string+=button_0.getText();text_result.setText(string);break;
//case '.':string+=button_star.getText();text_result.setText(string);break;
case '&':if(string==""||string.charAt(0)!='-')
string="-"+string;
else
string.replace('-','0');
text_result.setText(string);break;
case '+':number[top]=Float.valueOf(string).floatValue();string="";
basic=top;top=top%2+1;c=;break;
case '-':number[top]=Float.valueOf(string).floatValue();string="";
basic=top;top=top%2+1;c=;break;
case '*':number[top]=Float.valueOf(string).floatValue();string="";
basic=top;top=top%2+1;c=;break;
case '/':number[top]=Float.valueOf(string).floatValue();string="";
basic=top;top=top%2+1;c=;break;
case 'C':string="";text_result.setText("");basic=top=1;break;
case 's':if(string!=""){number[top]=Float.valueOf(string).floatValue();string="";
basic=top;top=top%2+1;}
text_result.setText(""+(Math.sqrt(number[basic])));number[basic]=(float)Math.sqrt(number[basic]);break;
case '=':switch(c)
{
case '+':if(string!=""){number[top]=Float.valueOf(string).floatValue();string="";}
text_result.setText(""+(temp=number[basic]+number[top]));number[basic]=temp;break;
case '-':if(string!=""){number[top]=Float.valueOf(string).floatValue();string="";}
text_result.setText(""+(temp=number[basic]-number[top]));number[basic]=temp;break;
case '*':if(string!=""){number[top]=Float.valueOf(string).floatValue();string="";}
text_result.setText(""+(temp=number[basic]*number[top]));number[basic]=temp;break;
case '/':if(string!=""){number[top]=Float.valueOf(string).floatValue();string="";}
text_result.setText(""+(temp=number[basic]/number[top]));number[basic]=temp;break;
}
}
}catch(Exception ie){}
}
public static void main(String args[])
{
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
calculator ca=new calculator();
}
}
很早以前做的一个,lz可以看看,你的代码实在太多了,看不来~~~~~~~~~~~~~~
已经打好了jar文件,可以直接运行