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); //標題邊框底端線 設(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)聽 當鼠標點擊時 程序結(jié)束 btnAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { JOptionPane.showMessageDialog(null, "黃蓋!!", "消息", JOptionPane.INFORMATION_MESSAGE); } //給“關(guān)于”添加事件監(jiān)聽 當鼠標點擊時 彈出對話框 顯示黃蓋 }); 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); //默認關(guān)閉可以關(guān)閉程序 j.setResizable(false); j.pack(); j.setVisible(true); } } 這個計算機,絕對讓你滿意
清水河網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司自2013年起到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
import java.util.Scanner;
public class JiSuan {
public static void main(String[] args) {
String zhanghao="765800331";//注意賬號密碼均為字符串類型
String mima="123456";
int isgoing=0;//當賬號或密碼輸入錯誤時提示是否繼續(xù)輸入
String select="";//六種運算中你的選擇
int result=0;//運算結(jié)果
int a=0;
int b=0;
Scanner sa = new Scanner(System.in);
do{
System.out.println("請輸入你的賬號:");
String zhanghao1= sa.next();
System.out.println("請輸入你的密碼:");
String mima1= sa.next();
if(zhanghao.equals(zhanghao1)mima.equals(mima1)){
while(zhanghao.equals(zhanghao1)mima.equals(mima1)){
System.out.println("*******************歡迎使用Java小計算器**************");
System.out.println("本計算器提供以下功能");
System.out.println("A.加法");
System.out.println("B.減法");
System.out.println("C.乘法");
System.out.println("D.除法");
System.out.println("E.a的b次冪");
System.out.println("F.退出計算器");
System.out.println("請選擇:");
select= sa.next();
char c=select.charAt(0);//申明一個變量c來接收select轉(zhuǎn)換成char類型的變量
switch(c){
case 'A':
System.out.println("請輸入第一個數(shù)");
a = sa.nextInt();
System.out.println("請輸入第二個數(shù)");
b = sa.nextInt();
result = a+b;
System.out.println("運算結(jié)果如下:"+result);
break;
case 'B':
System.out.println("請輸入第一個數(shù)");
a = sa.nextInt();
System.out.println("請輸入第二個數(shù)");
b = sa.nextInt();
result = a-b;
System.out.println("運算結(jié)果如下:"+result);
break;
case 'C':
System.out.println("請輸入第一個數(shù)");
a = sa.nextInt();
System.out.println("請輸入第二個數(shù)");
b = sa.nextInt();
result = a*b;
System.out.println("運算結(jié)果如下:"+result);
break;
case 'D':
System.out.println("請輸入第一個數(shù)");
a = sa.nextInt();
System.out.println("請輸入第二個數(shù)");
b = sa.nextInt();
result = a/b;
if(a%b!=0){
int m=a%b;
System.out.println("運算結(jié)果如下:"+result+" 余數(shù):"+m);
}else{
System.out.println("運算結(jié)果如下:"+result);
}
break;
case 'E':
System.out.println("請輸入第一個數(shù)");
a = sa.nextInt();
System.out.println("請輸入第二個數(shù)");
b = sa.nextInt();
int i=0;//指數(shù)的底數(shù)
int sum=1;//用來接收a的b次冪的結(jié)果
for(i=0;ib;i++){
sum=sum*a;
result=sum;
}
System.out.println("運算結(jié)果如下:"+result);
break;
case 'F':
System.out.println("退出計算器");
break;
default:
System.out.println("無效輸入");
break;
}
if(c=='F'){
break;
}
}
}else{
System.out.println("賬號或密碼輸入錯誤!");
System.out.println("是否繼續(xù)輸入?(1.是0.否)");
isgoing = sa.nextInt();
}
}while(isgoing==1);
}
}
以前寫的,給你了
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 ");
//四則運算符號鍵
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 = "";//保存第一個運算數(shù)字
private String operator = "";//保存運算符號
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());
//注冊四則運算監(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{
//如果輸入的是運算符號,保存第一個結(jié)果和運算符號
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,清空所有運算數(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í)行加法運算
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){//減法運算
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){//乘法運算
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){//除法運算
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) {}
}
}
import java.awt.*;
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 javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
/**
* 我的計算器。MyCalculator 繼承于 JFrame,是計算器的界面
*/
public class MyCalculator extends JFrame {
private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);
private JTextField textbox = new JTextField("0");
private CalculatorCore core = new CalculatorCore();
private ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
String label = b.getText();
String result = core.process(label);
textbox.setText(result);
}
};
public MyCalculator(String title) throws HeadlessException {
super(title); // 調(diào)用父類構(gòu)造方法
setupFrame(); // 調(diào)整窗體屬性
setupControls(); // 創(chuàng)建控件
}
private void setupControls() {
setupDisplayPanel(); // 創(chuàng)建文本面板
setupButtonsPanel(); // 創(chuàng)建按鈕面板
}
// 創(chuàng)建按鈕面板并添加按鈕
private void setupButtonsPanel() {
JPanel panel = new JPanel();
panel.setBorder(border);
panel.setLayout(new GridLayout(4, 5, 3, 3));
createButtons(panel, new String[]{
"7", "8", "9", "+", "C",
"4", "5", "6", "-", "CE",
"1", "2", "3", "*", "", // 空字符串表示這個位置沒有按鈕
"0", ".", "=", "/", ""
});
this.add(panel, BorderLayout.CENTER);
}
/**
* 在指定的面板上創(chuàng)建按鈕
*
* @param panel 要創(chuàng)建按鈕的面板
* @param labels 按鈕文字
*/
private void createButtons(JPanel panel, String[] labels) {
for (String label : labels) {
// 如果 label 為空,則表示創(chuàng)建一個空面板。否則創(chuàng)建一個按鈕。
if (label.equals("")) {
panel.add(new JPanel());
} else {
JButton b = new JButton(label);
b.addActionListener(listener); // 為按鈕添加偵聽器
panel.add(b);
}
}
}
// 設(shè)置顯示面板,用一個文本框來作為計算器的顯示部分。
private void setupDisplayPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(border);
setupTextbox();
panel.add(textbox, BorderLayout.CENTER);
this.add(panel, BorderLayout.NORTH);
}
// 調(diào)整文本框
private void setupTextbox() {
textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右對齊
textbox.setEditable(false); // 文本框只讀
textbox.setBackground(Color.white); // 文本框背景色為白色
}
// 調(diào)整窗體
private void setupFrame() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 當窗體關(guān)閉時程序結(jié)束
this.setLocation(100, 50); // 設(shè)置窗體顯示在桌面上的位置
this.setSize(300, 200); // 設(shè)置窗體大小
this.setResizable(false); // 窗體大小固定
}
// 程序入口
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
MyCalculator frame = new MyCalculator("我的計算器");
frame.setVisible(true); // 在桌面上顯示窗體
}
}
/**
* 計算器核心邏輯。這個邏輯只能處理 1~2 個數(shù)的運算。
*/
class CalculatorCore {
private String displayText = "0"; // 要顯示的文本
boolean reset = true;
private BigDecimal number1, number2;
private String operator;
private HashMapString, Operator operators = new HashMapString, Operator();
private HashMapString, Processor processors = new HashMapString, Processor();
CalculatorCore() {
setupOperators();
setupProcessors();
}
// 為每種命令添加處理方式
private void setupProcessors() {
processors.put("[0-9]", new Processor() {
public void calculate(String command) {
numberClicked(command);
}
});
processors.put("\\.", new Processor() {
public void calculate(String command) {
dotClicked();
}
});
processors.put("=", new Processor() {
public void calculate(String command) {
equalsClicked();
}
});
processors.put("[+\\-*/]", new Processor() {
public void calculate(String command) {
operatorClicked(command);
}
});
processors.put("C", new Processor() {
public void calculate(String command) {
clearClicked();
}
});
processors.put("CE", new Processor() {
public void calculate(String command) {
clearErrorClicked();
}
});
}
// 為每種 operator 添加處理方式
private void setupOperators() {
operators.put("+", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.add(number2);
}
});
operators.put("-", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.subtract(number2);
}
});
operators.put("*", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.multiply(number2);
}
});
operators.put("/", new Operator() {
public BigDecimal process(BigDecimal number1, BigDecimal number2) {
return number1.divide(number2, 30, RoundingMode.HALF_UP);
}
});
}
// 根據(jù)命令處理。這里的命令實際上就是按鈕文本。
public String process(String command) {
for (String pattern : processors.keySet()) {
if (command.matches(pattern)) {
processors.get(pattern).calculate(command);
break;
}
}
return displayText;
}
// 當按下 CE 時
private void clearErrorClicked() {
if (operator == null) {
number1 = null;
} else {
number2 = null;
}
displayText = "0";
reset = true;
}
// 當按下 C 時,將計算器置為初始狀態(tài)。
private void clearClicked() {
number1 = null;
number2 = null;
operator = null;
displayText = "0";
reset = true;
}
// 當按下 = 時
private void equalsClicked() {
calculateResult();
number1 = null;
number2 = null;
operator = null;
reset = true;
}
// 計算結(jié)果
private void calculateResult() {
number2 = new BigDecimal(displayText);
Operator oper = operators.get(operator);
if (oper != null) {
BigDecimal result = oper.process(number1, number2);
displayText = result.toString();
}
}
// 當按下 +-*/ 時(這里也可以擴展成其他中間操作符)
private void operatorClicked(String command) {
if (operator != null) {
calculateResult();
}
number1 = new BigDecimal(displayText);
operator = command;
reset = true;
}
// 當按下 . 時
private void dotClicked() {
if (displayText.indexOf(".") == -1) {
displayText += ".";
} else if (reset) {
displayText = "0.";
}
reset = false;
}
// 當按下 0-9 時
private void numberClicked(String command) {
if (reset) {
displayText = command;
} else {
displayText += command;
}
reset = false;
}
// 運算符處理接口
interface Operator {
BigDecimal process(BigDecimal number1, BigDecimal number2);
}
// 按鈕處理接口
interface Processor {
void calculate(String command);
}
}