1、首先準(zhǔn)備好軟件即eclipse和java,下載安裝完成后打開(kāi)eclipse。

成都創(chuàng)新互聯(lián)專(zhuān)注于文山州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供文山州營(yíng)銷(xiāo)型網(wǎng)站建設(shè),文山州網(wǎng)站制作、文山州網(wǎng)頁(yè)設(shè)計(jì)、文山州網(wǎng)站官網(wǎng)定制、微信小程序開(kāi)發(fā)服務(wù),打造文山州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供文山州網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
2、點(diǎn)擊左上角的file 新建一個(gè)project。
3、給project取一個(gè)名字,其他的選項(xiàng)都是默認(rèn)然后點(diǎn)擊finish。
4、接下來(lái)是新建一個(gè)class。
5、在給class取名字的時(shí)候注意用英文名的首字母要大寫(xiě)。完成后點(diǎn)擊finish。
6、這就是開(kāi)始寫(xiě)代碼的工作臺(tái),將代碼寫(xiě)在綠字下。
7、這就是第一個(gè)hello world程序。
//計(jì)算器
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class M10 extends WindowAdapter implements MouseListener {
private String first;
private String second;
private String operator;
private Button zero = new Button("0");
private Button one = new Button("1");
private Button two = new Button("2");
private Button three = new Button("3");
private Button four = new Button("4");
private Button five = new Button("5");
private Button six = new Button("6");
private Button seven = new Button("7");
private Button eight = new Button("8");
private Button nine = new Button("9");
private Button decimal = new Button(".");
private Button equal = new Button("=");
private Button add = new Button("+");
private Button sub = new Button("-");
private Button mul = new Button("*");
private Button div = new Button("/");
private TextField input = new TextField();
private Button CE = new Button("CE");
private Button DEL = new Button("Del");
public static void main(String[] args){
new M10();
}
public M10(){
Frame f = new Frame("Calculator");
f.add("North", input);
Panel keys = new Panel();
f.add(keys, "Center");
keys.setLayout(new GridLayout(4, 4));
keys.add(seven);
keys.add(eight);
keys.add(nine);
keys.add(add);
keys.add(four);
keys.add(five);
keys.add(six);
keys.add(sub);
keys.add(one);
keys.add(two);
keys.add(three);
keys.add(mul);
keys.add(zero);
keys.add(decimal);
keys.add(equal);
keys.add(div);
zero.addMouseListener(this);
one.addMouseListener(this);
two.addMouseListener(this);
three.addMouseListener(this);
four.addMouseListener(this);
five.addMouseListener(this);
six.addMouseListener(this);
seven.addMouseListener(this);
eight.addMouseListener(this);
nine.addMouseListener(this);
add.addMouseListener(this);
sub.addMouseListener(this);
div.addMouseListener(this);
mul.addMouseListener(this);
equal.addMouseListener(this);
decimal.addMouseListener(this);
Panel addtionalPanel = new Panel();
addtionalPanel.setLayout(new GridLayout(1, 2));
addtionalPanel.add(CE);
addtionalPanel.add(DEL);
CE.setBackground(Color.green);
DEL.setBackground(Color.cyan);
CE.addMouseListener(this);
DEL.addMouseListener(this);
f.add("South", addtionalPanel);
f.addWindowListener(this);
f.setVisible(true);
f.setLocation(200, 300);
f.setSize(200, 200);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void mouseClicked(MouseEvent e){
Button btn = (Button)e.getSource();
String key = btn.getActionCommand().trim();
if(btn == one || btn == two || btn == three || btn == zero || btn == four || btn == five || btn == six || btn == seven || btn == eight || btn == nine){
if(isBlank(operator)){
if(isBlank(first)){
first = key;
}
else{
first += key;
}
input.setText(first);
}
else{
if(isBlank(second)){
second = key;
}
else{
second += key;
}
input.setText(second);
}
}
else if(btn == decimal){
if(isBlank(operator)){
if(isBlank(first)){
first = "0.";
input.setText(first);
}
else{
if(first.indexOf(".") == -1){
first += ".";
input.setText(first);
}
}
}
else{
if(isBlank(second)){
second = "0.";
input.setText(second);
}
else{
if(second.indexOf(".") == -1){
second += ".";
input.setText(second);
}
}
}
}
else if(btn == equal){
if(!isBlank(second) !isBlank(first) !isBlank(operator)){
double result = 0.0D;
if(operator.equals("+")){
result = Double.parseDouble(first) + Double.parseDouble(second);
}
else if(operator.equals("-")){
result = Double.parseDouble(first) - Double.parseDouble(second);
}
else if(operator.equals("*")){
result = Double.parseDouble(first) * Double.parseDouble(second);
}
else if(operator.equals("/")){
result = Double.parseDouble(first) / Double.parseDouble(second);
}
int value = (int)Math.round(result);
if(value == result){
input.setText(String.valueOf(value));
}
else{
input.setText(String.valueOf(result));
}
first = String.valueOf(result);
second = null;
operator = null;
}
}
else if(btn == add || btn == sub || btn == div || btn == mul){
if(!isBlank(first)){
if(!isBlank(operator) !isBlank(second)){
if(operator.equals("+")){
first = String.valueOf(Double.parseDouble(first) + Double.parseDouble(second));
}
else if(operator.equals("-")){
first = String.valueOf(Double.parseDouble(first) - Double.parseDouble(second));
}
else if(operator.equals("*")){
first = String.valueOf(Double.parseDouble(first) * Double.parseDouble(second));
}
else if(operator.equals("/")){
first = String.valueOf(Double.parseDouble(first) / Double.parseDouble(second));
}
second = null;
}
operator = key;
}
}
else if(btn == CE){
first = null;
second = null;
operator = null;
input.setText(null);
}
else if(btn == DEL){
if(isBlank(operator)){
if(!isBlank(first)){
first = first.substring(0, first.length() -1);
input.setText(first);
}
}
else{
if(!isBlank(second)){
second = second.substring(0, second.length() -1);
input.setText(second);
}
}
}
}
private boolean isBlank(String str){
return str == null || str.trim().equals("");
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
//引入各種類(lèi)文件
import?java.awt.Button;
import?java.awt.Color;
import?java.awt.FlowLayout;
import?java.awt.Font;
import?java.awt.Frame;
import?java.awt.GridLayout;
import?java.awt.Panel;
import?java.awt.TextField;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
//定義JsqFrame繼承Frame
class?JsqFrame?extends?Frame?{
double?d1,?d2;??//定義兩個(gè)double類(lèi)型的變量
int?op?=?-1;??//定義兩個(gè)int類(lèi)型的變量
static?TextField?tf;//定義靜態(tài)對(duì)象TextField
CalPanelL?p1;??//定義CalPanelL對(duì)象
//?Constructor構(gòu)造方法
JsqFrame()?{
//以下設(shè)置屬性
super("計(jì)算器");
setLayout(new?FlowLayout());
setResizable(false);
setSize(250,?250);
tf?=?new?TextField(18);
tf.setEditable(false);
tf.setBackground(Color.lightGray);
tf.setForeground(Color.red);
tf.setFont(new?Font("Arial",?Font.BOLD,?16));
add(tf);
p1?=?new?CalPanelL();
add(p1);
setVisible(true);
//?addWindowListener(new?Wclose());
}
//添加按鈕繼承Button類(lèi)
class?CalButton?extends?Button?{
CalButton(String?s)?{
//設(shè)置按鈕屬性
super(s);
setBackground(Color.WHITE);?//設(shè)置顏色為白色
}
}
//定義顯示器繼承Panel類(lèi)
class?CalPanelL?extends?Panel?{
CalButton?a,?c,?b0,?b1,?b2,?b3,?b4,?b5,?b6,?b7,?b8,?b9,?bPN,?bPoint,
bAdd,?bSub,?bMul,?bDiv,?bL,?bR,?bLn,?bEqual,?bCE,?bQuit;
CalPanelL()?{
//設(shè)置顯示器的屬性
setLayout(new?GridLayout(6,?4));
setFont(new?Font("TimesRoman",?Font.BOLD,?16));
a?=?new?CalButton("");
c?=?new?CalButton("");
b0?=?new?CalButton("0");
b1?=?new?CalButton("1");
b2?=?new?CalButton("2");
b3?=?new?CalButton("3");
b4?=?new?CalButton("4");
b5?=?new?CalButton("5");
b6?=?new?CalButton("6");
b7?=?new?CalButton("7");
b8?=?new?CalButton("8");
b9?=?new?CalButton("9");
bPN?=?new?CalButton("+/-");
bPoint?=?new?CalButton(".");
//?設(shè)置按鈕
bAdd?=?new?CalButton("+");
bSub?=?new?CalButton("-");
bMul?=?new?CalButton("*");
bDiv?=?new?CalButton("/");
bL?=?new?CalButton("(");
bR?=?new?CalButton(")");
bLn?=?new?CalButton("ln");
bEqual?=?new?CalButton("=");
bCE?=?new?CalButton("CE");
bQuit?=?new?CalButton("退出");
//?加入按鈕
add(a);
add(c);
add(bCE);
bCE.addActionListener(new?PressBCE());
add(bQuit);
bQuit.addActionListener(new?PressBQuit());
add(b7);
b7.addActionListener(new?PressB7());
add(b8);
b8.addActionListener(new?PressB8());
add(b9);
b9.addActionListener(new?PressB9());
add(bDiv);
bDiv.addActionListener(new?PressBDiv());
add(b4);
b4.addActionListener(new?PressB4());
add(b5);
b5.addActionListener(new?PressB5());
add(b6);
b6.addActionListener(new?PressB6());
add(bMul);
bMul.addActionListener(new?PressBMul());
add(b1);
b1.addActionListener(new?PressB1());
add(b2);
b2.addActionListener(new?PressB2());
add(b3);
b3.addActionListener(new?PressB3());
add(bSub);
bSub.addActionListener(new?PressBSub());
add(b0);
b0.addActionListener(new?PressB0());
add(bPoint);
bPoint.addActionListener(new?PressBPoint());
add(bPN);
bPN.addActionListener(new?PressBPN());
;
add(bAdd);
bAdd.addActionListener(new?PressBAdd());
add(bL);
bL.addActionListener(new?PressBL());
add(bR);
bR.addActionListener(new?PressBR());
add(bLn);
bLn.addActionListener(new?PressBLn());
add(bEqual);
bEqual.addActionListener(new?PressBEqual());
}
}
//定義PressBAdd實(shí)現(xiàn)ActionListener
//大體的意思是按計(jì)算機(jī)按鍵的時(shí)出發(fā)的時(shí)間,設(shè)置按鍵的監(jiān)聽(tīng)[加號(hào)的監(jiān)聽(tīng)事件]
class?PressBAdd?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
try?{
String?d1?=?tf.getText();
op?=?0;
tf.setText(d1?+?"+");
}?catch?(Exception?ee)?{
}
}
}
//定義PressBSub實(shí)現(xiàn)ActionListener
//大體的意思是按計(jì)算機(jī)按鍵的時(shí)出發(fā)的時(shí)間,設(shè)置按鍵的監(jiān)聽(tīng)[減號(hào)的監(jiān)聽(tīng)事件]
class?PressBSub?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
try?{
String?d1?=?tf.getText();
op?=?1;
tf.setText(d1?+?"-");
}?catch?(Exception?ee)?{
}
}
}
//定義PressBMul實(shí)現(xiàn)ActionListener
//大體的意思是按計(jì)算機(jī)按鍵的時(shí)出發(fā)的時(shí)間,設(shè)置按鍵的監(jiān)聽(tīng)[乘號(hào)的監(jiān)聽(tīng)事件]
class?PressBMul?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
try?{
String?d1?=?tf.getText();
op?=?2;
tf.setText(d1?+?"*");
}?catch?(Exception?ee)?{
}
}
}
//定義PressBDiv實(shí)現(xiàn)ActionListener
//大體的意思是按計(jì)算機(jī)按鍵的時(shí)出發(fā)的時(shí)間,設(shè)置按鍵的監(jiān)聽(tīng)[除號(hào)的監(jiān)聽(tīng)事件]
class?PressBDiv?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
try?{
String?d1?=?tf.getText();
op?=?3;
tf.setText(d1?+?"/");
}?catch?(Exception?ee)?{
}
}
}
//定義PressBL實(shí)現(xiàn)ActionListener
//大體的意思是按計(jì)算機(jī)按鍵的時(shí)出發(fā)的時(shí)間,設(shè)置按鍵的監(jiān)聽(tīng)[向左鍵的監(jiān)聽(tīng)事件]
class?PressBL?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
try?{
String?d1?=?tf.getText();
op?=?3;
tf.setText(d1?+?"(");
}?catch?(Exception?ee)?{
}
}
}
//定義PressBR實(shí)現(xiàn)ActionListener
//大體的意思是按計(jì)算機(jī)按鍵的時(shí)出發(fā)的時(shí)間,設(shè)置按鍵的監(jiān)聽(tīng)[向右鍵的監(jiān)聽(tīng)事件]
class?PressBR?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
try?{
String?d1?=?tf.getText();
op?=?3;
tf.setText(d1?+?")");
}?catch?(Exception?ee)?{
}
}
}
//定義PressBEqual實(shí)現(xiàn)ActionListener
//大體的意思是按計(jì)算機(jī)按鍵的時(shí)出發(fā)的時(shí)間,設(shè)置按鍵的監(jiān)聽(tīng)[等號(hào)的監(jiān)聽(tīng)事件]
class?PressBEqual?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
try?{
Jsq?jsq?=?new?Jsq();
String?s?=?tf.getText();
System.out.println(s);
while?(s.indexOf("(")?+?1??0??s.indexOf(")")??0)?{
String?s1?=?jsq.caculateHigh(s);
System.out.println(s1);
s?=?s1;
}
String?str?=?jsq.cacluteMain(s);
System.out.println(str);
tf.setText(String.valueOf(str));
}?catch?(Exception?ee)?{
}
}
}
/*
*?批量寫(xiě)吧
*?以下是按1、2、3等等的監(jiān)聽(tīng)事件
*?還有清零的等等監(jiān)聽(tīng)事件
*/
class?PressBLn?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
try?{
double?x?=?Double.parseDouble(tf.getText());
double?y;
y?=?Math.log10(x);
tf.setText(y?+?"");
}?catch?(Exception?ee)?{
}
}
}
class?PressBCE?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
tf.setText("");
}
}
class?PressBPN?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
try?{
String?text?=?tf.getText();
if?(text?!=?"")?{
if?(text.charAt(0)?==?'-')
tf.setText(text.substring(1));
else?if?(text.charAt(0)?=?'0'??text.charAt(0)?=?'9')
tf.setText("-"?+?text.substring(0));
else?if?(text.charAt(0)?==?'.')
tf.setText("-0"?+?text.substring(0));
}
}?catch?(Exception?ee)?{
}
}
}
class?PressBPoint?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
if?(text.lastIndexOf(".")?==?-1)
tf.setText(text?+?".");
}
}
class?PressB0?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"0");
}
}
class?PressB1?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"1");
}
}
class?PressB2?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"2");
}
}
class?PressB3?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"3");
}
}
class?PressB4?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"4");
}
}
class?PressB5?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"5");
}
}
class?PressB6?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"6");
}
}
class?PressB7?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"7");
}
}
class?PressB8?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"8");
}
}
class?PressB9?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?tf.getText();
tf.setText(text?+?"9");
}
}
class?PressBQuit?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
System.exit(0);
}
}
public?void?Js()?{
String?text?=?tf.getText();
tf.setText(text);
}
}
本文標(biāo)題:java計(jì)算器代碼要注釋,java計(jì)算器代碼要注釋嘛
URL地址:http://chinadenli.net/article47/dsiiohj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、做網(wǎng)站、靜態(tài)網(wǎng)站、定制開(kāi)發(fā)、手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)