计算器代码java
‘壹’ 鐢╦ava缂栧缑璁$畻鍣ㄧ▼搴忚蒋浠跺拰婧愪唬镰
java璁$畻鍣ㄦ簮绋嫔簭
java璁$畻鍣
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class calculation extends JFrame
{public calculation() /*鏋勯犳柟娉*/
{super("璁℃暟鍣");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initTextPanel(); /*鏂囨湰妗*/
initControlPanel(); /*鎺у埗阌*/
initKeyPanel(); /*鏁板瓧鍜岃繍绠楃*/
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(TextPanel,BorderLayout.NORTH);
pane.add(ControlPanel,BorderLayout.CENTER);
pane.add(KeyPanel,BorderLayout.SOUTH);
pack();
try
{UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception ex)
{;} /璁╅敭*璁剧疆Windons瑙傛劅*/
SwingUtilities.updateComponentTreeUI(this);
setResizable(false);
setVisible(true);
}
private void initTextPanel() /*璁剧疆鏂囨湰妗*/
{ TextPanel=new JPanel();
TextPanel.setLayout(new FlowLayout());
Resultarea =new JTextField("0",25);
Resultarea.setEditable(false); /*璁剧疆涓嶅彲缂栬緫*/
Color color=Color.white;
Resultarea.setBackground(color); /*棰滆壊*/
Resultarea.setHorizontalAlignment(JTextField.RIGHT); /*璁炬墥婊戞壆缃鏄剧ず浣岖疆*/
TextPanel.add(Resultarea);
}
private void initControlPanel() /*璁剧疆鎺у埗阌鏄ョ伓*/
{ControlPanel=new JPanel();
ControlPanel.setLayout(new GridLayout(1,3,4,4));
JButton b1=new JButton("Backspace"); /*Backspace阌*/
b1.setFont(font1);
b1.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{String s1=Resultarea.getText();
int l=s1.length();
Resultarea.setText(s1.substring(0 ,l-1));
}
});
ControlPanel.add(b1);
JButton b2=new JButton("CE"); /*CE阌*/
b2.setFont(font1);
b2.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{Resultarea.setText("0");
isNew=true;
}
});
ControlPanel.add(b2);
JButton b3=new JButton("C"); /*C阌*/
b3.setFont(font1);
b3.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{Resultarea.setText("0");
pnum="";
operation="";
isNew=true;
}
});
ControlPanel.add(b3);
}
private void initKeyPanel() /*璁剧疆鏁板瓧阌鍜岃繍绠楃﹂敭*/
{String key[] = {"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
KeyPanel = new JPanel();
KeyPanel.setLayout(new GridLayout(4,5,4,4));
for(int i=0;i<20;i++)
{String label = key[i];
JButton b = new JButton(label);
b.setActionCommand(key[i]);
b.setFont(font2);
KeyPanel.add(b);
b.addActionListener(new ActionListener() /*镞犲悕鐩戝惉鍣*/
{public void actionPerformed(ActionEvent e)
{Key_actionPerformed(e);
}
});
}
}
public void Key_actionPerformed(ActionEvent e) /*鏁板瓧阌鍜岃繍绠楃﹂敭镞犲悕鐩戝惉鍣*/
{String s=(e.getActionCommand());
String st=Resultarea.getText();
if(s.equals("0")) /*杈揿叆鏁颁负0*/
{if(st.equals("0"))
return;
else
{if(!isNew)
Resultarea.setText(st+"0");
else
Resultarea.setText("0");
}
isNew=false;
return;
}
if(s.equals("+/-")) /*杈揿叆鏁颁负+/-*/
{double k=Double.parseDouble(st);
{if(k!=0)
display(-k);
}
return;
}
if(s.equals("1")||s.equals("2")||s.equals("3")||s.equals("4")||s.equals("5")||s.equals("6")||s.equals("7")||s.equals("8")||s.equals("9")) /*杈揿叆1-9*/
{if(!isNew)
Resultarea.setText(st+s);
else
{ Resultarea.setText(s);
isNew=false;
}
return;
}
if(s.equals(".")) /*杈揿叆灏忔暟镣*/
{if(Resultarea.getText().indexOf(".")==-1) /*涓嶅瓨鍦ㄥ皬鏁扮偣*/
{if(isNew)
{Resultarea.setText("0.");
isNew=false;
}
else
Resultarea.setText(st+".");
}
return;
}
isNew=true; /*鎸変笅杩愮畻绗︼纴杈揿叆鏂扮殑鏁*/
if(s.equals("="))
{compute(s);
operation="";
}
else
{if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) /*浜岀洰杩愮畻绗﹀彿*/
{if(operation.equals(""))
{pnum=Resultarea.getText();
operation=s;
}
else
compute(s);
}
else /*涓鐩杩愮畻*/
{Count count1=new Count(Double.parseDouble(st));
if(s.equals("sqrt"))
{
display(count1.sqrt());
}
else
{if(s.equals("1/x"))
{if(st.equals("0"))
{Resultarea.setText("闄ゆ暟涓嶈兘涓0.");
operation="";
pnum="";
}
else
display(count1.reciprocal()); /*姹傚掓暟*/
}
else
display(Double.parseDouble(st)/100); /*杈揿叆%锛屼娇褰揿墠鏄剧ず镄勫奸櫎浜100*/
}
}
}
}
private void compute(String s)
{if(s.equals("="))
{if(operation.equals(""))
return;
}
double data1=Double.parseDouble(pnum);
double data2=Double.parseDouble(Resultarea.getText());
Count count2=new Count(data1,data2); /*锷犲噺涔橀櫎璁$畻*/
if(operation.equals("+"))
display((count2.plus()));
else
{if(operation.equals("-"))
display((count2.minus()));
else
{if(operation.equals("*"))
display((count2.multiply()));
else
if(operation.equals("/"))
{if(data2==0)
{Resultarea.setText("闄ゆ暟涓嶈兘涓0");
operation="";
pnum="";
return;
}
else
display((count2.divide()));
}
}
}
operation=""; /*绗﹀彿涓哄綋鍓岖*/
pnum=Resultarea.getText();/*杩愮畻鏁颁负褰揿墠鏂囨湰鏁*/
}
public void display(double result) /*鏄剧ず杈揿嚭鏂规硶*/
{int a=(int)result;
if(a==result)
Resultarea.setText(String.valueOf(a));
else
Resultarea.setText(String.valueOf(result));
if(Resultarea.getText().equals("NaN"))
Resultarea.setText("杈揿叆鍑芥暟镞犳晥");
}
private JPanel TextPanel; /*鏂囨湰妗嗘夋澘*/
private JTextField Resultarea; /*鏂囨湰妗*/
private JPanel ControlPanel; /*鎺у埗阌阌闱㈡澘*/
private JPanel KeyPanel; /*鏁板瓧阌鍜岃繍绠楃﹂敭闱㈡澘*/
private Font font1=new Font("Dialog",0, 10); /*鎺у埗阌瀛椾綋*/
private Font font2 = new Font("Dialog",0,10); /*鏁板瓧阌鍜岀﹀彿阌瀛椾綋*/
private String pnum=""; /*鍓嶆搷浣沧暟*/
private boolean isNew=true; /*鎺у埗鏄钖︽槸鏂版暟*/
private String operation=""; /*杩愮畻绗*/
}
class tester /*娴嬭瘯绫*/
{
public static void main(String[] args)
{
new calculation();
}
}
‘贰’ 用JAVA编写的科学计算器源代码
以下是一个简单的用Java编写的科学计算器的源代码示例:
java
import java.util.Scanner;
public class ScientificCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Scientific Calculator!");
System.out.println("Enter 'add', 'subtract', 'multiply', 'divide', 'sin', 'cos', 'tan', 'log', 'exp', 'sqrt', or 'quit' to exit.");
while (true) {
System.out.print("Enter operation (e.g., add 2 3): ");
String operation = scanner.nextLine();
if (operation.equalsIgnoreCase("quit")) {
break;
}
String[] parts = operation.split(" ");
double num1 = Double.parseDouble(parts[1]);
double num2 = Double.parseDouble(parts[2]);
switch (parts[0].toLowerCase()) {
case "add":
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
break;
case "subtract":
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
break;
case "multiply":
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
break;
case "divide":
if (num2 != 0) {
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
} else {
System.out.println("Error: Division by zero is not allowed.");
}
break;
case "sin":
System.out.println("sin(" + num1 + ") = " + Math.sin(Math.toRadians(num1)));
break;
case "cos":
System.out.println("cos(" + num1 + ") = " + Math.cos(Math.toRadians(num1)));
break;
case "tan":
System.out.println("tan(" + num1 + ") = " + Math.tan(Math.toRadians(num1)));
break;
case "log":
System.out.println("log(" + num1 + ") = " + Math.log10(num1));
break;
case "exp":
System.out.println("exp(" + num1 + ") = " + Math.exp(num1));
break;
case "sqrt":
if (num1 >= 0) {
System.out.println("sqrt(" + num1 + ") = " + Math.sqrt(num1));
} else {
System.out.println("Error: Cannot calculate the square root of a negative number.");
}
break;
default:
System.out.println("Error: Invalid operation.");
break;
}
}
scanner.close();
System.out.println("Goodbye!");
}
}
这个科学计算器支持基本的四则运算(加、减、乘、除)以及一些科学运算(正弦、余弦、正切、对数、指数和平方根)。用户可以通过输入相应的操作和两个数字来执行计算。例如,输入“add 2 3”将计算2加3的结果。
代码首先导入了`Scanner`类,用于从用户处获取输入。然后,在`main`方法中,创建了一个`Scanner`对象,用于读取用户的输入。程序通过一个无限循环来持续接收用户的输入,直到用户输入“quit”为止。
在循环中,程序首先提示用户输入一个操作,然后读取用户的输入并将其分割为多个部分。接着,程序将第二个和第三个部分转换为`double`类型的数字,并根据第一个部分(即操作)执行相应的计算。
程序使用`switch`语句来根据用户输入的操作执行相应的计算。对于基本的四则运算,程序直接执行相应的计算并输出结果。对于科学运算,程序使用了Java的`Math`类中的相应方法。例如,对于正弦运算,程序使用了`Math.sin`方法,并将角度转换为弧度作为参数传递给它。
如果用户输入了无效的操作或无效
‘叁’ java编一个计算器的代码
界面漂亮堪御尺比系统自带穗拆兆计算器,功能完美加减乘除开平方等等猜租全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Counter {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frame.show();
}
}
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();
menuFile.setText("文件");
menuFile1.setText("编辑");
menuFile2.setText("查看");
menuFile3.setText("帮助");
menuFileExit.setText("退出");
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CounterFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuBar.add(menuFile1);
menuBar.add(menuFile2);
menuBar.add(menuFile3);
setTitle("计算器");
setJMenuBar(menuBar);
setSize(new Dimension(400, 280));
this.getContentPane().add(new Allpanel());
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (this.field);
}
}
class Allpanel extends JPanel {
public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}
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=Tool.getinstance().getfield();
this.setLayout(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]);
//this.add(new JButton(arg[i]));
this.add(b);
if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();
if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}
else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();
}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}
}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}
begin="no";
to="yes";
}
});
}
if(i==17)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}
if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}
if(!Vec.contains(".")&&!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}
}
});
}
if(i==18)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
jq=Double.parseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}
if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="/";
}
});
}
if(i==4)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
text.setText(String.valueOf(Math.sqrt(cq)));
}
});
}
if(i==8)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}
if(i==19)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}
else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}
else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}
else
{
jg=String.valueOf(dy);
}
}
text.setText(jg);
Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Tool.getinstance().getfield();
JButton jb1=new JButton("Backspace");
JButton jb2=new JButton(" CE ");
JButton jb3=new JButton(" C ");
this.add(jb1);
this.add(jb2);
this.add(jb3);
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String back=Tool.getinstance().getfield().getText();
text.setText(backmethod(back));
Centercenter.Vec.remove(Centercenter.Vec.size()-1);
}
});
jb3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
text.setText("0.");
Centercenter.Vec.clear();
Centercenter.Vec.add(".");
Centercenter.vc.add("a");
Centercenter.begin="yes";
Centercenter.vc1.clear();
Centercenter.what=null;
Centercenter.tool=0;
}
});
}
public String backmethod(String str)
{
return str.substring(0,str.length()-1);
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
this.setLayout(new GridLayout(4,1,3,3));
this.add(new JButton("MC"));
this.add(new JButton("MR"));
this.add(new JButton("MS"));
this.add(new JButton("M+"));
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}
---------------------------------------------------------------------------
=============《按你要求特意后改过的最简单功能的代码如下》========================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Counter2 {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frame.show();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
setTitle("计算器");
setSize(new Dimension(400, 280));
this.getContentPane().add(new Allpanel());
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (this.field);
}
}
class Allpanel extends JPanel {
public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}
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=Tool.getinstance().getfield();
this.setLayout(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]);
//this.add(new JButton(arg[i]));
this.add(b);
if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();
if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}
else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();
}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}
}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}
begin="no";
to="yes";
}
});
}
if(i==14)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}
if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}
if(!Vec.contains(".")&&!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}
}
});
}
if(i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==11)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
jq=Double.parseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}
if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="/";
}
});
}
if(i==7)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}
if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}
else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}
else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}
else
{
jg=String.valueOf(dy);
}
}
text.setText(jg);
Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Tool.getinstance().getfield();
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}
------------------------------------------------------------
才子_辉祝您愉快!
‘肆’ java 写的计算器源代码只实现加减乘除四则运算即可
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
public class calculator
{
String str1="0"; //运算数1 初值一定为0 为了程序的安全
String str2="0"; //运算数2
String fh="+"; //运算符
String jg="";//结果
//状态开关 重要
int k1=1;//开关1 用于选择输入方向 将要写入str2或 str2
int k2=1;//开关2 符号键 次数 k2>1说明进行的是2+3-9+8 这样的多符号运算
int k3=1;//开关3 str1 是否可以被清0 ==1时可以 !=1时不能被清0
int k4=1;//开关4 str2 同上
int k5=1;//开关5 控制小数点可否被录入 ==1时可以 !=1 输入的小数点被丢掉
JButton jicunqi; //寄存器 记录 是否连续按下符号键
Vector vt=new Vector(20,10);
JFrame frame=new JFrame("sunshine---计算器");
JTextField jg_TextField=new JTextField(jg,20);//20列
JButton clear_Button=new JButton("清除");
JButton button0=new JButton("0");
JButton button1=new JButton("1");
JButton button2=new JButton("2");
JButton button3=new JButton("3");
JButton button4=new JButton("4");
JButton button5=new JButton("5");
JButton button6=new JButton("6");
JButton button7=new JButton("7");
JButton button8=new JButton("8");
JButton button9=new JButton("9");
JButton button_Dian=new JButton(".");
JButton button_jia=new JButton("+");
JButton button_jian=new JButton("-");
JButton button_cheng=new JButton("*");
JButton button_chu=new JButton("/");
JButton button_dy=new JButton("=");
public static void main(String[] args)
{
calculator calculator=new calculator();
}
calculator()
{
jg_TextField.setHorizontalAlignment(JTextField.RIGHT );//文本框 右对齐
JPanel pan=new JPanel();
pan.setLayout(new GridLayout(4,4,5,5));//四行四列 边距为5像素
pan.add(button7);
pan.add(button8);
pan.add(button9);
pan.add(button_chu);
pan.add(button4);
pan.add(button5);
pan.add(button6);
pan.add(button_cheng);
pan.add(button1);
pan.add(button2);
pan.add(button3);
pan.add(button_jian);
pan.add(button0);
pan.add(button_Dian);
pan.add(button_dy);
pan.add(button_jia);
pan.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));//pan对象的边距
JPanel pan2=new JPanel();
pan2.add(jg_TextField);
JPanel pan3=new JPanel(); //为什么要 多此一句呢? 因为我不会设置 按钮的大小
pan3.setLayout(new FlowLayout());
pan3.add(clear_Button);
//clear_Button.setSize(10,10);//设置清零按钮的大小 吗的 不好使 !!
frame.setLocation(300, 200); //主窗口 出现在位置
frame.setResizable(false); //不能调大小
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(pan2,BorderLayout.NORTH);
frame.getContentPane().add(pan,BorderLayout.CENTER);
frame.getContentPane().add(pan3,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
//以上是 控件 和 布局
//下面是事件处理 程 序
//--------------- 数 字 键 ----------------
class JianTing implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1;//还原开关k5状态
}
str1=str1+ss;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//显示
}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //还原开关k5状态
}
str2=str2+ss;
//k2=2;
k4=k4+1;
///////////////测试////////////////
jg_TextField.setText(str2);
}
}
}
//--------符 号-----------
class JianTing_fh implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss2=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if(k2==1)
{
k1=2;//开关 k1 为1时,向数1写 为2时,向数2写
k5=1;
fh=ss2;
k2=k2+1;//按符号键的次数
}
else
{
int a=vt.size();
JButton c=(JButton)vt.get(a-2); if(!(c.getText().equals("+"))&&!(c.getText().equals("-"))&&!(c.getText().equals("*"))&&!(c.getText().equals("/")))
{
yuns();
str1=jg;
k1=2;//开关 k1 为1时,向数1写 为2时,向数2写
k5=1;
k4=1;
fh=ss2;
} k2=k2+1;
}
}
}
//--------清除-------
class JianTing_clear implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
k5=1;
k2=1;
k1=1;
k3=1;
k4=1;
str1="0";
str2="0";
fh="";
jg="";
jg_TextField.setText(jg);
vt.clear();
}
}
//----------------等 于 ---------------------
class JianTing_dy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
yuns();
k1=1; //还原开关k1状态
//str1=jg;
k2=1;
k3=1;//还原开关k3状态
k4=1; //还原开关k4状态
str1=jg; //为7+5=12 +5=17 这种计算做准备
}
}
//----------------小数点 ---------------------
class JianTing_xiaos implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if(k5==1)
{
String ss2=((JButton)e.getSource()).getText();
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1; //还原开关k5状态
}
str1=str1+ss2;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//显示
}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //还原开关k5状态
}
str2=str2+ss2;
//k2=2;
k4=k4+1;
///////////////测试////////////////
jg_TextField.setText(str2);
}
}
k5=k5+1;
}
}
//注册 监听器
JianTing_dy jt_dy=new JianTing_dy();
JianTing jt= new JianTing();//临听数字键
JianTing_fh jt_fh= new JianTing_fh();//临 听符 号键
JianTing_clear jt_c=new JianTing_clear(); //清除键
JianTing_xiaos jt_xs=new JianTing_xiaos();// 小数点 键
button7.addActionListener(jt);
button8.addActionListener(jt);
button9.addActionListener(jt);
button_chu.addActionListener(jt_fh);
button4.addActionListener(jt);
button5.addActionListener(jt);
button6.addActionListener(jt);
button_cheng.addActionListener(jt_fh);
button1.addActionListener(jt);
button2.addActionListener(jt);
button3.addActionListener(jt);
button_jian.addActionListener(jt_fh);
button0.addActionListener(jt);
button_Dian.addActionListener(jt_xs);
button_dy.addActionListener(jt_dy);
button_jia.addActionListener(jt_fh);
clear_Button.addActionListener(jt_c);
//关闭事件处理程序
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
//---------------计 算------------------
public void yuns()
{
double a2,b2;//运算数1,2
String c=fh;// 运算符
double jg2=0 ;//结果
if (c.equals(""))
{
//System.out.println("请输入运算符");
jg_TextField.setText("请输入运算符");
}
else
{
System.out.println("str1:"+str1);//调试时 使 用
System.out.println("str2:"+str2);//调试时 使 用
System.out.println("运算符:"+fh);//调试时 使 用
if (str1.equals(".")) //字符串 "." 转换成double型数据时 会出错 所以手工转
str1="0.0";
if (str2.equals("."))
str2="0.0";
a2=Double.valueOf(str1).doubleValue();
b2=Double.valueOf(str2).doubleValue();
System.out.println("double型的a2:"+a2); //调试时 使 用
System.out.println("double型的b2:"+b2); //调试时 使 用
if (c.equals("+"))
{
jg2=a2+b2;
}
if (c.equals("-"))
{
jg2=a2-b2;
}
if (c.equals("*"))
{
jg2=a2*b2;
}
if (c.equals("/"))
{
if(b2==0)
{
jg2=0;//0000000000000 by 0 cu!
}
else
{
jg2=a2/b2;
}
}
System.out.println("double型a2"+fh+"b2结果:"+jg2);
System.out.println();
jg=((new Double(jg2)).toString());
jg_TextField.setText(jg);
}
}
}
‘伍’ java课程设计题目及代码是什么
java课程设计题目及代码分别是:
1、题目:计算器。设计内容是设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算。
设计要求是设计的计算器应用程序可以完成家法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。
2、代码:
数字按钮NumberButton类如下:
import java.awt.
import java.awt.event.
import javax.swing.
public class NumberButton extends Button.
{
int number.
public NumberButton(int number).
{
super(""+number).
this.number=number.
setForeground(Color.blue).
}
public int getNumber().
{
return number;
}
}
其它java课程设计题目及代码是:
题目:华容道。编写一个按钮的子类,使用该子类创建的对象代表华容道中的人物。通过焦点事件控制人物颜色,当人物获得焦点时颜色为蓝色,当失去焦点时颜色为灰色。
通过键盘事件和鼠标事件来实现曹操、关羽等人物的移动。当人物上发生鼠标事件或键盘事件时,如果鼠标指针的位置是在人物的下方(也就是组件的下半部分)或按下键盘的“↓“键,该人物向下移动。向左、向右和向上的移动原理类似。
代码是:
String name[]={"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"}.
for(int i=0;i<name.length;i++).
{
person[i]=new Person(i,name[i]).
person[i].addKeyListener(this).
person[i].addMouseListener(this).
// person[i].addFocusListener(new Person).
add(person[i]).
}
person[0].setBounds(104,54,100,100).
person[1].setBounds(104,154,100,50).
person[2].setBounds(54,154,50,100).
person[3].setBounds(204,154,50,100).
person[4].setBounds(54,54,50,100).
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);