java计算器菜单栏上的“关于计算器”代码怎么写啊?
“关于计算器”这个非常简单呀,首先在菜单栏上添加“关于计算器”这项MenuItem,然后添加监听,功能是点击后菜单之后new 一下这个“关于计算器”的新窗口不就可以了么?新窗口放一个单独的类写,至于那里面想写什么就写什么,随意了,简单介绍,作者什么的,既然都能写出计算器的界面了,这个应该不是什么问题。
创新互联公司专注于企业营销型网站、网站重做改版、天津网站定制设计、自适应品牌网站建设、HTML5、商城网站开发、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为天津等各大城市提供网站开发制作服务。
java界面设计 计算器(有菜单栏(编辑(粘贴,复制,撤销)和工具(加减乘除并且有快捷)))
写给你了
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;// 是否为实数
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;// 操作数
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("复制(C)");
mnuPaste=new JMenuItem("粘贴(P)");
JMenuItem mnuVisit=new JMenuItem("查看帮助(V)");
JMenuItem mnuAbout=new JMenuItem("关于计算器(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("复制(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.")) {
// 如果文本框中有内容
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则清除缓冲区
String temp = "";
memory.replace(0, memory.length(), temp);
bM.setText(" ");
} else if (s.equals("MR")) {
// 如果按键为MR则恢复缓冲区的数到文本框
jt1.setText(memory.toString());
} else if (s.equals("MS")) {
// 如果按键为MS则将文本框的数存入缓冲区
String s1 = jt1.getText().trim();
memory.replace(0, memory.length(), s1);
bM.setText("M");
} else if (s.equals("M+")) {
// 如果按键为MS则将文本框值与缓冲区的数相加但不显示结果
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则将文本框中的数据为它的倒数
String temp = jt1.getText().trim();
double dtemp = Double.parseDouble(temp);
jt1.setText("" + 1 / dtemp);
} else if (s.equals("sqrt")) {
// 如果按键为sqrt则将文本框中的内容求平方根
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"这个数字键
{
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编程跪求一简易计算器带菜单
给你现成的吧,不知道是不是你要的菜单效果:
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", "关闭", };
static double a;
static String s = "", str;// 定义变量 创建对像
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]);
} // 创建按钮 并添加到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());// 添加监听器
}
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);// 都是些窗中设置 添加相关组件和监听器
}
public void windowClosing(WindowEvent e) {
System.exit(0);// 退出系统
}
class buttonlistener implements ActionListener {// 编写监听器事件 通过按键得出给果
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,按钮的定义和功能多的实现麻烦注释一下,速度的
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("复制");
private MenuItem item3 = new MenuItem("粘贴");
private MenuItem item4 = new MenuItem("关于计算机");
// 显示器面板
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();
// 按钮内容
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() {
//设置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);
//修改结束
f.setMenuBar(menubar);
f.pack();
f.setResizable(false);
f.setLocation(400, 350);
f.setVisible(true);
//修改……关闭窗口时,停止程序
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
/**
* 实现事件.
* @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();
//文本框现有值
String text = label.getText();
if (numBtn.indexOf(btnName) 0) {
/**
* 数字操作
*/
//首个数字不能为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)) {
/**
* 小数点操作
*/
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 - 返回计算结果
* @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!
本文标题:java计算器代码带菜单 java计算器算法
文章URL:http://scyingshan.cn/article/dosesdj.html