import java.awt.*;
成都創(chuàng)新互聯(lián)2013年開創(chuàng)至今,先為烏海海南等服務(wù)建站,烏海海南等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為烏海海南企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;public class CaculatorA {
private JFrame jf;
private JButton[] jbs;
private JTextField jtf;
private JButton clear;
private double num1,num2,jieguo;
private char c;
/**
* 構(gòu)造方法實例化屬性
*
*/
public CaculatorA(){
jf=new JFrame("我的計算器v1.0");
jtf=new JTextField(20);
clear=new JButton("clear");
jbs=new JButton[16];
String str="123+456-789*0./=";
for(int i=0; istr.length(); i++){
jbs[i]=new JButton(str.charAt(i)+"");
}
init();
addEventHandler();
// setFont();
// setColor();
showMe();
}
/**
哥們,給你一個,很早寫的
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator {
//計算器面板
private JFrame f = new JFrame("Calculator");
//輸入面扳
private JPanel inputPanel = new JPanel();
//加減乘除面板
private JPanel operationPanel = new JPanel();
//數(shù)字面板
private JPanel buttonsPanel = new JPanel();
//輸入數(shù)據(jù)文本框
private JTextField input = new JTextField(20);
//退格鍵
private JButton backspace = new JButton("BackSpace");
//清空
private JButton CE = new JButton("CE ");
//刪除
private JButton C = new JButton("C ");
//四則運(yùn)算符號鍵
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");
//小數(shù)點
private JButton decimal = new JButton(".");
//等號
private JButton equal = new JButton("=");
//數(shù)字鍵
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");
private String num1 = "";//保存第一個運(yùn)算數(shù)字
private String operator = "";//保存運(yùn)算符號
public static void main(String[] args) {
new Calculator();//new計算器實例
}
public Calculator(){
//添加組件,布局
inputPanel.add(input);
f.add(inputPanel, BorderLayout.NORTH);
operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
f.add(operationPanel, BorderLayout.CENTER);
buttonsPanel.add(add);
buttonsPanel.add(sub);
buttonsPanel.add(mul);
buttonsPanel.add(div);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(zero);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(decimal);
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(equal);
buttonsPanel.setLayout(new GridLayout(4, 4));
f.add(buttonsPanel, BorderLayout.SOUTH);
//注冊各個組件監(jiān)聽事件
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());
//注冊四則運(yùn)算監(jiān)聽
add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());
//注冊數(shù)字監(jiān)聽事件
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class NumberMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(input.getText().trim().equals("0")){//如果文本框已經(jīng)是0,結(jié)果還是0
input.setText(((JButton)e.getSource()).getText().trim());
}else{//否則的話,把0添加到后面,譬如文本框是1,結(jié)果就為10
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class CalcMouseListener implements MouseListener{
//如果輸入的是運(yùn)算符號,保存第一個結(jié)果和運(yùn)算符號
public void mouseClicked(MouseEvent e) {
num1 = input.getText().trim();input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
private class OperationMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
if(e.getSource() == backspace){//退格鍵,刪除一個直到?jīng)]有字符刪除
String inputText = input.getText();
if(inputText.length() 0){
input.setText(inputText.substring(0, inputText.length() - 1));
}
}else if(e.getSource() == C){
input.setText("0");//C,清空所有運(yùn)算數(shù)字和符號
num1 = "";
}else if(e.getSource() == CE){
input.setText("0");//CE--將文本框置為0
}else if(e.getSource() == decimal){
String text = input.getText().trim();
//如果按了小數(shù)點,如果文本框已經(jīng)有小數(shù)點,不做任何操作,否則在結(jié)果后面加上小數(shù)點
if(text.indexOf(".") == -1){
input.setText(text.concat("."));
}
}else if(e.getSource() == equal){
//如果是等號
if(!operator.trim().equals("")){
if(!input.getText().trim().equals("")){
double result = 0D;
if(operator.equals("+")){//執(zhí)行加法運(yùn)算
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){//減法運(yùn)算
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){//乘法運(yùn)算
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){//除法運(yùn)算
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
}
//格式化最終結(jié)果,保留兩位小數(shù)點
input.setText(new DecimalFormat("0.00").format(result));
}
}
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
}
簡單寫了下,代碼如下請參照:
/**
*?計算器類
*?
*?@author?Administrator
*
*/
public?class?Calculator?extends?JFrame?implements?ActionListener?{
private?static?final?long?serialVersionUID?=?3868243398506940702L;
//?文本框
private?JTextField?result;
//?按鈕數(shù)組
private?JButton[]?buttons;
//?按鈕文本
private?final?String[]?characters?=?{?"7",?"8",?"9",?"/",?"4",?"5",?"6",
"*",?"1",?"2",?"3",?"-",?"0",?".",?"=",?"+"?};
//?是否為第一個輸入的數(shù)字
private?boolean?isFirstDigit?=?true;
//?運(yùn)算結(jié)果
private?double?resultNum?=?0.0;
//?運(yùn)算符
private?String?operator?=?"=";
public?Calculator(String?title)?{
//?設(shè)置標(biāo)題欄
super(title);
//?初始化各組件
init();
//?注冊各組件監(jiān)聽器
registerListener();
//?顯示窗體
setVisible(true);
}
/**
*?初始化各組件
*/
private?void?init()?{
//?常用屬性初始化
setSize(220,?200);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
/*?文本框?qū)ο蟪跏蓟?*/
result?=?new?JTextField("0");
//?文本右對齊
result.setHorizontalAlignment(JTextField.RIGHT);
//?設(shè)置是否可編輯
result.setEditable(false);
/*?按鈕初始化?*/
buttons?=?new?JButton[characters.length];
for?(int?i?=?0;?i??buttons.length;?i++)?{
buttons[i]?=?new?JButton(characters[i]);
buttons[i].setFocusable(false);?//?不允許按鈕定位焦點
}
/*?將文本框與按鈕添加到窗體中?*/
add(result,?BorderLayout.NORTH);
JPanel?pnl?=?new?JPanel(new?GridLayout(4,?4,?5,?5));
for?(JButton?jButton?:?buttons)?{
pnl.add(jButton);
}
add(pnl);
this.getContentPane().setFocusable(true);
}
/**
*?注冊監(jiān)聽器
*/
private?void?registerListener()?{
for?(JButton?jButton?:?buttons)?{
jButton.addActionListener(this);
}
//?注冊鍵盤事件
this.getContentPane().addKeyListener(new?KeyAdapter()?{
@Override
public?void?keyPressed(KeyEvent?e)?{
String?text?=?String.valueOf(e.getKeyChar());
if?(Character.isDigit(text.charAt(0))?||?".".equals(text))?{?//?數(shù)字或小數(shù)點
handleNumber(text);
}?else?if?("+-*/=".indexOf(text)?!=?-1)?{?//?運(yùn)算符
handleOperator(text);
}?else?if?(e.getKeyCode()?==?8)?{?//?退格鍵
String?tmp?=?result.getText().trim();
if?(tmp.length()?==?1)?{
result.setText("0");
isFirstDigit?=?true;
}?else?{
result.setText(tmp.substring(0,?tmp.length()?-?1));
}
}
}
});
}
@Override
public?void?actionPerformed(ActionEvent?e)?{
JButton?btn?=?(JButton)?e.getSource();
String?text?=?btn.getText().trim();
if?(Character.isDigit(text.charAt(0))?||?".".equals(text))?{?//?處理數(shù)字和小數(shù)點
handleNumber(text);
}?else?{?//?處理運(yùn)算符
handleOperator(text);
}
}
/**
*?處理數(shù)字和小數(shù)點
*?
*?@param?text
*/
private?void?handleNumber(String?text)?{
if?(isFirstDigit)?{?//?第一次輸入
if?(".".equals(text))?{
this.result.setText("0.");
}?else?{
this.result.setText(text);
}
}?else?if?("0".equals(text)??"0".equals(this.result.getText()))?{
isFirstDigit?=?true;
return;
}?else?if?(".".equals(text)??this.result.getText().indexOf(".")?==?-1)?{
this.result.setText(this.result.getText()?+?".");
}?else?if?(!".".equals(text))?{
this.result.setText(this.result.getText()?+?text);
}
isFirstDigit?=?false;
}
/**
*?處理運(yùn)算符
*?
*?@param?text
*/
private?void?handleOperator(String?text)?{
switch?(operator)?{?//?處理各項運(yùn)算??適用于JDK1.7版本的
case?"+":
resultNum?+=?Double.parseDouble(this.result.getText());
break;
case?"-":
resultNum?-=?Double.parseDouble(this.result.getText());
break;
case?"*":
resultNum?*=?Double.parseDouble(this.result.getText());
break;
case?"/":
resultNum?/=?Double.parseDouble(this.result.getText());
break;
case?"=":
resultNum?=?Double.parseDouble(this.result.getText());
break;
}
//?將文本框的值修改為運(yùn)算結(jié)果
this.result.setText(String.valueOf(resultNum));
//?將點擊的運(yùn)算符放入operator保存
operator?=?text;
//?下一個數(shù)字第一次點擊
isFirstDigit?=?true;
}
public?static?void?main(String[]?args)?{
?new?Calculator("My?Calculator");
?}
}
運(yùn)行結(jié)果如下:
界面漂亮堪比系統(tǒng)自帶計算器,功能完美加減乘除開平方等等全部具備,還有清零按鈕,小數(shù)點的使用,連加連乘功能完全參考系統(tǒng)官方計算器經(jīng)過長期調(diào)試改進(jìn)而成,馬上拷貝代碼拿去試試看吧,絕不后悔!
代碼如下:
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;i20;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;i16;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);
}
}
------------------------------------------------------------
才子_輝祝您愉快!
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; //導(dǎo)包 public class Jisuanqi extends JFrame implements ActionListener { //繼承JFrame 實現(xiàn)事件監(jiān)聽 private JTextField reasult; private JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnAC, btnAdd, btnSub, btnReasult, btnD, btnAbout, btnCancel; private boolean add, sub, end, s, c; private String str; private double num1, num2; public Jisuanqi() { //構(gòu)造屬性 JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); TitledBorder tb = new TitledBorder("輸出"); tb.setTitleColor(Color.BLUE); //標(biāo)題邊框底端線 設(shè)置顏色 btnAbout = new JButton(" 關(guān)于 "); btnCancel = new JButton("Cancel"); //兩個按鈕 btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { System.exit(0); } }); //給Cancel添加事件監(jiān)聽 當(dāng)鼠標(biāo)點擊時 程序結(jié)束 btnAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { JOptionPane.showMessageDialog(null, "黃蓋?。?, "消息", JOptionPane.INFORMATION_MESSAGE); } //給“關(guān)于”添加事件監(jiān)聽 當(dāng)鼠標(biāo)點擊時 彈出對話框 顯示黃蓋 }); p3.add(btnAbout); p3.add(btnCancel); // JPanel p4=new JPanel(); // JPanel p5=new JPanel(); // reasult.setBorder(tb); reasult = new JTextField("0", 20); reasult.setEditable(false); //設(shè)置不能修改 reasult.setHorizontalAlignment(JTextField.RIGHT); // 設(shè)置文本的水平對齊方式。 reasult.setForeground(Color.BLUE); //顏色 p1.setBorder(tb); p1.add(reasult); btn0 = new JButton("0"); btn0.addActionListener(this); btn1 = new JButton("1"); btn1.addActionListener(this); btn2 = new JButton("2"); btn2.addActionListener(this); btn3 = new JButton("3"); btn3.addActionListener(this); btn4 = new JButton("4"); btn4.addActionListener(this); btn5 = new JButton("5"); btn5.addActionListener(this); btn6 = new JButton("6"); btn6.addActionListener(this); btn7 = new JButton("7"); btn7.addActionListener(this); btn8 = new JButton("8"); btn8.addActionListener(this); btn9 = new JButton("9"); btn9.addActionListener(this); btnD = new JButton("."); btnD.addActionListener(this); btnD.setForeground(Color.RED); btnAC = new JButton("AC"); btnAC.addActionListener(this); btnAC.setBackground(Color.PINK); btnAdd = new JButton("+"); btnAdd.addActionListener(this); btnAdd.setForeground(Color.BLUE); btnSub = new JButton("—"); btnSub.addActionListener(this); btnSub.setForeground(Color.BLUE); btnReasult = new JButton("="); btnReasult.addActionListener(this); btnReasult.setForeground(Color.RED); //事件監(jiān)聽 + 顏色 p2.add(btn1); p2.add(btn2); p2.add(btn3); p2.add(btn4); p2.add(btn5); p2.add(btn6); p2.add(btn7); p2.add(btn8); p2.add(btn9); p2.add(btn0); p2.add(btnD); p2.add(btnAC); p2.add(btnAdd); p2.add(btnSub); p2.add(btnReasult); //面板上添加按鈕 p2.setLayout(new GridLayout(5, 3)); //面板上設(shè)置對齊方式 add(p1, BorderLayout.NORTH); add(p2, BorderLayout.CENTER); add(p3, BorderLayout.SOUTH); //將p1 p2 p3 面板對象添加到JFrame } public void num(int i) { String s = null; s = String.valueOf(i); if (end) { // 如果數(shù)字輸入結(jié)束,則將文本框置零,重新輸入 reasult.setText("0"); end = false; } if ((reasult.getText()).equals("0")) { // 如果文本框的內(nèi)容為零,則覆蓋文本框的內(nèi)容 reasult.setText(s); } else { // 如果文本框的內(nèi)容不為零,則在內(nèi)容后面添加數(shù)字 str = reasult.getText() + s; reasult.setText(str); } }/* * * String s=null; * * s=String.valueOf(i); * * str=reasult.getText()+s; * * reasult.setText(str); * * } */ public void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) num(1); else if (e.getSource() == btn2) num(2); else if (e.getSource() == btn3) num(3); else if (e.getSource() == btn4) num(4); else if (e.getSource() == btn5) num(5); else if (e.getSource() == btn6) num(6); else if (e.getSource() == btn7) num(7); else if (e.getSource() == btn8) num(8); else if (e.getSource() == btn9) num(9); else if (e.getSource() == btn0) num(0); else if (e.getSource() == btnAdd) { sign(1); btnD.setEnabled(true); } else if (e.getSource() == btnSub) { sign(2); btnD.setEnabled(true); } else if (e.getSource() == btnAC) { btnD.setEnabled(true); reasult.setText("0"); } else if (e.getSource() == btnD) { str = reasult.getText(); str += "."; reasult.setText(str); btnD.setEnabled(false); } else if (e.getSource() == btnReasult) { btnD.setEnabled(true); num2 = Double.parseDouble(reasult.getText()); if (add) { num1 = num1 + num2; } else if (sub) { num1 = num1 - num2; } reasult.setText(String.valueOf(num1)); end = true; } } public void sign(int s) { if (s == 1) { add = true; sub = false; } else if (s == 2) { add = false; sub = true; } num1 = Double.parseDouble(reasult.getText()); end = true; } //設(shè)計計算的過程 public static void main(String[] args) { Jisuanqi j = new Jisuanqi(); j.setTitle("+/-簡易計算器"); j.setLocation(500, 280); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默認(rèn)關(guān)閉可以關(guān)閉程序 j.setResizable(false); j.pack(); j.setVisible(true); } } 這個計算機(jī),絕對讓你滿意
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Counter extends WindowAdapter
{
static JFrame f=new JFrame("計算器");
static JTextField text1=new JTextField("0.");
static String source="";
static String cal="";
static String object="";
static boolean flag=false;
static boolean flag1=true;
static boolean flag2=false;
public void init()
{
try
{
Container c=f.getContentPane();
JPanel pan1=new JPanel();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JButton b11=new JButton("+");
JButton b12=new JButton("-");
JButton b13=new JButton("*");
JButton b14=new JButton("/");
JButton b15=new JButton(".");
JButton b16=new JButton("=");
JButton bclar=new JButton("清零");
text1.setHorizontalAlignment(JTextField.RIGHT);
c.add(text1,"North");
c.add(pan1);
A aa=new A();
Result re=new Result();
Opertion op=new Opertion();
Clar cl=new Clar();
b1.addActionListener(aa);
b2.addActionListener(aa);
b3.addActionListener(aa);
b4.addActionListener(aa);
b5.addActionListener(aa);
b6.addActionListener(aa);
b7.addActionListener(aa);
b8.addActionListener(aa);
b9.addActionListener(aa);
b0.addActionListener(aa);
b11.addActionListener(op);
b12.addActionListener(op);
b13.addActionListener(op);
b14.addActionListener(op);
b16.addActionListener(re);
b15.addActionListener(aa);
bclar.addActionListener(cl);
pan1.add(b1);
pan1.add(b2);
pan1.add(b3);
pan1.add(b11);
pan1.add(b4);
pan1.add(b5);
pan1.add(b6);
pan1.add(b12);
pan1.add(b7);
pan1.add(b8);
pan1.add(b9);
pan1.add(b13);
pan1.add(b0);
pan1.add(b15);
pan1.add(b16);
pan1.add(b14);
pan1.add(bclar);
f.setSize(200,220);
f.setVisible(true);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
class A implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String a=text1.getText();
String s=e.getActionCommand();
if(a.equals("0.")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/"))
text1.setText(s);
else {
if(flag2)
{
text1.setText(s);
flag2=false;
}
else
text1.setText(a+s);
}
}
}
class Opertion implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
cal=e.getActionCommand();
if(flag1==true)
source=text1.getText();
text1.setText(cal);
flag1=false;
flag=true;
}
}
class Result implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double num1;
num1=Double.parseDouble(source);
object=text1.getText();
double num2;
num2=Double.parseDouble(object);
double result=0;
if(cal.equals("+"))
result=num1+num2;
if(cal.equals("-"))
result=num1-num2;
if(cal.equals("*"))
result=num1*num2;
if(cal.equals("/"))
if(num2==0)
text1.setText("除數(shù)不能為0");
else
result=num1/num2;
String s1=Double.toString(result);
text1.setText(s1);
flag1=true;
flag2=true;
}
}
class Clar implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
text1.setText("0.");
}
}
public static void main(String[] args)
{
Counter count=new Counter();
count.init();
}
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
分享題目:計算器的java代碼 計算器java代碼加法部分
網(wǎng)頁網(wǎng)址:http://jinyejixie.com/article42/dodoehc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、微信小程序、響應(yīng)式網(wǎng)站、企業(yè)網(wǎng)站制作、營銷型網(wǎng)站建設(shè)、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)