真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java計算器代碼帶菜單 java計算器算法

java計算器菜單欄上的“關(guān)于計算器”代碼怎么寫?。?/h2>

“關(guān)于計算器”這個非常簡單呀,首先在菜單欄上添加“關(guān)于計算器”這項MenuItem,然后添加監(jiān)聽,功能是點擊后菜單之后new 一下這個“關(guān)于計算器”的新窗口不就可以了么?新窗口放一個單獨的類寫,至于那里面想寫什么就寫什么,隨意了,簡單介紹,作者什么的,既然都能寫出計算器的界面了,這個應(yīng)該不是什么問題。

創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、天津網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站開發(fā)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為天津等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

java界面設(shè)計 計算器(有菜單欄(編輯(粘貼,復(fù)制,撤銷)和工具(加減乘除并且有快捷)))

寫給你了

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.event.*;

class JPanelEa1 extends JFrame implements MenuListener,ActionListener

{

JPanel mainp,p1,p2,p3,p4;

JTextField jt1;

JMenuItem mnuCopy;

JMenuItem mnuPaste;

JButton bM;

boolean isDouble = false;// 是否為實數(shù)

int opFlag = -1;

static double t1 = 0, t2 = 0, t3 = 0, result = 0;

static int opflag1 = -1, opflag2 = -1, flag = 0, resflag = 1;

int preOp, currentOp = 0;// 標準位

double op1 = 0, op2 = 0;// 操作數(shù)

double n3;

StringBuffer buf = new StringBuffer(20);

StringBuffer copyBoard = new StringBuffer(20);// 剪貼板

StringBuffer memory = new StringBuffer(20);// M系列

StringBuffer str = new StringBuffer();

public JPanelEa1()

{

p1=new JPanel();

p1.setLayout(new GridLayout(2,1,10,10));

JMenuBar mnuNotepad=new JMenuBar();

JMenu mnuEdit=new JMenu("編輯(E)");

mnuEdit.setMnemonic(KeyEvent.VK_E);

JMenu mnuCheck=new JMenu("查看(V)");

mnuCheck.setMnemonic(KeyEvent.VK_V);

JMenu mnuHelp=new JMenu("幫助(H)");

mnuCopy=new JMenuItem("復(fù)制(C)");

mnuPaste=new JMenuItem("粘貼(P)");

JMenuItem mnuVisit=new JMenuItem("查看幫助(V)");

JMenuItem mnuAbout=new JMenuItem("關(guān)于計算器(A)");

JSeparator sep=new JSeparator();

jt1=new JTextField("0.");

jt1.setEnabled(false);

jt1.setHorizontalAlignment(JTextField.RIGHT);

mnuEdit.addMenuListener(this);

mnuCheck.addMenuListener(this);

mnuHelp.addMenuListener(this);

mnuCopy.addActionListener(this);

mnuPaste.addActionListener(this);

mnuVisit.addActionListener(this);

mnuAbout.addActionListener(this);

mnuNotepad.add(mnuEdit);

mnuNotepad.add(mnuCheck);

mnuNotepad.add(mnuHelp);

mnuEdit.add(mnuCopy);

mnuEdit.add(mnuPaste);

mnuEdit.add(sep);

mnuHelp.add(mnuVisit);

mnuHelp.add(sep);

mnuHelp.add(mnuAbout);

p1.add(mnuNotepad);

p1.add(jt1);

p2=new JPanel();

p2.setLayout(new FlowLayout(FlowLayout.CENTER));

bM = new JButton(" ");

bM.setEnabled(false);

p2.add(bM);

Button b25=new Button("Backspace");

b25.addActionListener(this);

Button b26=new Button("CE");

b26.addActionListener(this);

Button b27=new Button("C");

b27.addActionListener(this);

p2.add(b25);

p2.add(b26);

p2.add(b27);

p3=new JPanel();

p3.setLayout(new GridLayout(4,6));

Button button1=new Button("MC");

button1.addActionListener(this);

Button button2=new Button("7");

button2.addActionListener(this);

Button button3=new Button("8");

button3.addActionListener(this);

Button button4=new Button("9");

button4.addActionListener(this);

Button button5=new Button("/");

button5.addActionListener(this);

Button button6=new Button("sqrt");

button6.addActionListener(this);

Button button7=new Button("MR");

button7.addActionListener(this);

Button button8=new Button("4");

button8.addActionListener(this);

Button button9=new Button("5");

button9.addActionListener(this);

Button button10=new Button("6");

button10.addActionListener(this);

Button button11=new Button("*");

button11.addActionListener(this);

Button button12=new Button("%");

button12.addActionListener(this);

Button button13=new Button("MS");

button13.addActionListener(this);

Button button14=new Button("1");

button14.addActionListener(this);

Button button15=new Button("2");

button15.addActionListener(this);

Button button16=new Button("3");

button16.addActionListener(this);

Button button17=new Button("-");

button17.addActionListener(this);

Button button18=new Button("1/x");

button18.addActionListener(this);

Button button19=new Button("M+");

button19.addActionListener(this);

Button button20=new Button("0");

button20.addActionListener(this);

Button button21=new Button("+/-");

button21.addActionListener(this);

Button button22=new Button(".");

button22.addActionListener(this);

Button button23=new Button("+");

button23.addActionListener(this);

Button button24=new Button("=");

button24.addActionListener(this);

p3.add(button1);p3.add(button2);

p3.add(button3);p3.add(button4);

p3.add(button5);p3.add(button6);

p3.add(button7);p3.add(button8);

p3.add(button9);p3.add(button10);

p3.add(button11);p3.add(button12);

p3.add(button13);p3.add(button14);

p3.add(button15);p3.add(button16);

p3.add(button17);p3.add(button18);

p3.add(button19);p3.add(button20);

p3.add(button21);p3.add(button22);

p3.add(button23);p3.add(button24);

setSize(250,150);

mainp=new JPanel();

mainp.setLayout(new GridLayout(3,1));

mainp.add(p1);

mainp.add(p2);

mainp.add(p3);

setContentPane(mainp);

setTitle("計算器");

setSize(300,300);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e)

{

String s= e.getActionCommand();

if (s.equals("復(fù)制(C)")) {

String temp = jt1.getText().trim();

copyBoard.replace(0, copyBoard.length(), temp);

mnuPaste.setEnabled(true);

} else if (s.equals("粘貼(p)")) {

jt1.setText(copyBoard.toString());

} else if (s.equals("CE")) {

// 如果是CE則清除文本框

jt1.setText("0.");

} else if (s.equals("Backspace")) {

if (!jt1.getText().trim().equals("0.")) {

// 如果文本框中有內(nèi)容

if (str.length() != 1 str.length() != 0) {

jt1.setText(str.delete(str.length() - 1, str.length())

.toString());

} else {

jt1.setText("0.");

str.setLength(0);

}

}

op2 = Double.parseDouble(jt1.getText().trim());

} else if (s.equals("C")) {

// 如果是C刪除當前計算

jt1.setText("0.");

op1 = op2 = 0;

str.replace(0, str.length(), " ");

preOp = currentOp = 0;

} else if (s.equals("MC")) {

// 如果是MC則清除緩沖區(qū)

String temp = "";

memory.replace(0, memory.length(), temp);

bM.setText(" ");

} else if (s.equals("MR")) {

// 如果按鍵為MR則恢復(fù)緩沖區(qū)的數(shù)到文本框

jt1.setText(memory.toString());

} else if (s.equals("MS")) {

// 如果按鍵為MS則將文本框的數(shù)存入緩沖區(qū)

String s1 = jt1.getText().trim();

memory.replace(0, memory.length(), s1);

bM.setText("M");

} else if (s.equals("M+")) {

// 如果按鍵為MS則將文本框值與緩沖區(qū)的數(shù)相加但不顯示結(jié)果

String temp1 = jt1.getText().trim();

double dtemp = Double.parseDouble(temp1);

String temp2 = memory.toString();

dtemp += Double.parseDouble(temp2);

temp1 = String.valueOf(dtemp);

memory.replace(0, memory.length(), temp1);

} else if (s.equals("1/x")) {

// 如果按鍵為1/x則將文本框中的數(shù)據(jù)為它的倒數(shù)

String temp = jt1.getText().trim();

double dtemp = Double.parseDouble(temp);

jt1.setText("" + 1 / dtemp);

} else if (s.equals("sqrt")) {

// 如果按鍵為sqrt則將文本框中的內(nèi)容求平方根

String temp = jt1.getText().trim();

double dtemp = Double.parseDouble(temp);

jt1.setText("" + Math.sqrt(dtemp));

} else if (s.equals("+")) {

str.setLength(0);

if (currentOp == 0) {

preOp = currentOp = 1;

op2 = 0;

jt1.setText("" + op1);

} else {

currentOp = preOp;

preOp = 1;

switch (currentOp) {

case 1:

op1 += op2;

jt1.setText("" + op1);

break;

case 2:

op1 -= op2;

jt1.setText("" + op1);

break;

case 3:

op1 *= op2;

jt1.setText("" + op1);

break;

case 4:

op1 /= op2;

jt1.setText("" + op1);

break;

}

}

} else if (s.equals("-")) {

str.setLength(0);

if (currentOp == 0) {

preOp = currentOp = 2;// op1=op2;op2=0;

jt1.setText("" + op1);

} else {

currentOp = preOp;

preOp = 2;

switch (currentOp) {

case 1:

op1 = op1 + op2;

jt1.setText("" + op1);

break;

case 2:

op1 = op1 - op2;

jt1.setText("" + op1);

break;

case 3:

op1 = op1 * op2;

jt1.setText("" + op1);

break;

case 4:

op1 = op1 / op2;

jt1.setText("" + op1);

break;

}

}

} else if (s.equals("*"))// *

{

str.setLength(0);

if (currentOp == 0) {

preOp = currentOp = 3;// op1=op2;op2=1;

jt1.setText("" + op1);// op1=op2;

} else {

currentOp = preOp;

preOp = 3;

switch (currentOp) {

case 1:

op1 = op1 + op2;

jt1.setText("" + op1);

break;

case 2:

op1 = op1 - op2;

jt1.setText("" + op1);

break;

case 3:

op1 = op1 * op2;

jt1.setText("" + op1);

break;

case 4:

op1 = op1 / op2;

jt1.setText("" + op1);

break;

}

}

} else if (s.equals("/"))// /

{

str.setLength(0);

if (currentOp == 0) {

preOp = currentOp = 4;// op2=1;

jt1.setText("" + op1);// op1=op2;

} else {

currentOp = preOp;

preOp = 4;

switch (currentOp) {

case 1:

op1 = op1 + op2;

jt1.setText("" + op1);

break;

case 2:

op1 = op1 - op2;

jt1.setText("" + op1);

break;

case 3:

op1 = op1 * op2;

jt1.setText("" + op1);

break;

case 4:

op1 = op1 / op2;

jt1.setText("" + op1);

break;

}

}

} else if (s.equals("="))// =

{

if (currentOp == 0) {

str.setLength(0);

jt1.setText("" + op2);

} else {

str.setLength(0);

currentOp = preOp;

switch (currentOp) {

case 1:

op1 = op1 + op2;

jt1.setText("" + op1);

break;

case 2:

op1 = op1 - op2;

jt1.setText("" + op1);

break;

case 3:

op1 = op1 * op2;

jt1.setText("" + op1);

break;

case 4:

op1 = op1 / op2;

jt1.setText("" + op1);

break;

}

currentOp = 0;

op2 = 0;

}

} else if (s.equals(".")) {

isDouble = true;

if (jt1.getText().trim().indexOf('.') != -1)

;

else {

if (jt1.getText().trim().equals("0")) {

str.setLength(0);

jt1.setText((str.append("0" + s)).toString());

}

// else

// if(jt1.getText().trim().equals("")){}//如果初時顯示為空則不做任何操作

else {

jt1.setText((str.append(s)).toString());

}

}

} else if (s.equals("0"))// 如果選擇的是"0"這個數(shù)字鍵

{

if (jt1.getText().trim().equals("0.")) {

} else {

jt1.setText(str.append(s).toString());

op2 = Double.parseDouble(jt1.getText().trim());

}

} else {

jt1.setText(str.append(s).toString());

op2 = Double.parseDouble(jt1.getText().trim());

if (currentOp == 0)

op1 = op2;

}

}

public static void main(String args[])

{

JPanelEa1 g=new JPanelEa1();

}

@Override

public void menuSelected(MenuEvent e) {

// TODO Auto-generated method stub

}

@Override

public void menuDeselected(MenuEvent e) {

// TODO Auto-generated method stub

}

@Override

public void menuCanceled(MenuEvent e) {

// TODO Auto-generated method stub

}

}

