一个简易计算器的问题
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calc extends JFrame implements ActionListener
{
static int i=0;
JButton jb1=new JButton("0");
JButton jb2=new JButton("1");
JButton jb3=new JButton("2");
JButton jb4=new JButton("3");
JButton jb5=new JButton("4");
JButton jb6=new JButton("5");
JButton jb7=new JButton("6");
JButton jb8=new JButton("7");
JButton jb9=new JButton("8");
JButton jb10=new JButton("9");
JButton jb11=new JButton("+");
JButton jb12=new JButton("-");
JButton jb13=new JButton("=");
JLabel jl=new JLabel("答案是多少");
JPanel jp=new JPanel();
GridLayout gl=new GridLayout(5,2);
public Calc()
{
this.setTitle("简易计算器");
jp.setLayout(gl);
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
jp.add(jb5);
jp.add(jb6);
jp.add(jb7);
jp.add(jb8);
jp.add(jb9);
jp.add(jb10);
jp.add(jb11);
jp.add(jb12);
jp.add(jb13);
jp.add(jl);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jb6.addActionListener(this);
jb7.addActionListener(this);
jb8.addActionListener(this);
jb9.addActionListener(this);
jb10.addActionListener(this);
jb11.addActionListener(this);
jb12.addActionListener(this);
jb13.addActionListener(this);
this.add(jp);
this.setBounds(300,200,500,300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
jl.setText("0");
i=0;
}
if(e.getSource()==jb2)
{
jl.setText("1");
i=1;
}
if(e.getSource()==jb3)
{
jl.setText("2");
i=2;
}
if(e.getSource()==jb4)
{
jl.setText("3");
i=3;
}
if(e.getSource()==jb5)
{
jl.setText("4");
i=4;
}
if(e.getSource()==jb6)
{
jl.setText("5");
i=5;
}
if(e.getSource()==jb7)
{
jl.setText("6");
i=6;
}
if(e.getSource()==jb8)
{
jl.setText("7");
i=7;
}
if(e.getSource()==jb9)
{
jl.setText("8");
i=8;
}
if(e.getSource()==jb10)
{
jl.setText("9");
i=9;
}
if(e.getSource()==jb11)
{
jl.setText(i+"+");
}
if(e.getSource()==jb12)
{
jl.setText(i+"-");
}
if(e.getSource()==jb13)
{
jl.setText("=");
}
}
public static void main(String[] args)
{
Calc cl=new Calc();
}
}
这是我刚学JAVA编写的计算器软件界面,但在事件响应过程中,卡住了,麻烦帮忙改写下哦,,,不知道怎么去响应事件得出的结果,你们运行下,看看如何编写出这个超级简单的计算器咯,不胜感激。。。请按照我的思路,网络上计算器的代码实在太多了,,,这是个只能进行加减的计算器,,,纯粹是想锻炼下。。。。
[解决办法]
觉得比较理想,但还没有做的一个方法:
把输入的数字和运算符存在字符串里
然后自己写一个求中序表达式转后续表达式的类
转成后续就能用栈求值了
计算器我以前也做过个,用的是比较机械的方法,按下运算符时,把刚才输入的值放进字符串数组
然后查看前一个运算符,如果前一个是加号那就不管他,如果前一个是乘号就把数组里最后两个乘起来
只有乘除是优先运算的,加法都不去管它,按等号的时候把数组里的全加起来就行了
[解决办法]
如果要在label上累计显示刚才的输入记录,可以这么写
jl.setText(jl.getText()+"0");
[解决办法]
import javax.swing.*;import java.awt.event.*;import java.awt.*;public class Calc extends JFrame implements ActionListener { private int num1; private int num2; private String operate; JButton jb1 = new JButton("0"); JButton jb2 = new JButton("1"); JButton jb3 = new JButton("2"); JButton jb4 = new JButton("3"); JButton jb5 = new JButton("4"); JButton jb6 = new JButton("5"); JButton jb7 = new JButton("6"); JButton jb8 = new JButton("7"); JButton jb9 = new JButton("8"); JButton jb10 = new JButton("9"); JButton jb11 = new JButton("+"); JButton jb12 = new JButton("-"); JButton jb13 = new JButton("="); JLabel jl = new JLabel(); JPanel jp = new JPanel(); GridLayout gl = new GridLayout(5, 2); public Calc() { this.setTitle("简易计算器"); jp.setLayout(gl); jp.add(jb1); jp.add(jb2); jp.add(jb3); jp.add(jb4); jp.add(jb5); jp.add(jb6); jp.add(jb7); jp.add(jb8); jp.add(jb9); jp.add(jb10); jp.add(jb11); jp.add(jb12); jp.add(jb13); jp.add(jl); jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jb4.addActionListener(this); jb5.addActionListener(this); jb6.addActionListener(this); jb7.addActionListener(this); jb8.addActionListener(this); jb9.addActionListener(this); jb10.addActionListener(this); jb11.addActionListener(this); jb12.addActionListener(this); jb13.addActionListener(this); this.add(jp); this.setBounds(300, 200, 500, 300); this.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == jb1) { jl.setText(jl.getText()+"0"); } if (e.getSource() == jb2) { jl.setText(jl.getText()+"1"); } if (e.getSource() == jb3) { jl.setText(jl.getText()+"2"); } if (e.getSource() == jb4) { jl.setText(jl.getText()+"3"); } if (e.getSource() == jb5) { jl.setText(jl.getText()+"4"); } if (e.getSource() == jb6) { jl.setText(jl.getText()+"5"); } if (e.getSource() == jb7) { jl.setText(jl.getText()+"6"); } if (e.getSource() == jb8) { jl.setText(jl.getText()+"7"); } if (e.getSource() == jb9) { jl.setText(jl.getText()+"8"); } if (e.getSource() == jb10) { jl.setText(jl.getText()+"9"); } if (e.getSource() == jb11) { if(jl.getText()!=null&&jl.getText().trim().length()!=0){ num1 = Integer.parseInt(jl.getText()); } jl.setText(""); operate = "+"; } if (e.getSource() == jb12) { if(jl.getText()!=null&&jl.getText().trim().length()!=0){ num1 = Integer.parseInt(jl.getText()); } jl.setText(""); operate = "-"; } if (e.getSource() == jb13) { if(jl.getText()!=null&&jl.getText().trim().length()!=0){ num2 = Integer.parseInt(jl.getText()); } if(operate != null){ if(operate.equals("+")){ jl.setText(String.valueOf(num1+num2)); } else { jl.setText(String.valueOf(num1-num2)); } } } } public static void main(String[] args) { Calc cl = new Calc(); }}
[解决办法]
还不能支持连加或连减
[解决办法]
。。。。
[解决办法]
怎么不用数组来表示那些按钮?
一行一个add,一行一个if 多烦啊。用数组+循环,代码量大大的减少。