import java.awt.*;
創(chuàng)新互聯(lián)是一家專業(yè)從事成都網(wǎng)站設計、成都網(wǎng)站建設、網(wǎng)頁設計的品牌網(wǎng)絡公司。如今是成都地區(qū)具影響力的網(wǎng)站設計公司,作為專業(yè)的成都網(wǎng)站建設公司,創(chuàng)新互聯(lián)依托強大的技術實力、以及多年的網(wǎng)站運營經(jīng)驗,為您提供專業(yè)的成都網(wǎng)站建設、營銷型網(wǎng)站建設及網(wǎng)站設計開發(fā)服務!
import java.awt.event.*;
import javax.swing.*;
public class calculator extends JFrame implements ActionListener {
private boolean i=true,clickable = true ;//這里的clickabel 是用來判斷小數(shù)點的 如果有小數(shù)點的話 clickable則為false就不對小數(shù)點進行添加了 i在下面有解釋
private double value=0; //這個就是顯示出來的值了
String operate = "="; //操作數(shù) 默認情況下是=號
JFrame jframe=new JFrame("計算機");
JTextField text1=new JTextField("0");
JButton back = new JButton("退格");
JButton daoshu = new JButton("1/x");
JButton remain = new JButton("%");
JButton PI = new JButton("PI"); //顯示圓周率小數(shù)點后7位
JButton square = new JButton("x^2");
JButton cube = new JButton("x^3");
JButton plu=new JButton("+");
JButton min=new JButton("-");
JButton mul=new JButton("*");
JButton equ=new JButton("=");
JButton div=new JButton("/");
JButton code=new JButton("+/-");
JButton point=new JButton(".");
JButton num1=new JButton("1");
JButton num2=new JButton("2");
JButton num3=new JButton("3");
JButton num4=new JButton("4");
JButton num5=new JButton("5");
JButton num6=new JButton("6");
JButton num7=new JButton("7");
JButton num8=new JButton("8");
JButton num9=new JButton("9");
JButton num0=new JButton("0");
JButton num00=new JButton("00");
JButton clear=new JButton("C");
calculator(){
jframe.setSize(400, 400);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setBackground(Color.gray);
p.setLayout(new GridLayout(5, 5)); //采用的布局方式為網(wǎng)格布局
PI.addActionListener(this);
daoshu.addActionListener(this);
remain.addActionListener(this);
square.addActionListener(this);
cube.addActionListener(this);
plu.addActionListener(this);
equ.addActionListener(this);
num1.addActionListener(this);
num2.addActionListener(this);
clear.addActionListener(this);
num3.addActionListener(this);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
num0.addActionListener(this);
num00.addActionListener(this);
min.addActionListener(this);
code.addActionListener(this);
div.addActionListener(this);
point.addActionListener(this);
mul.addActionListener(this);
back.addActionListener(this);
jframe.add(p);
p.add(PI);
p.add(code);
p.add(daoshu);
p.add(back);
p.add(clear);
p.add(num7);
p.add(num8);
p.add(num9);
p.add(plu);
p.add(remain);
p.add(num4);
p.add(num5);
p.add(num6);
p.add(min);
p.add(square);
p.add(num1);
p.add(num2);
p.add(num3);
p.add(mul);
p.add(cube);
p.add(num0);
p.add(num00);
p.add(point);
p.add(div);
p.add(equ);
jframe.add(text1,"North");
jframe.setLocation(300, 300);
jframe.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
String str1,str2;
Object soc=evt.getSource();
String s = evt.getActionCommand(); //獲取按鈕的數(shù)據(jù)
if(soc==back){ //進行退格操作
str1=text1.getText();
int n=str1.length();
text1.setText(str1.substring(0,n-1));
str2 = text1.getText();
}
else if (s.equals("x^2")) {
str1=text1.getText();
double k = Double.parseDouble(str1);
k *=k;
text1.setText(String.valueOf(k));
}
else if (s.equals("x^3")) {
str1=text1.getText();
double k = Double.parseDouble(str1);
k =k*k*k;
text1.setText(String.valueOf(k));
}
else if(s.equals("PI")){ //顯示圓周率
double Pi=3.1415926;
String pi=String.valueOf(Pi);
text1.setText(pi);
}
else if(s.equals("+/-")){ //變換正負號
double a = 0;
str1=text1.getText();
a=Double.parseDouble(str1.trim());
a=-1*a;
str1=String.valueOf(a);
text1.setText(str1);
}
else if(s.equals("1/x")){ //求倒
str1=text1.getText();
double i = 1/Double.parseDouble(str1.trim());
String k = String.valueOf(i);
text1.setText(k);
}
else if(s.equals(".")){ //小數(shù)點
clickable=true;
for (int i = 0; i text1.getText().length(); i++)
if ('.' == text1.getText().charAt(i))
{
clickable=false;
break;
} //第一層判斷是否里面含有小數(shù)點;
if(clickable==true) //第二層判斷
text1.setText(text1.getText()+".");
i=false;
}
else if(s.equals("C")){ //清零 所有的數(shù)據(jù)變回初始值
value = 0;
text1.setText("0");
i=true;
operate = "=" ;
}
else if ('0' = s.charAt(0) s.charAt(0) = '9' ) { //若輸入的為數(shù)字以及小數(shù)點的時候執(zhí)行下面
if (i) { //判斷獲得的command是否為操作符
text1.setText(s);
}
else {
text1.setText(text1.getText() + s);
}
i = false;
}
else { //若輸入運算操作符的話則執(zhí)行下面的代碼
count(Double.parseDouble(text1.getText().trim())); //這里一開始調(diào)用 count()方法的話首先執(zhí)行的是operate.equals("=")
//執(zhí)行上個操作符的運算
operate = s;
i = true;
}
}
private void count(double a) { //計算區(qū)域
if (operate.equals("+")) {
value += a;
}
else if (operate.equals("-")) {
value -= a;
}
else if (operate.equals("*")) {
value *= a;
}
else if (operate.equals("/")) {
value /= a;
}
else if (operate.equals("%")) {
value %= a;
}
else if (operate.equals("=")) {
value = a;
}
text1.setText(Double.toString(value));
}
public static void main(String[] arg)
{
calculator cal=new calculator();
}
}
這是例子 你可以參考下~監(jiān)聽的 部分
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
public class calculator
{
String str1="0"; //運算數(shù)1 初值一定為0 為了程序的安全
String str2="0"; //運算數(shù)2
String fh="+"; //運算符
String jg="";//結(jié)果
//狀態(tài)開關 重要
int k1=1;//開關1 用于選擇輸入方向 將要寫入str2或 str2
int k2=1;//開關2 符號鍵 次數(shù) k21說明進行的是2+3-9+8 這樣的多符號運算
int k3=1;//開關3 str1 是否可以被清0 ==1時可以 !=1時不能被清0
int k4=1;//開關4 str2 同上
int k5=1;//開關5 控制小數(shù)點可否被錄入 ==1時可以 !=1 輸入的小數(shù)點被丟掉
JButton jicunqi; //寄存器 記錄 是否連續(xù)按下符號鍵
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); //主窗口 出現(xiàn)在位置
frame.setResizable(false); //不能調(diào)大小
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);
//以上是 控件 和 布局
//下面是事件處理 程 序
//--------------- 數(shù) 字 鍵 ----------------
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狀態(tài)
}
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狀態(tài)
}
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時,向數(shù)1寫 為2時,向數(shù)2寫
k5=1;
fh=ss2;
k2=k2+1;//按符號鍵的次數(shù)
}
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時,向數(shù)1寫 為2時,向數(shù)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狀態(tài)
//str1=jg;
k2=1;
k3=1;//還原開關k3狀態(tài)
k4=1; //還原開關k4狀態(tài)
str1=jg; //為7+5=12 +5=17 這種計算做準備
}
}
//----------------小數(shù)點 ---------------------
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狀態(tài)
}
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狀態(tài)
}
str2=str2+ss2;
//k2=2;
k4=k4+1;
///////////////測試////////////////
jg_TextField.setText(str2);
}
}
k5=k5+1;
}
}
//注冊 監(jiān)聽器
JianTing_dy jt_dy=new JianTing_dy();
JianTing jt= new JianTing();//臨聽數(shù)字鍵
JianTing_fh jt_fh= new JianTing_fh();//臨 聽符 號鍵
JianTing_clear jt_c=new JianTing_clear(); //清除鍵
JianTing_xiaos jt_xs=new JianTing_xiaos();// 小數(shù)點 鍵
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;//運算數(shù)1,2
String c=fh;// 運算符
double jg2=0 ;//結(jié)果
if (c.equals(""))
{
//System.out.println("請輸入運算符");
jg_TextField.setText("請輸入運算符");
}
else
{
System.out.println("str1:"+str1);//調(diào)試時 使 用
System.out.println("str2:"+str2);//調(diào)試時 使 用
System.out.println("運算符:"+fh);//調(diào)試時 使 用
if (str1.equals(".")) //字符串 "." 轉(zhuǎn)換成double型數(shù)據(jù)時 會出錯 所以手工轉(zhuǎn)
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); //調(diào)試時 使 用
System.out.println("double型的b2:"+b2); //調(diào)試時 使 用
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結(jié)果:"+jg2);
System.out.println();
jg=((new Double(jg2)).toString());
jg_TextField.setText(jg);
}
}
}
import java.text.DecimalFormat;
import java.util.Scanner;
public class Zhidao {
public static void main(String[] args) {
String condition = "";
Zhidao zhidao = new Zhidao();
do{
Scanner scanner = new Scanner(System.in);
try{
System.out.print("請輸入第一個數(shù):");
double x = scanner.nextDouble();
System.out.print("請輸入第二個數(shù):");
double y = scanner.nextDouble();
System.out.print("請輸入運算符:");
String s = scanner.next();
char z = s.charAt(0);
zhidao.yunsuan(x, y, z);
}catch(Exception e){
System.out.println("請輸入正確的數(shù)據(jù)!");
}
System.out.print("是否繼續(xù)?continue:繼續(xù),任意字符:結(jié)束");
condition = scanner.next();
}while("continue".equals(condition));
}
public static void yunsuan(double x,double y,Character z){
DecimalFormat r=new DecimalFormat();
r.applyPattern("#0.00");
if(z.equals('+')){
System.out.println(x+"+"+y+"=" + r.format((x+y)));
} else if(z.equals('-')){
System.out.println(x+"-"+y+"=" + r.format((x-y)));
} else if(z.equals('*')){
System.out.println(x+"*"+y+"=" + r.format((x*y)));
} else if(z.equals('/')){
if(y==0){
System.out.println("被除數(shù)不能為0");
} else{
System.out.println(x+"/"+y+"=" + r.format((x/y)));
}
}else{
System.out.println("無法識別改運算符");
}
}
}
剛好用設計模式(簡單工廠模式)編寫了一個計算器。
package?com.medavis.simplefactory.ui;
import?java.util.Scanner;
import?com.medavis.simplefactory.operate.OperatorFactory;
public?class?CaculatorUI?{
private?static?Scanner?sc;
public?static?void?main(String[]?args)?{
try?{
System.out.println("Please?input?a?number:");
sc?=?new?Scanner(System.in);
long?first?=?sc.nextLong();
System.out.println("Please?input?a?operational?character?(+-*/):");
sc?=?new?Scanner(System.in);
String?operator?=?sc.nextLine();
System.out.println("Please?input?a?number:");
sc?=?new?Scanner(System.in);
long?second?=?sc.nextLong();
OperatorFactory?opf=new?OperatorFactory();
System.out.println(first?+?operator?+?second?+?"="?+?opf.getOperator(operator).getResult(first,?second));
}?catch(Exception?e)?{
System.out.println("Your?input?is?wrong!");
}
}
}
package?com.medavis.simplefactory.operate;
public?class?OperatorAdd?extends?Operator{
@Override
public?long?getResult(long?first,?long?second)?{
return?first+second;
}
}
package?com.medavis.simplefactory.operate;
public?class?OperatorDiv??extends?Operator{
@Override
public?long?getResult(long?first,?long?second)?{
return?first/second;
}
}
package?com.medavis.simplefactory.operate;
public?class?OperatorFactory?{
public?Operator?getOperator(String?operator)?throws?Exception{
switch(operator){
case?"+":
return?new?OperatorAdd();
case?"-":
return?new?OperatorSub();
case?"*":
return?new?OperatorMul();
case?"/":
return?new?OperatorDiv();
default:
throw?new?Exception();?
}
}
}
package?com.medavis.simplefactory.operate;
public?class?OperatorMul??extends?Operator?{
@Override
public?long?getResult(long?first,?long?second)?{
return?first*second;
}
}
package?com.medavis.simplefactory.operate;
public?class?OperatorSub??extends?Operator{
@Override
public?long?getResult(long?first,?long?second)?{
return?first-second;
}
}
(首先建個類,把這些復制粘貼進去)
import java.awt.*;
import javax.swing.*;
public class F {
JFrame frame = new JFrame("計算機");
JPanel pl = new JPanel();
JPanel p2 = new JPanel();
static JTextField show = new JTextField();
static JButton b0 = new JButton("0");
static JButton b1 = new JButton("1");
static JButton b2 = new JButton("2");
static JButton b3 = new JButton("3");
static JButton b4 = new JButton("4");
static JButton b5 = new JButton("5");
static JButton b6 = new JButton("6");
static JButton b7 = new JButton("7");
static JButton b8 = new JButton("8");
static JButton b9 = new JButton("9");
JButton bjia = new JButton("+");
JButton bjian = new JButton("-");
JButton bcheng = new JButton("*");
JButton bchu = new JButton("/");
JButton bdian = new JButton(".");
JButton bdeng = new JButton("=");
JButton bqingchu = new JButton("清除");
public void y() {
pl.setLayout(new GridLayout(1, 1));
pl.add(show);
}
public void p() {
b1.addActionListener(new U());
b2.addActionListener(new U());
b3.addActionListener(new U());
b4.addActionListener(new U());
b5.addActionListener(new U());
b6.addActionListener(new U());
b7.addActionListener(new U());
b8.addActionListener(new U());
b9.addActionListener(new U());
b0.addActionListener(new U());
bjia.addActionListener(new Fu());
bjian.addActionListener(new Fu());
bcheng.addActionListener(new Fu());
bchu.addActionListener(new Fu());
bdeng.addActionListener(new Deng());
bqingchu.addActionListener(new Qing());
p2.setLayout(new GridLayout(6, 3));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b0);
p2.add(bjia);
p2.add(bjian);
p2.add(bcheng);
p2.add(bchu);
p2.add(bdian);
p2.add(bqingchu);
p2.add(bdeng);
}
public void o() {
frame.setLayout(new BorderLayout());
frame.add(pl, BorderLayout.NORTH);
frame.add(p2, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
F f = new F();
f.y();
f.p();
f.o();
}
}
(再新建個類 把這些也復制粘貼進去)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class U implements ActionListener {
public static String str = "";
public static String a = "";
public static String b = "";
public static String k = "";
public void actionPerformed(ActionEvent e) {
String w = e.getActionCommand();//字
if (k.equals("")) {
a += w;
F.show.setText(a);
} else {
b += w;
F.show.setText(b);
}
}
}
(再新建一個,把下面的復制粘貼)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Deng implements ActionListener {
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(U.a);
int b = Integer.parseInt(U.b);
int c = 0;
if (U.k.equals("+")) {
c = a + b;
} else
if (U.k.equals("-")) {
c = a - b;
} else
if (U.k.equals("*")) {
c = a * b;
} else
if (U.k.equals("/")) {
c = a / b;
} else {
}
String d = String.valueOf(c);
F.show.setText(d);
U.a = d;
U.b = "";
U.k = "";
}
}
(在建一個 復制粘貼)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Fu implements ActionListener {
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();//字
U.k = a;
}
}
(在建一個)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Qing implements ActionListener {
public void actionPerformed(ActionEvent e) {
U.a = "";
U.b = "";
U.k = "";
F.show.setText("");
}
}