扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
package com;
公司主营业务:成都网站设计、做网站、成都外贸网站建设公司、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出龙川免费做网站回馈大家。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class calculator implements ActionListener { //导入动作监听接口
//设计面板中的单位
JFrame frame;
JTextField textAnswer;
JPanel panel, panel1, panel2, panel3;
JMenuBar mainMenu;
JTextField textMemory;
JLabel labelMemSpace; //labelMemSpace单纯做摆设,控制面板的形状
JButton buttonBk, buttonCe, buttonC;
JButton button[];
JButton buttonMC, buttonMR, buttonMS, buttonMAdd;
JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,
buttonDiv, buttonMod;
JButton buttonSqrt, buttonDao, buttonEqual;
JMenu editMenu, viewMenu, helpMenu;
JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal;
DecimalFormat df; //设置数据输出精度
boolean clickable; //控制当前能否按键
double memoryd; //使用内存中存储的数字
int memoryi;
double vard, answerd; //用来保存double型数据的中间值(vard)和最后结果(answerd)
short key = -1, prekey = -1; //key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
String copy; //做复制用
JTextArea help; //帮助
JScrollPane scrollHelp;
//构造函数
@SuppressWarnings({ "static-access", "deprecation" })
public calculator() {
clickable = true;
answerd = 0;
frame = new JFrame("计算器");
df = new DecimalFormat("0.##############"); //设置数据输出精度(对于double型值)
textAnswer = new JTextField(15);
textAnswer.setText("");
textAnswer.setEditable(false);
textAnswer.setBackground(new Color(255, 255, 255));
panel = new JPanel();
frame.getContentPane().add(panel);
panel1 = new JPanel();
panel2 = new JPanel();
panel.setLayout(new BorderLayout());
//设计整个面板
package importantpg;
import java.awt.*;
import java.awt.event.*;
public class jisuanqi
{
TextArea tf=new TextArea(3,50 );
Button lb=new Button("clear");
Panel p1=new Panel();
Panel p2=new Panel();
public jisuanqi()
{ Frame f=new Frame("计算器");
Panel p=new Panel();
f.setBackground(Color.white);
f.setSize(500, 1500);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(1);}
});
Button[] b=new Button[18]; b[10]=new Button("-"); b[15]=new Button("(");
b[11]=new Button("+"); b[12]=new Button("*"); b[16]=new Button(")");
b[13]=new Button("/"); b[14]=new Button("="); b[17]=new Button("退格");
for(int i=0;i=9;++i)
{
b[i]=new Button(""+i);
}
p.setLayout(new GridLayout(6,3));
avent aa=new avent();
lb.addActionListener(aa);
for(int i=0;i18;++i)
{
p.add(b[i]);b[i].addActionListener(aa);
}
p1.setLayout(new BorderLayout());
p1.add(lb,"North");p1.add(tf,"South");f.add(p1, "South");
f.add(p,"Center"); f.pack();
} //构造方法
public static void main(String args[])
{
new jisuanqi();
}
class avent implements ActionListener //事件体
{
public void actionPerformed(ActionEvent e)
{
String s=null; String in="";
s=e.getActionCommand();
if (s!="="s!=nulls!="clear"s!="退格")
{
tf.append(s);
in+=s; s=null;
}
else if(s=="clear")
{
tf.replaceRange("",0,1000);
}else if(s=="=")
{
tf.append("="+jisuan(tf.getText()));
}
else if(s=="退格") {}
}///////////////////////////////////////////////事件处理方法
}
///////////////////////////////////////计算过程//////////////
static String jisuan(String s)
{
String hs= "";
char[] ch = new char[s.length()];
int top = -1;
for (int i = 0; i s.length(); i++)
{
char rsc = s.charAt(i);
switch(rsc)
{
case ' ' : break;
case '+' : //操作符
case '-' :
while (top = 0) //栈不为空时
{
char pc = ch[top--]; //pop Operator
if (pc == '(')
{
ch[++top] = pc; //push Operator
break;
}
else {hs = hs + pc;}
}
ch[++top] = rsc; //push Operator
hs += " ";break;
case '*' : //操作符
case '/' :
while (top = 0) //栈不为空时
{
char c = ch[top--]; //pop Operator
if (c == '(')
{
ch[++top] = c; //push Operator
break;
}
else
{
if (c == '+' || c == '-')
{
ch[++top] = c; //push Operator
break;
}
else
{
hs = hs + c;
}
}
}
ch[++top] = rsc; //push Operator
hs += " ";
break;
case '(' : //操作符
ch[++top] = rsc;
hs+= " ";
break;
case ')' : //操作符
while (top = 0) //栈不为空时
{
char c = ch[top--]; //pop Operator
if (c == '(')
{
break;
}
else
{
hs= hs + c;
}
}
hs += " ";
break;
default : //操作数
hs= hs + rsc;
break;
}//switch
}/////////////////////////////for
while (top = 0)
{
hs= hs + ch[top--]; //pop Operator
}
//System.out.println("hs="+hs); //后缀
//////////////////////////////////////////////后缀////////////////////////////////////
//后缀表达式计算
double[] result = new double[hs.length()];
double x, y;
top = - 1;
String st = "";
for (int i = 0; i hs.length(); i++)
{
char c = hs.charAt(i);
if ((c = '0' c = '9') || c == '.')
{
st += c;
}
if ((c == ' ' st != "") || i == hs.length() - 1)
{
result[++top] = Double.parseDouble(st) ; //push Operands
st = "";
}
if (c == '+' || c == '-' || c == '*' || c == '/')
{
if ((st != ""))
{
result[++top] =Double.parseDouble(st) ; //push Operands
st = "";
}
y = result[top--];
x = result[top--];
switch (c)
{
case '+' :
result[++top] = x+y;break;//push 中间结果再次入栈
case '-' :
result[++top] = x-y;break;
case '*' :
result[++top] = x*y;break;
case '/' :
result[++top] = x/y;break;
default :break;
}
}////////////////////if
} ///////////////////////for
return String.valueOf(result[top]); //pop 最终结果
} /////////////////////////jisuan
}
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; //导包 public class Jisuanqi extends JFrame implements ActionListener { //继承JFrame 实现事件监听 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() { //构造属性 JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); TitledBorder tb = new TitledBorder("输出"); tb.setTitleColor(Color.BLUE); //标题边框底端线 设置颜色 btnAbout = new JButton(" 关于 "); btnCancel = new JButton("Cancel"); //两个按钮 btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { System.exit(0); } }); //给Cancel添加事件监听 当鼠标点击时 程序结束 btnAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { JOptionPane.showMessageDialog(null, "黄盖!!", "消息", JOptionPane.INFORMATION_MESSAGE); } //给“关于”添加事件监听 当鼠标点击时 弹出对话框 显示黄盖 }); 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); //设置不能修改 reasult.setHorizontalAlignment(JTextField.RIGHT); // 设置文本的水平对齐方式。 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); //事件监听 + 颜色 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)); //面板上设置对齐方式 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) { // 如果数字输入结束,则将文本框置零,重新输入 reasult.setText("0"); end = false; } if ((reasult.getText()).equals("0")) { // 如果文本框的内容为零,则覆盖文本框的内容 reasult.setText(s); } else { // 如果文本框的内容不为零,则在内容后面添加数字 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; } //设计计算的过程 public static void main(String[] args) { Jisuanqi j = new Jisuanqi(); j.setTitle("+/-简易计算器"); j.setLocation(500, 280); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认关闭可以关闭程序 j.setResizable(false); j.pack(); j.setVisible(true); } } 这个计算机,绝对让你满意
窗体写好了,运算你自己写
import java.awt.Button;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SimpleCalculator {
private JFrame f = new JFrame();
private JPanel firstPanel = new JPanel();
private JLabel firstNumLabel = new JLabel("第一个数字");
private JTextField number1 = new JTextField();
private JPanel secondPanel = new JPanel();
private Button add = new Button("+");
private Button mul = new Button("*");
private Button clear = new Button("清除");
private JPanel thirdPanel = new JPanel();
private JLabel resultLabel = new JLabel("结果为");
private JTextField result = new JTextField();
public SimpleCalculator(){
firstPanel.setLayout(new GridLayout(1, 2));
firstPanel.add(firstNumLabel);
firstPanel.add(number1);
secondPanel.setLayout(new GridLayout(1, 3));
secondPanel.add(add);
secondPanel.add(mul);
secondPanel.add(clear);
thirdPanel.setLayout(new GridLayout(1, 2));
thirdPanel.add(resultLabel);
thirdPanel.add(result);
f.add(new JLabel("简易计算器"));
f.add(firstPanel);
f.add(secondPanel);
f.add(thirdPanel);
f.setLayout(new GridLayout(4, 1));
f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new SimpleCalculator();
}
}
package jisuanqi_new;
import java.awt.*;
import java.awt.event.*;
public class JiSuanQi_new implements ActionListener
{
Panel p1;//声明面板p1
TextField t1;//声明文本行t1
String[] label = {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//声明标签数组label1存放按钮上的标签
Button[] b;//声明按钮数组存放16个按钮
private int i;//声明i以备后用
private String op1 = "0";//运算数备用
private String operator = "+";//运算符备用
private boolean append = false;//备用
public JiSuanQi_new()//构造方法
{
t1=new TextField();//初始化文本行t1
b = new Button[label.length];//初始化按钮数组b
p1=new Panel();//初始化面板p1
p1.setLayout(new GridLayout(4,4,4,4));//使面板选择网格布局管理器以备储存16个按钮(4行4列)
for(int i=0;ib.length;i++)//利用for循环把标签放在按钮上,使每个按钮添加事件监听器,在面板p1上添加上16个按钮
{
b[i] = new Button(label[i]);//把标签依次放在16个按钮上
b[i].addActionListener(this);//使每个按钮添加动作事件监听器
p1.add(b[i]); //分别将按钮添加到面板p1上
}
Frame f=new Frame("计算器1.0");//初始化窗口f,起名字计算器1.0
f.setLayout(new BorderLayout());//为窗口选择边界布局管理器
f.add(BorderLayout.NORTH,t1);//把文本行他添加到窗口的北部
f.add(BorderLayout.CENTER,p1);//把面吧p1添加到窗口的中间
f.addWindowListener(new WindowAdapter(){//给窗口f添加窗口事件监听器
public void windowClosing(WindowEvent eve){//运行窗口关闭方法
System.exit(0);//退出程序
}
});
f.setSize(250, 250);//设置窗口大小
f.setLocation(200,200);
f.setVisible(true);//显示窗口
}
public static void main(String args[])
{
new JiSuanQi_new(); //调用构造方法
}
public void actionPerformed(ActionEvent ae)
{//按钮被操作发生
String comm = ae.getActionCommand();//返回与此动作相关的命令字符串,即:使用者第一次点击的按钮是什么。
if("0123456789".indexOf(comm)!=-1)//如果相关命令字符串为0~9之间的数字则执行
{
if(append){
String temp = t1.getText();//新数字
t1.setText(temp+comm);
}else{ //因为此时append为false执行这个
t1.setText(comm); //将文本行t1设置为相关命令字符串(你按中的按钮代码)
append = true;//此时append=true若继续按按钮若继续按数字的话1.第一次的按话不会改变2.非第一次按得话会覆盖之前按得数字(即缺点:只能进行单个数字的计算)
}
}
else if(("+-*/".indexOf(comm)!=-1))//如果相关命令字符串为+-*/之间的数字则执行
{
//保存
//t1.setText(comm);
op1 = t1.getText();//把第一个数赋值给op1
operator = comm;//把命令字符串赋值给operator
append = false;//此时append为false即若继续按按钮若按数字的话将重复上面的动作,按符号的话将覆盖之前的符号
}
else if("=".equals(comm))//如果按的是=号,则按条件进行下面的运算
{
String op2 = t1.getText();//op2第二个数
double d1 = Double.parseDouble(op1);
double d2 = Double.parseDouble(op2);
if(operator.equals("+")){
d1 = d1 + d2 ;
}else if(operator.equals("-")){
d1 = d1 - d2;
}else if(operator.equals("*")){
d1 = d1 * d2;
}else {
d1 = d1 / d2;
}
t1.setText(d1+"");//显示计算结果
append = false;
}
else if(".".equals(comm))//若是.号继续按
{
String temp = t1.getText();
if(temp.indexOf(".")==-1){
t1.setText(temp+".");
append = true;
}
}
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流