JAVA編程跪求一簡易計算器帶菜單

給你現(xiàn)成的吧,不知道是不是你要的菜單效果:

import java.awt.*;

import java.awt.event.*;

public class JSQ extends WindowAdapter {

Panel p1 = new Panel();

Panel p2 = new Panel();

Panel p3 = new Panel();

TextField txt;

private Button[] b = new Button[19];

private String ss[] = { "1", "2", "3", "+", "-", "4", "5", "6", "*", "/",

"7", "8", "9", "0", "=", "%", "清空", "sqrt", "關(guān)閉", };

static double a;

static String s = "", str;// 定義變量 創(chuàng)建對像

public static void main(String args[]) {

(new JSQ()).frame();

}

public void frame() {

Frame fm = new Frame("計算器");

for (int i = 0; i = 18; i++) {

b[i] = new Button(ss[i]);

}

for (int i = 0; i = 17; i++) {

p2.add(b[i]);

} // 創(chuàng)建按鈕 并添加到P2

b[18].setBackground(Color.yellow);

txt = new TextField(16);

txt.setEditable(false);

for (int i = 0; i = 18; i++) {

b[i].addActionListener(new buttonlistener());// 添加監(jiān)聽器

}

b[18].addActionListener(new close());

fm.addWindowListener(this);

fm.setBackground(Color.red);

p1.setLayout(new BorderLayout());

p1.add(txt, "North");

p2.setLayout(new GridLayout(4, 4));

p3.setLayout(new BorderLayout());

p3.add(b[18]);

fm.add(p1, "North");

fm.add(p2, "Center");

fm.add(p3, "South");

fm.pack();

fm.setVisible(true);// 都是些窗中設(shè)置 添加相關(guān)組件和監(jiān)聽器

}

public void windowClosing(WindowEvent e) {

System.exit(0);// 退出系統(tǒng)

}

class buttonlistener implements ActionListener {// 編寫監(jiān)聽器事件 通過按鍵得出給果

public void actionPerformed(ActionEvent e) {

Button btn = (Button) e.getSource();

if (btn.getLabel().equals("=")) {

if (s.equals("sqrt")) {

str = String.valueOf(a);

txt.setText(str);

s = "";

} else {

jisuan();

str = String.valueOf(a);

txt.setText(str);

s = "";

}

} else if (btn.getLabel().equals("+")) {

jisuan();

txt.setText("");

s = "+";

} else if (btn.getLabel().equals("-")) {

jisuan();

txt.setText("");

s = "-";

} else if (btn.getLabel().equals("/")) {

jisuan();

txt.setText("");

s = "/";

} else if (btn.getLabel().equals("*")) {

jisuan();

txt.setText("");

s = "*";

} else if (btn.getLabel().equals("%")) {

jisuan();

txt.setText("");

s = "%";

} else if (btn.getLabel().equals("sqrt")) {

s = "sqrt";

jisuan();

txt.setText("");

} else {

txt.setText(txt.getText() + btn.getLabel());

if (btn.getLabel() == "清空")

txt.setText("");

}

}

public void jisuan() {// 編寫具體計算方法

if (s.equals("+"))

a += Double.parseDouble(txt.getText());

else if (s.equals("-"))

a -= Double.parseDouble(txt.getText());

else if (s.equals("*"))

a *= Double.parseDouble(txt.getText());

else if (s.equals("/"))

a /= Double.parseDouble(txt.getText());

else if (s.equals("%"))

a %= Double.parseDouble(txt.getText());

else if (s.equals("sqrt"))

a = Math.sqrt(Double.parseDouble(txt.getText()));

else

a = Double.parseDouble(txt.getText());

}

}

}

