其實(shí)你這種學(xué)習(xí)編程的方法我是極度不推薦的,正確的方法應(yīng)該從基礎(chǔ)開始,你這個程序是用圖形用戶界面來寫的,入門的話最好還是控制臺程序,這樣你可以了解整個程序的流程和語法,如果你沒有C語言的基礎(chǔ),請找一本講解C語言的書學(xué)習(xí),然后學(xué)習(xí)好了你再來看JAVA感覺就很簡單了。(絕對原創(chuàng),請求給分)
公司主營業(yè)務(wù):成都網(wǎng)站制作、網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出普定免費(fèi)做網(wǎng)站回饋大家。
import java.awt.*;
import java.awt.event.*; //導(dǎo)入包,因為后面要用到里面的東西
public class jisuanqi extends WindowAdapter {
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel(); //構(gòu)造3個panel對象,這個是個容器,里面可以添加別的組件
TextField txt; // TextField 組件,相當(dāng)于一個文本區(qū)域
private Button[] b = new Button[17]; //看名字就知道是按鈕了吧
private String ss[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2",
"3", "*", "清空", "0", "=", "/", "關(guān)閉" };
static int a;
static String s, str;
//入口函數(shù),相當(dāng)于C里面的MAIN函數(shù)
public static void main(String args[]) {
(new jisuanqi()).frame(); //調(diào)用函數(shù),java里叫方法
}
//將窗口實(shí)例化,frame是一個窗口,awt包里的。算法方面自己看看就能明白
public void frame() {
Frame fm = new Frame("簡單計算器");
for (int i = 0; i = 16; i++) {
b[i] = new Button(ss[i]);
}
for (int i = 0; i = 15; i++) {
p2.add(b[i]);
}
fm.setBounds(300,200,200,200);
b[16].setBackground(Color.yellow);
txt = new TextField(15);
txt.setEditable(false);
for (int i = 0; i = 16; i++) {
b[i].addActionListener(new buttonlistener());
}
b[16].addActionListener(new close());
fm.addWindowListener(this);
fm.setBackground(Color.red);
p1.setLayout(new BorderLayout());
p1.add(txt, "Center");
p2.setLayout(new GridLayout(4, 4));
p3.setLayout(new BorderLayout());
p3.add(b[16]);
fm.add(p1, "North");
fm.add(p2, "Center");
fm.add(p3, "South");
fm.pack();
fm.setVisible(true);
}
//關(guān)閉窗口的方法
public void windowClosing(WindowEvent e) {
System.exit(0);
}
//用這種方法在一個類里面定義另一個類是不好的,應(yīng)該單獨(dú)用以個文件承載,這個類是實(shí)現(xiàn)了ActionListener接口,用于監(jiān)聽按鈕事件。正規(guī)的寫法應(yīng)該是在主類里面實(shí)現(xiàn)這個接口,然后寫一個方法來調(diào)用。
class buttonlistener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button btn = (Button) e.getSource();
if (btn.getLabel() == "=") {
jisuan();
str = String.valueOf(a);
txt.setText(str);
s = "";
} else if (btn.getLabel() == "+") {
jisuan();
txt.setText("");
s = "+";
} else if (btn.getLabel() == "-") {
jisuan();
txt.setText("");
s = "-";
} else if (btn.getLabel() == "/") {
jisuan();
txt.setText("");
s = "/";
} else if (btn.getLabel() == "*") {
jisuan();
txt.setText("");
s = "*";
} else {
txt.setText(txt.getText() + btn.getLabel());
if (btn.getLabel() == "清空")
txt.setText("");
}
}
public void jisuan() {
if (s == "+")
a += Integer.parseInt(txt.getText());
else if (s == "-")
a -= Integer.parseInt(txt.getText());
else if (s == "*")
a *= Integer.parseInt(txt.getText());
else if (s == "/")
a /= Integer.parseInt(txt.getText());
else
a = Integer.parseInt(txt.getText());
}
}
}
//關(guān)閉按鈕事件
class close implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
//引入各種類文件
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;??//定義兩個double類型的變量
int?op?=?-1;??//定義兩個int類型的變量
static?TextField?tf;//定義靜態(tài)對象TextField
CalPanelL?p1;??//定義CalPanelL對象
//?Constructor構(gòu)造方法
JsqFrame()?{
//以下設(shè)置屬性
super("計算器");
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類
class?CalButton?extends?Button?{
CalButton(String?s)?{
//設(shè)置按鈕屬性
super(s);
setBackground(Color.WHITE);?//設(shè)置顏色為白色
}
}
//定義顯示器繼承Panel類
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ī)按鍵的時出發(fā)的時間,設(shè)置按鍵的監(jiān)聽[加號的監(jiān)聽事件]
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ī)按鍵的時出發(fā)的時間,設(shè)置按鍵的監(jiān)聽[減號的監(jiān)聽事件]
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ī)按鍵的時出發(fā)的時間,設(shè)置按鍵的監(jiān)聽[乘號的監(jiān)聽事件]
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ī)按鍵的時出發(fā)的時間,設(shè)置按鍵的監(jiān)聽[除號的監(jiān)聽事件]
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ī)按鍵的時出發(fā)的時間,設(shè)置按鍵的監(jiān)聽[向左鍵的監(jiān)聽事件]
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ī)按鍵的時出發(fā)的時間,設(shè)置按鍵的監(jiān)聽[向右鍵的監(jiān)聽事件]
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ī)按鍵的時出發(fā)的時間,設(shè)置按鍵的監(jiān)聽[等號的監(jiān)聽事件]
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)?{
}
}
}
/*
*?批量寫吧
*?以下是按1、2、3等等的監(jiān)聽事件
*?還有清零的等等監(jiān)聽事件
*/
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);
}
}
java中字符串的相等,請不要用==號,應(yīng)該改成s.equals("==")
else
if(s=="退格"){
a=a/10;
}
這個方法不太好,比如我輸入1,再退格,應(yīng)該等于0.1嗎?
我給你找找
package com.bj.calcultor;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calcultor extends Frame implements ActionListener {
public static void main(String[] args) {//定義主方
new Calcultor();//創(chuàng)建匿名對象,并調(diào)用test()方法;
}
//定義按鈕名稱
String[] arr={"1","2","3","4","5","6","7","8","9","0","+","-","*","/","=","."};
JButton [] button=new JButton[arr.length];
JButton reset = new JButton("CE");
JTextField display = new JTextField(20);
//創(chuàng)建窗口,定義組件
//執(zhí)行窗口事件:關(guān)閉窗口
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public Calcultor(){
super("計算器");//定義標(biāo)題
//定義面板容器,并布局
JPanel jpanel=new JPanel(new GridLayout(4,4));
//添加按鈕,并給按鈕添加名稱
for (int i = 0; i arr.length; i++) {
button[i]= new JButton(arr[i]);
jpanel.add(button[i]);
}
JPanel panel2 = new JPanel();
panel2.add("Northr", display);
display.setEnabled(false);
panel2.add("East", reset);
this.add("North", panel2);
this.add("Center", jpanel);
for (int i = 0; i arr.length; i++){
addWindowListener(new WindowCloser());
setVisible(true);
setSize(400,400);
pack();
button[i].addActionListener(this);
reset.addActionListener(this);
display.addActionListener(this);
}
}
@Override
public void actionPerformed(ActionEvent e) {//定義事件
// TODO Auto-generated method stub
Object target=e.getSource();
String lable=e.getActionCommand();
if(target==reset){
handleReset();
}else if("0123456789.".indexOf(lable)0){
handleNumber(lable);
}else{
hadleOperator(lable);
}
}
boolean isFirstDigit=true;
private void hadleOperator(String key) {
if(operator.equals("+")){
number += Double.valueOf(display.getText());
}else if (operator.equals("-")){
number -= Double.valueOf(display.getText());
}else if (operator.equals("*")){
number *= Double.valueOf(display.getText());
}else if (operator.equals("/")){
number /= Double.valueOf(display.getText());
}else if(operator.equals("=")){
number =Double.valueOf(display.getText());
}
display.setText(String.valueOf(number));
operator=key;
isFirstDigit=true;
}
private void handleNumber(String key) {
if(true){
display.setText(key);//把鍵值設(shè)置為文本框內(nèi)容
}else if(key.equals(".") display.getText().indexOf(".")0){
display.setText(display.getText()+".");//把文本框內(nèi)容設(shè)置:display.getText()+"."
}else if(!key.equals(".")){
display.setText(display.getText() + key);//把文本框內(nèi)容設(shè)置:display.getText()+key
isFirstDigit=false;
}
}
private void handleReset() {
display.setText("0");
isFirstDigit=true;
operator="=";
}
String operator="=";
Double number=0.0;
}
當(dāng)前文章:java計算器代碼講解 java簡單計算器代碼
文章網(wǎng)址:http://muchs.cn/article8/hggiip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、用戶體驗、Google、ChatGPT、網(wǎng)站制作、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)