界面漂亮堪比系统自带计算器,功能完美加减乘除开平方等等全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!
代码如下:
import javaawt;
import javaawtevent;
import javaxswing;
import javautil;
public class Counter {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frameshow();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenu menuFile1 = new JMenu();
JMenu menuFile2 = new JMenu();
JMenu menuFile3 = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFilesetText("文件");
menuFile1setText("编辑");
menuFile2setText("查看");
menuFile3setText("帮助");
menuFileExitsetText("退出");
menuFileExitaddActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CounterFramethiswindowClosed();
}
}
);
menuFileadd(menuFileExit);
menuBaradd(menuFile);
menuBaradd(menuFile1);
menuBaradd(menuFile2);
menuBaradd(menuFile3);
setTitle("计算器");
setJMenuBar(menuBar);
setSize(new Dimension(400, 280));
thisgetContentPane()add(new Allpanel());
thisaddWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFramethiswindowClosed();
}
}
);
}
protected void windowClosed() {
Systemexit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
thisfield=new JTextField(30);
thisfieldsetHorizontalAlignment(JTextFieldRIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (thisfield);
}
}
class Allpanel extends JPanel {
public Allpanel() {
thissetLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
thisadd(np,BorderLayoutNORTH);
thisadd(cp,BorderLayoutCENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/
Method Centercenter
/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Toolgetinstance()getfield();
thissetLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","sqrt","4","5","6","","%","1","2","3","-","1/x","0","+/-","","+","="};
for(int i=0;i<20;i++)
{
final JButton b=new JButton(arg[i]);
//thisadd(new JButton(arg[i]));
thisadd(b);
if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=bgetText();
String ma=textgetText();
if(vc3contains("v3"))
{
textsetText("0"+mark);
vc3clear();
}
else if(vccontains("a"))
{
if(vc2contains("v2"))
{
textsetText("0"+mark);
vcclear();
vc2clear();
}
else
{
textsetText(mark);
vcclear();
Vecclear();
Vecadd(mark);
}
}
else
{
textsetText(matrim()+mark);
Vecadd(mark);
}
begin="no";
to="yes";
}
});
}
if(i==17)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=bgetText();
String m=textgetText();
if("yes"equals(begin))
{
vc3add("v3");
}
if(vc1contains("v1"))
{
vc2add("v2");
vc1clear();
}
if(!Veccontains("")&&!vccontains("a"))
{
textsetText(mtrim()+mar);
Vecadd("");
}
}
});
}
if(i==18)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
add=DoubleparseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="+";
}
});
}
if(i==13)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
jq=DoubleparseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="-";
}
});
}
if(i==3)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cq=DoubleparseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="/";
}
});
}
if(i==4)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cq=DoubleparseDouble(ma);
textsetText(StringvalueOf(Mathsqrt(cq)));
}
});
}
if(i==8)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cs=DoubleparseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=toolcs;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="";
}
});
}
if(i==19)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
dy=DoubleparseDouble(ma);
if(what=="add")
{
jg=StringvalueOf((tool+dy));
}
if(what=="jq")
{
jg=StringvalueOf((tool-dy));
}
if(what=="cs")
{
jg=StringvalueOf((tooldy));
}
if(what=="cq")
{
jg=StringvalueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=StringvalueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=StringvalueOf(dy-tool);
}
else if(to=="")
{
tool=cs;
jg=StringvalueOf(dytool);
}
else if(to=="/")
{
tool=cq;
jg=StringvalueOf(dy/tool);
}
else
{
jg=StringvalueOf(dy);
}
}
textsetText(jg);
Vecclear();
Vecadd("");
vcadd("a");
vc1add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Toolgetinstance()getfield();
JButton jb1=new JButton("Backspace");
JButton jb2=new JButton(" CE ");
JButton jb3=new JButton(" C ");
thisadd(jb1);
thisadd(jb2);
thisadd(jb3);
jb1addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String back=Toolgetinstance()getfield()getText();
textsetText(backmethod(back));
CentercenterVecremove(CentercenterVecsize()-1);
}
});
jb3addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
textsetText("0");
CentercenterVecclear();
CentercenterVecadd("");
Centercentervcadd("a");
Centercenterbegin="yes";
Centercentervc1clear();
Centercenterwhat=null;
Centercentertool=0;
}
});
}
public String backmethod(String str)
{
return strsubstring(0,strlength()-1);
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
thissetLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
thisadd(cn,BorderLayoutNORTH);
thisadd(cc,BorderLayoutCENTER);
thisadd(cw,BorderLayoutWEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
thissetLayout(new GridLayout(4,1,3,3));
thisadd(new JButton("MC"));
thisadd(new JButton("MR"));
thisadd(new JButton("MS"));
thisadd(new JButton("M+"));
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Toolgetinstance()getfield();
thisadd(tf);
}
}
---------------------------------------------------------------------------
=============《按你要求特意后改过的最简单功能的代码如下》========================
import javaawt;
import javaawtevent;
import javaxswing;
import javautil;
public class Counter2 {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frameshow();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
setTitle("计算器");
setSize(new Dimension(400, 280));
thisgetContentPane()add(new Allpanel());
thisaddWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFramethiswindowClosed();
}
}
);
}
protected void windowClosed() {
Systemexit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
thisfield=new JTextField(30);
thisfieldsetHorizontalAlignment(JTextFieldRIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (thisfield);
}
}
class Allpanel extends JPanel {
public Allpanel() {
thissetLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
thisadd(np,BorderLayoutNORTH);
thisadd(cp,BorderLayoutCENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/
Method Centercenter
/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Toolgetinstance()getfield();
thissetLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","4","5","6","","1","2","3","-","0","=","","+"};
for(int i=0;i<16;i++)
{
final JButton b=new JButton(arg[i]);
//thisadd(new JButton(arg[i]));
thisadd(b);
if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=bgetText();
String ma=textgetText();
if(vc3contains("v3"))
{
textsetText("0"+mark);
vc3clear();
}
else if(vccontains("a"))
{
if(vc2contains("v2"))
{
textsetText("0"+mark);
vcclear();
vc2clear();
}
else
{
textsetText(mark);
vcclear();
Vecclear();
Vecadd(mark);
}
}
else
{
textsetText(matrim()+mark);
Vecadd(mark);
}
begin="no";
to="yes";
}
});
}
if(i==14)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=bgetText();
String m=textgetText();
if("yes"equals(begin))
{
vc3add("v3");
}
if(vc1contains("v1"))
{
vc2add("v2");
vc1clear();
}
if(!Veccontains("")&&!vccontains("a"))
{
textsetText(mtrim()+mar);
Vecadd("");
}
}
});
}
if(i==15)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
add=DoubleparseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="+";
}
});
}
if(i==11)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
jq=DoubleparseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="-";
}
});
}
if(i==3)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cq=DoubleparseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="/";
}
});
}
if(i==7)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
cs=DoubleparseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=toolcs;
textsetText(StringvalueOf((tool)));
}
vcadd("a");
vc1add("v1");
to="";
}
});
}
if(i==13)
{
baddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=textgetText();
dy=DoubleparseDouble(ma);
if(what=="add")
{
jg=StringvalueOf((tool+dy));
}
if(what=="jq")
{
jg=StringvalueOf((tool-dy));
}
if(what=="cs")
{
jg=StringvalueOf((tooldy));
}
if(what=="cq")
{
jg=StringvalueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=StringvalueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=StringvalueOf(dy-tool);
}
else if(to=="")
{
tool=cs;
jg=StringvalueOf(dytool);
}
else if(to=="/")
{
tool=cq;
jg=StringvalueOf(dy/tool);
}
else
{
jg=StringvalueOf(dy);
}
}
textsetText(jg);
Vecclear();
Vecadd("");
vcadd("a");
vc1add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Toolgetinstance()getfield();
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
thissetLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
thisadd(cn,BorderLayoutNORTH);
thisadd(cc,BorderLayoutCENTER);
thisadd(cw,BorderLayoutWEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Toolgetinstance()getfield();
thisadd(tf);
}
}
------------------------------------------------------------
才子_辉祝您愉快!
double 是双精度类型,也就是说你的d是double,而你的d100中的100是整形(int)。这是自动类型转换
符合以下条件时java将会做自动类型转换:
(1)转换前的数据类型与转换后的类型兼容。
(2)转换后的数据类型的表示范围比转换前的类型大。
其中double的表示范围比int大,符合条件(2)。因此java会自动将原来为int的类型的变量100转换为double类型。
打开IED:打开自己java编程的软件,采用的是eclipse软件。
建立java工程。
编写类。
编写类的详细步骤1类的基本结构:
由于这里用到了界面,所以要进行窗口界面的编程,按钮事件的处理,和计算处理界面;
package MyCaculator;
import javaawt;
import javaawtevent;
import javaxswing;
public class MyCaculator extends JFrame {
private int add=1,sub=2,mul=3,div=4;
private int op=0;
boolean ifOp;
private String output="0";
private Button[] jba=new Button[]{new Button("7"),new Button("8"),
new Button("9"),new Button("+"),
new Button("4"),new Button("5"),new Button("6"),new Button("-"),
new Button("1"),new Button("2"),new Button("3"),new Button(""),
new Button("0"),new Button(""),new Button("="),new Button("/")};
private JPanel jpt=new JPanel();
private JPanel jpb=new JPanel();
private JTextField jtf=new JTextField("");
private MyCaculator(){
}
private void operate(String x){
}
public String add(String x){
return output;
}
public String subtract(String x){
return output;
}
public String multiply(String x){
return output;
}
public String divide(String x){
return output;
}
public String Equals(String x){
return output;
}
public void opClean(){
}
class setOperate_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
class setLabel_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String[] args) {
}
}
2建立构造方法:
所谓构造方法就是,对自己的主类进行初始化,代码如下:
private MyCaculator(){
jptsetLayout(new BorderLayout());
jptadd(jtf);
thisadd(jpt,BorderLayoutNORTH);
jpbsetLayout(new GridLayout(4,4));
for(int i=0;i<jbalength;i++){
jpbadd(jba[i]);
if(i==3||i==7||i==11||i==15||i==14)
jba[i]addActionListener(new setOperate_Act());
else
jba[i]addActionListener(new setLabel_Act());
}
thisadd(jpb,BorderLayoutCENTER);
thissetSize(250, 200);
thissetResizable(false);
thissetVisible(true);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
3建立数据计算方法
这里的数据计算方法有6个,一个是主方法其他几个是加减乘除的处理方法,代码如下:
private void operate(String x){
double x1=DoublevalueOf(x);
double y=DoublevalueOf(output);
switch(op){
case 0:output=x;break;
case 1:output=StringvalueOf(y+x1);break;
case 2:output =StringvalueOf(y-x1);break;
case 3:output =StringvalueOf(yx1);break;
case 4:
if(x1!=0) output=StringvalueOf(y/x1);
else output="不能为0";
break;
}
}
public String add(String x){
operate(x);
op=add;
return output;
}
public String subtract(String x){
operate(x);
op=sub;
return output;
}
public String multiply(String x){
operate(x);
op=mul;
return output;
}
public String divide(String x){
operate(x);
op=div;
return output;
}
public String Equals(String x){
operate(x);
op=0;
return output;
}
public void opClean(){
op=0;
output ="0";
}
4事件处理方法
这里的时间处理方法,没有建立一个整体的方法,二是在为了便于处理的方法,将按钮事件分成两个部分,并采用两个子类来实现,这两个类时内部类要写在主类内部的,代码如下:
class setOperate_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(egetSource()==jba[3]){
jtfsetText(add(jtfgetText()));
ifOp=true;
}
else if(egetSource()==jba[7]){
jtfsetText(subtract(jtfgetText()));
ifOp=true;
}
else if(egetSource()==jba[11]){
jtfsetText(multiply(jtfgetText()));
ifOp=true;
}
else if(egetSource()==jba[15]){
jtfsetText(divide(jtfgetText()));
ifOp=true;
}
else if(egetSource()==jba[14]){
jtfsetText(Equals(jtfgetText()));
ifOp=true;
}
}
}
class setLabel_Act implements ActionListener{
public void actionPerformed(ActionEvent e) {
Button tempb=(Button)egetSource();
if(ifOp){
jtfsetText(tempbgetLabel());
ifOp=false;
}else {
jtfsetText(jtfgetText()+tempbgetLabel());
}
}
}
5建立main方法:
要想实现我们的代码,我们需在main方法中,实例化我们的对象。
public static void main(String[] args) {
new MyCaculator();
}
在java文件头上导入DecimalFormat包,控制java的格式化输出;举例如下:
import javatext;//导入javatextDecimalFormat包,
public class test{
public static void main(String arg[]){
DecimalFormat df=new DecimalFormat("00");//确定输出格式以“00”格式输出结果
Systemoutprintln(dfformat(03f1000f));//输出结果:300
}
}
以上就是关于java编一个计算器的代码全部的内容,包括:java编一个计算器的代码、java double b = 0.333 , 乘以100 以后小数位变多;、怎么用JAVA编程编写一个计算器等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)