class close implements ActionListener {// 退出

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

}

求計算器的JAVA代碼,要純AWT,按鈕的定義和功能多的實現(xiàn)麻煩注釋一下,速度的

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Frame;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.Menu;

import java.awt.MenuBar;

import java.awt.MenuItem;

import java.awt.Panel;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.math.BigDecimal;

public class Calculator implements ActionListener {

private Frame f = new Frame("計算器");

// 菜單欄

private MenuBar menubar = new MenuBar();

private Menu menu1 = new Menu("查看");

private Menu menu2 = new Menu("編輯");

private Menu menu3 = new Menu("幫助");

private MenuItem item1 = new MenuItem("標準型");

private MenuItem item2 = new MenuItem("復(fù)制");

private MenuItem item3 = new MenuItem("粘貼");

private MenuItem item4 = new MenuItem("關(guān)于計算機");

// 顯示器面板

private Panel panel1 = new Panel();

private TextField label = new TextField(21);

// 按鈕面板

private Panel panel2 = new Panel();

private GridBagLayout gb = new GridBagLayout();

private GridBagConstraints gbc = new GridBagConstraints();

// 按鈕內(nèi)容

private String[] cal = {"MC", "MR", "MS", "M+", "M-", "←", "CE", "C", "±", "√", "7", "8", "9", "/", "%", "4",

"5", "6", "*", "1/x", "1", "2", "3", "-", "=", "0", ".", "+"};

private Button[] bs = new Button[28];

// 臨時存儲

private double tmp1 = 0;

private double tmp2 = 0;

// 判斷臨時存儲器中是否有值

private boolean istmp1 = false, istmp2 = false;

//臨時保存運算符

private String tmpoperator = "";

// 是否清空

private boolean isclear = false;

public void init() {

//設(shè)置Frame布局方式

f.setLayout(new BorderLayout());

// 初始化界面

label.setEditable(false);

label.setText("0");

panel1.add(label);

panel2.setLayout(gb);

gbc.gridwidth = 1;

gbc.gridheight = 1;

gbc.fill = GridBagConstraints.BOTH;

gbc.insets = new Insets(2, 2, 2, 2);

gbc.gridx = 0;

gbc.gridy = 0;

for (int i = 0; i bs.length; i++) {

bs[i] = new Button(cal[i]);

int x = 1;

String actionCommand = bs[i].getActionCommand().trim();

if("0".equals(actionCommand)) {

gbc.gridwidth = 2;

x = 2;

} else if("=".equals(actionCommand)) {

gbc.gridheight = 2;

}

gb.setConstraints(bs[i], gbc);

panel2.add(bs[i]);

gbc.gridwidth = 1;

gbc.gridheight = 1;

gbc.gridx += x;

if((i+1)%5 == 0) {

gbc.gridx = 0;

gbc.gridy += 1;

}

//為按鈕添加事件

bs[i].addActionListener(this);

}

menu1.add(item1);

menu2.add(item2);

menu2.add(item3);

menu3.add(item4);

menubar.add(menu1);

menubar.add(menu2);

menubar.add(menu3);

//修改開始

f.add(panel1, BorderLayout.NORTH);

f.add(panel2, BorderLayout.CENTER);

//修改結(jié)束

f.setMenuBar(menubar);

f.pack();

f.setResizable(false);

f.setLocation(400, 350);

f.setVisible(true);

//修改……關(guān)閉窗口時,停止程序

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

/**

* 實現(xiàn)事件.

* @param e

* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)

*/

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String numBtn = ",0,1,2,3,4,5,6,7,8,9,";

String operator = ",+,-,*,/,";

//按鈕名稱

String btnName = e.getActionCommand();

//文本框現(xiàn)有值

String text = label.getText();

if (numBtn.indexOf(btnName) 0) {

/**

* 數(shù)字操作

*/

//首個數(shù)字不能為0

if (text.length() == 1 "0".equals(btnName)) {

label.setText("0");

} else {

if ("0".equals(text)) {

text = "";

}

if (isclear) {

text = "";

isclear = false;

}

label.setText(text + btnName);

}

} else if (".".equals(btnName)) {

/**

* 小數(shù)點操作

*/

if (text.indexOf(".") == -1) {

label.setText(text + btnName);

}

} else if (operator.indexOf(btnName) 0) {

/**

* 運算符操作

*/

isclear = true;

if (!istmp1) {

tmp1 = Double.parseDouble(text);

istmp1 = true;

} else if (!istmp2) {

tmp2 = Double.parseDouble(text);

istmp2 = true;

}

if (istmp1 istmp2 !"".equals(tmpoperator)) {

tmp1 = operator();

label.setText(String.valueOf(tmp1));

tmp2 = 0;

istmp1 = true;

istmp2 = false;

}

tmpoperator = btnName;

} else if ("=".equals(btnName)) {

if (istmp1 !istmp2) {

tmp2 = Double.parseDouble(text);

istmp2 = true;

}

if (istmp1 istmp2 !"".equals(tmpoperator)) {

label.setText(String.valueOf(operator()));

tmp1 = 0;

tmp2 = 0;

istmp1 = false;

istmp2 = false;

}

isclear = true;

}

}

/**

* @Description:計算

* @return - 返回計算結(jié)果

* @throws

*/

public double operator() {

BigDecimal dec1 = BigDecimal.valueOf(tmp1);

BigDecimal dec2 = BigDecimal.valueOf(tmp2);

if ("+".equals(tmpoperator)) {

return dec1.add(dec2).doubleValue();

} else if ("-".equals(tmpoperator)) {

return dec1.subtract(dec2).doubleValue();

} else if ("*".equals(tmpoperator)) {

return dec1.multiply(dec2).doubleValue();

} else if ("/".equals(tmpoperator)) {

return dec1.divide(dec2).doubleValue();

}

return 0;

}

public static void main(String[] arg) {

new Calculator().init();

}

}

只做了+-*/操作,其他操作主要就是在事件中進行判斷!

有問題再追問吧,good luck!


網(wǎng)站欄目:java計算器代碼帶菜單 java計算器算法
文章源于:http://weahome.cn/article/dosesdj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部