主要涉及的知識(shí)點(diǎn): 類的寫(xiě)法, 以及方法的調(diào)用 .建議多做練習(xí). 如果有看不懂的地方. 可以繼續(xù)追問(wèn),一起討論.
成都創(chuàng)新互聯(lián)公司是專業(yè)的汝南網(wǎng)站建設(shè)公司,汝南接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行汝南網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
參考代碼如下
//Number類
class?Number?{
private?int?n1;//私有的整型數(shù)據(jù)成員n1
private?int?n2;//私有的整型數(shù)據(jù)成員n2
//?通過(guò)構(gòu)造函數(shù)給n1和n2賦值
public?Number(int?n1,?int?n2)?{
this.n1?=?n1;
this.n2?=?n2;
}
//?加法
public?int?addition()?{
return?n1?+?n2;
}
//?減法
public?int?subtration()?{
return?n1?-?n2;
}
//?乘法
public?int?multiplication()?{
return?n1?*?n2;
}
//?除法?(可能除不盡,所以使用double作為返回類型)
public?double?division()?{
return?n1?*?1.0?/?n2;?//?通過(guò)n1*1.0?把計(jì)算結(jié)果轉(zhuǎn)換成double類型.
}
}
//Exam4?類
public?class?Exam4{
public?static?void?main(String[]?args)?{
Number?number=new?Number(15,?6);//創(chuàng)建Number類的對(duì)象
//下面的是調(diào)用方法得到返回值進(jìn)行輸出顯示
System.out.println("加法"+number.addition());
System.out.println("減法"+number.subtration());
System.out.println("乘法"+number.multiplication());
System.out.println("除法"+number.division());
}
}
具體代碼如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator ?extends JFrame implements ActionListener ?{
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//對(duì)圖形組件實(shí)例化
jf=new JFrame("任靜的計(jì)算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件監(jiān)聽(tīng)
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件處理
public void actionPerformed(ActionEvent e) {
//在這里完成事件處理 ?使計(jì)算器可以運(yùn)行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋體",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
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.ImageIcon;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
import?javax.swing.SwingConstants;
public?class?Jisuanqi?extends?JFrame?implements?ActionListener?{
/**
?*
?*/
private?static?final?long?serialVersionUID?=?1L;
Result?result?=?new?Result();?//?定義text的面板
Number_Key?number_key?=?new?Number_Key();?//?定義按鈕面板
//?當(dāng)點(diǎn)擊按鈕+、-、*、/時(shí),com?=?true
boolean?com?=?false;
//?當(dāng)i=0時(shí)說(shuō)明是我們第一次輸入,字符串text不會(huì)累加
int?i?=?0;
//?存放text的內(nèi)容
String?text?=?"";
//?存放點(diǎn)擊按鈕+、-、*、/之前的數(shù)值
double?defbutton?=?0;
//?+、-、*、/的代號(hào)分別為1,2,3,4
int?symbol?=?0;
//?構(gòu)造函數(shù)
Jisuanqi()?{
super("WangJiao");?//?設(shè)定標(biāo)題
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?//?設(shè)定關(guān)閉窗體時(shí)退出程序
JPanel?pane?=?new?JPanel();?//?定義主面板
pane.setLayout(new?BorderLayout());
setBounds(380,?220,?30,?80);?//?前兩個(gè)參數(shù)是在屏幕上顯示的坐標(biāo),后兩個(gè)是大小
//?替換圖標(biāo)
ImageIcon?icon?=?new?ImageIcon("F:1.GIF");
//?Jisuanqi.class.getResource("APPLE.GIF")
//?);
setIconImage(icon.getImage());
pane.add(result,?BorderLayout.NORTH);
pane.add(number_key,?BorderLayout.CENTER);
pane.add(number_key.equal,?BorderLayout.SOUTH);
number_key.one.addActionListener(this);?//?對(duì)1按鈕添加監(jiān)聽(tīng)事件
number_key.two.addActionListener(this);?//?對(duì)2按鈕添加監(jiān)聽(tīng)事件
number_key.three.addActionListener(this);?//?對(duì)3按鈕添加監(jiān)聽(tīng)事件
number_key.four.addActionListener(this);?//?對(duì)4按鈕添加監(jiān)聽(tīng)事件
number_key.five.addActionListener(this);?//?對(duì)5按鈕添加監(jiān)聽(tīng)事件
number_key.six.addActionListener(this);?//?對(duì)6按鈕添加監(jiān)聽(tīng)事件
number_key.seven.addActionListener(this);?//?對(duì)7按鈕添加監(jiān)聽(tīng)事件
number_key.eight.addActionListener(this);?//?對(duì)8按鈕添加監(jiān)聽(tīng)事件
number_key.nine.addActionListener(this);?//?對(duì)9按鈕添加監(jiān)聽(tīng)事件
number_key.zero.addActionListener(this);?//?對(duì)0按鈕添加監(jiān)聽(tīng)事件
number_key.ce.addActionListener(this);?//?對(duì)置零按鈕添加監(jiān)聽(tīng)事件
number_key.plus.addActionListener(this);?//?對(duì)+按鈕添加監(jiān)聽(tīng)事件
number_key.equal.addActionListener(this);?//?對(duì)=按鈕添加監(jiān)聽(tīng)事件
number_key.sub.addActionListener(this);?//?對(duì)-按鈕添加監(jiān)聽(tīng)事件
number_key.mul.addActionListener(this);?//?對(duì)*按鈕添加監(jiān)聽(tīng)事件
number_key.div.addActionListener(this);?//?對(duì)/按鈕添加監(jiān)聽(tīng)事件
number_key.point.addActionListener(this);?//?對(duì).按鈕添加監(jiān)聽(tīng)事件
setContentPane(pane);
pack();?//?初始化窗體大小為正好盛放所有按鈕
}
//?各個(gè)按鈕觸發(fā)的事件
public?void?actionPerformed(ActionEvent?e)?{
/*
?*?如果是點(diǎn)擊數(shù)字按鈕那么先要判斷是否在此之前點(diǎn)擊了+、-、*、/、=,如果是那么com=true?如果沒(méi)有com=
?*?false;或者是否點(diǎn)擊數(shù)字鍵,如果是i?=?1,如果沒(méi)有?i?=?0;
?*/
if?(e.getSource()?==?number_key.one)?{
if?(com?||?i?==?0)?{
result.text.setText("1");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
result.text.setText(text?+?"1");
}
}?else?if?(e.getSource()?==?number_key.two)?{
if?(com?||?i?==?0)?{
result.text.setText("2");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
result.text.setText(text?+?"2");
}
}?else?if?(e.getSource()?==?number_key.three)?{
if?(com?||?i?==?0)?{
result.text.setText("3");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
result.text.setText(text?+?"3");
}
}?else?if?(e.getSource()?==?number_key.four)?{
if?(com?||?i?==?0)?{
result.text.setText("4");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
result.text.setText(text?+?"4");
}
}?else?if?(e.getSource()?==?number_key.five)?{
if?(com?||?i?==?0)?{
result.text.setText("5");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
result.text.setText(text?+?"5");
}
}?else?if?(e.getSource()?==?number_key.six)?{
if?(com?||?i?==?0)?{
result.text.setText("6");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
result.text.setText(text?+?"6");
}
}?else?if?(e.getSource()?==?number_key.seven)?{
if?(com?||?i?==?0)?{
result.text.setText("7");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
result.text.setText(text?+?"7");
}
}?else?if?(e.getSource()?==?number_key.eight)?{
if?(com?||?i?==?0)?{
result.text.setText("8");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
result.text.setText(text?+?"8");
}
}?else?if?(e.getSource()?==?number_key.nine)?{
if?(com?||?i?==?0)?{
result.text.setText("9");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
result.text.setText(text?+?"9");
}
}
/*
?*?對(duì)于0這個(gè)按鈕有一定的說(shuō)法,在我的程序里不會(huì)出現(xiàn)如00000這樣的情況,我加了判斷條件就是
?*?如果text中的數(shù)值=0就要判斷在這個(gè)數(shù)值中是否有.存在?如果有那么就在原來(lái)數(shù)值基礎(chǔ)之上添?加0;否則保持原來(lái)的數(shù)值不變
?*/
else?if?(e.getSource()?==?number_key.zero)?{?//?result.text.getText()是得到text里內(nèi)容的意思
if?(com?||?i?==?0)?{
result.text.setText("0");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
if?(Float.parseFloat(text)??0?||?Float.parseFloat(text)??0)?{?//?Float.parseFloat(text)就是類型轉(zhuǎn)換了,下面都是一樣
result.text.setText(text?+?"0");
}?else?{
if?(text.trim().indexOf(".")?==?-1)?{
result.text.setText(text);
}?else?{
result.text.setText(text?+?"0");
}
}
}
}?else?if?(e.getSource()?==?number_key.ce)?{
result.text.setText("0");
i?=?0;
com?=?true;
//?text?=?"";
defbutton?=?0;
}
/*
?*?本程序不會(huì)讓一個(gè)數(shù)值中出現(xiàn)2個(gè)以上的小數(shù)點(diǎn).具體做法是:判斷是否已經(jīng)存在.存在就不添加,?不存在就添加.
?*/
else?if?(e.getSource()?==?number_key.point)?{
if?(com?||?i?==?0)?{
result.text.setText("0.");
com?=?false;
i?=?1;
}?else?{
text?=?result.text.getText();
if?(text.trim().indexOf(".")?==?-1)?{
result.text.setText(text?+?".");
}?else?{
result.text.setText(text);
}
}
}?//?獲得點(diǎn)擊+之前的數(shù)值
else?if?(e.getSource()?==?number_key.plus)?{
com?=?true;
i?=?0;
defbutton?=?Double.parseDouble(result.text.getText());
symbol?=?1;
}?//?獲得點(diǎn)擊-之前的數(shù)值
else?if?(e.getSource()?==?number_key.sub)?{
com?=?true;
i?=?0;
defbutton?=?Double.parseDouble(result.text.getText());
symbol?=?2;
}?//?獲得點(diǎn)擊*之前的數(shù)值
else?if?(e.getSource()?==?number_key.mul)?{
com?=?true;
i?=?0;
defbutton?=?Double.parseDouble(result.text.getText());
System.out.println(defbutton);
symbol?=?3;
}?//?獲得點(diǎn)擊/之前的數(shù)值
else?if?(e.getSource()?==?number_key.div)?{
com?=?true;
i?=?0;
defbutton?=?Double.parseDouble(result.text.getText());
symbol?=?4;
}?else?if?(e.getSource()?==?number_key.equal)?{
switch?(symbol)?{
case?1:?{?//?計(jì)算加法
double?ad?=?defbutton
+?Double.parseDouble(result.text.getText());
result.text.setText(ad?+?"");
i?=?0;
text?=?"";
break;
}
case?2:?{?//?計(jì)算減法
double?ad?=?defbutton
-?Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i?=?0;
text?=?"";
break;
}
case?3:?{?//?計(jì)算乘法
double?ad?=?defbutton
*?Double.parseDouble(result.text.getText());
result.text.setText(ad?+?"");
i?=?0;
text?=?"";
break;
}
case?4:?{?//?計(jì)算除法
double?ad?=?defbutton
/?Double.parseDouble(result.text.getText());
result.text.setText(ad?+?"");
i?=?0;
text?=?"";
break;
}
}
System.out.println(com);
}
System.out.println(result.text.getText());
}
@SuppressWarnings("deprecation")
public?static?void?main(String[]?args)?{
Jisuanqi?loveyou?=?new?Jisuanqi();
loveyou.show();
}
}
//?計(jì)算器數(shù)字按鈕定義面板
class?Number_Key?extends?JPanel?{
/**
?*
?*/
private?static?final?long?serialVersionUID?=?1L;
JButton?zero?=?new?JButton("0");?//?數(shù)字鍵0
JButton?one?=?new?JButton("1");?//?數(shù)字鍵1
JButton?two?=?new?JButton("2");?//?數(shù)字鍵2
JButton?three?=?new?JButton("3");?//?數(shù)字鍵3
JButton?four?=?new?JButton("4");?//?數(shù)字鍵4
JButton?five?=?new?JButton("5");?//?數(shù)字鍵5
JButton?six?=?new?JButton("6");?//?數(shù)字鍵6
JButton?seven?=?new?JButton("7");?//?數(shù)字鍵7
JButton?eight?=?new?JButton("8");?//?數(shù)字鍵8
JButton?nine?=?new?JButton("9");?//?數(shù)字鍵9
JButton?plus?=?new?JButton("+");
JButton?sub?=?new?JButton("-");
JButton?mul?=?new?JButton("*");
JButton?div?=?new?JButton("/");
JButton?equal?=?new?JButton("=");
JButton?ce?=?new?JButton("清零");?//?置零鍵
JButton?point?=?new?JButton(".");
Number_Key()?{
setLayout(new?GridLayout(4,?4,?1,?1));?//?定義布局管理器為網(wǎng)格布局
setBackground(Color.blue);?//?設(shè)置背景顏色
//?添加按鈕
add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);
add(zero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
}
}
//?計(jì)算器顯示結(jié)果的窗體
class?Result?extends?JPanel?{
/**
?*
?*/
private?static?final?long?serialVersionUID?=?1L;
//?text先是輸入和結(jié)果
JTextField?text?=?new?JTextField("0");
@SuppressWarnings("deprecation")
Result()?{?//?講輸入的數(shù)字或得到的結(jié)果在text的右邊顯示
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.enable(false);?//?文本框不能編輯
setLayout(new?BorderLayout());?//?設(shè)定布局管理器邊框布局
add(text,?BorderLayout.CENTER);?//?text放置在窗體的中間
}
}
直接復(fù)制?保存成Jisuanqi?.java可以直接運(yùn)行了
當(dāng)前標(biāo)題:java代碼寫(xiě)計(jì)算器 java代碼寫(xiě)計(jì)算器論文概述
路徑分享:http://jinyejixie.com/article12/doscogc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站收錄、搜索引擎優(yōu)化、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)